numbers类
本帖最后由 bin554385863 于 2019-11-21 13:32 编辑数字的字符串
2019年11月21日11:44:00
numbers.h
#ifndef NUMBERS_H
#define NUMBERS_H
#include <iostream>
#include <initializer_list>
template <typename tp>
class numbers
{
private:
size_t numsLen;
tp *numsPtr;
public:
//默认构造
numbers() : numsLen(0), numsPtr(nullptr) {}
//使用C风格数字数组构造
numbers(const tp *arr, const size_t arrSize);
//可变参数构造
numbers(std::initializer_list<tp> list);
//拷贝构造
numbers(const numbers<tp> &nums);
/*--------------------------------------------------------*/
//按索引获取元素;按索引修改元素的值
tp &at(const size_t index);
//对象的大小
size_t size()const;
/*--------------------------------------------------------*/
//重载下标运算符
tp &operator[](const size_t index);
//重载函数调用符()
numbers<tp> &operator()(const numbers<tp> &nums);
/*--------------------------------------------------------*/
//添加元素
numbers<tp> operator+(const tp num);
//合并对象
numbers<tp> operator+(const numbers<tp> &nums);
/*--------------------------------------------------------*/
//深度拷贝
numbers<tp> &operator=(const numbers<tp> &nums);
friend std::ostream& operator<<(std::ostream& os, numbers<tp>& nums)
{
size_t sz = nums.size();
for (size_t i = 0; i < sz; i++)
{
os<<nums<<" ";
}
return os;
}
~numbers()
{
delete [] numsPtr;
}
};
#endif
----------------------------
numbers.cpp
#include "numbers.h"
//使用C风格数字数组构造
template <typename tp>
numbers<tp>::numbers(const tp *arr, const size_t arrSize) : numsLen(arrSize)
{
numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(arr + i);
}
}
//可变参数构造
template <typename tp>
numbers<tp>::numbers(std::initializer_list<tp> list) : numsLen(list.size())
{
numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(list.begin() + i);
}
}
//拷贝构造
template <typename tp>
numbers<tp>::numbers(const numbers<tp> &nums) : numsLen(nums.numsLen)
{
numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(nums.numsPtr + i);
}
}
/*--------------------------------------------------------------------*/
//按索引获取元素;按索引修改元素的值
template <typename tp>
inline tp &numbers<tp>::at(const size_t index)
{
return numsPtr;
}
//对象的大小
template <typename tp>
inline size_t numbers<tp>::size() const
{
return numsLen;
}
/*--------------------------------------------------------*/
//重载下标运算符
template <typename tp>
inline tp &numbers<tp>::operator[](const size_t index)
{
return numsPtr;
}
template <typename tp>
numbers<tp> &numbers<tp>::operator()(const numbers<tp> &nums)
{
delete[] numsPtr;
numsLen = nums.numsLen;
numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(nums.numsPtr + i);
}
return *this;
}
/*--------------------------------------------------------*/
//添加元素
template <typename tp>
numbers<tp> numbers<tp>::operator+(const tp num)
{
numbers<tp> t_nums(numsPtr, numsLen);
if (t_nums.numsPtr != nullptr)
{
tp *t_numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(t_numsPtr + i) = this->at(i);
}
*(t_numsPtr + numsLen) = num;
t_nums.numsLen++;
delete[] t_nums.numsPtr;
t_nums.numsPtr = t_numsPtr;
}
else
{
t_nums.numsLen+=1;
t_nums.numsPtr = new tp{num};
}
return t_nums;
}
//合并对象
template <typename tp>
numbers<tp> numbers<tp>::operator+(const numbers<tp> &nums)
{
numbers<tp> t_nums(numsPtr, numsLen);
for (size_t i = 0; i < nums.numsLen; i++)
{
t_nums = t_nums + *(nums.numsPtr+i);
}
return t_nums;
}
/*--------------------------------------------------------*/
//深度拷贝
template <typename tp>
numbers<tp> &numbers<tp>::operator=(const numbers<tp> &nums)
{
if (numsPtr != nullptr)
{
numsLen = nums.numsLen;
delete[] numsPtr;
numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(nums.numsPtr + i);
}
}
else
{
numsLen = nums.numsLen;
numsPtr = new tp;
for (size_t i = 0; i < nums.numsLen; i++)
{
this->at(i) = *(nums.numsPtr + i);
}
}
return *this;
}
--------------------------------------------------------------
main.c
#include "numbers.cpp"
int main(int argc, char const *argv[])
{
numbers<int> nums = {1, 2}, a, b = {5, 6}, c = {7, 8, 9};
a(nums);//测试()的重载
a = a + 3 + 4 + b + c + 10;//测试+号的重载
std::cout<<a<<"\nsize = "<<a.size()<<std::endl;
return 0;
}
----------------------------------------------------------------------
Microsoft Windows [版本 10.0.18363.476]
(c) 2019 Microsoft Corporation。保留所有权利。
E:\Users\admin\Documents\VScode>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.1\debugAdapters\bin\WindowsDebugLauncher.exe
--stdin=Microsoft-MIEngine-In-k52q2f0n.kxj --stdout=Microsoft-MIEngine-Out-nocoxtbv.vhe --stderr=Microsoft-MIEngine-Error-xpdxnbxc.0yk
--pid=Microsoft-MIEngine-Pid-xqgq4qdr.b0g --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
1 2 3 4 5 6 7 8 9 10
size = 10
E:\Users\admin\Documents\VScode> 本帖最后由 bin554385863 于 2019-11-21 14:15 编辑
---删除()的重载:
原因:与=的深度复制功能重复
numbers.h
#ifndef NUMBERS_H
#define NUMBERS_H
#include <iostream>
#include <initializer_list>
template <typename tp>
class numbers
{
private:
size_t numsLen;
tp *numsPtr;
public:
//默认构造
numbers() : numsLen(0), numsPtr(nullptr) {}
//使用C风格数字数组构造
numbers(const tp *arr, const size_t arrSize);
//可变参数构造
numbers(std::initializer_list<tp> list);
//拷贝构造
numbers(const numbers<tp> &nums);
/*--------------------------------------------------------*/
//按索引获取元素;按索引修改元素的值
tp &at(const size_t index);
//对象的大小
size_t size()const;
/*--------------------------------------------------------*/
//重载下标运算符
tp &operator[](const size_t index);
/*--------------------------------------------------------*/
//添加元素
numbers<tp> operator+(const tp num);
//合并对象
numbers<tp> operator+(const numbers<tp> &nums);
/*--------------------------------------------------------*/
//深度拷贝
numbers<tp> &operator=(const numbers<tp> &nums);
friend std::ostream& operator<<(std::ostream& os, numbers<tp>& nums)
{
size_t sz = nums.size();
for (size_t i = 0; i < sz; i++)
{
os<<nums<<" ";
}
return os;
}
~numbers()
{
delete [] numsPtr;
}
};
#endif
numbers.cpp
#include "numbers.h"
//使用C风格数字数组构造
template <typename tp>
numbers<tp>::numbers(const tp *arr, const size_t arrSize) : numsLen(arrSize)
{
numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(arr + i);
}
}
//可变参数构造
template <typename tp>
numbers<tp>::numbers(std::initializer_list<tp> list) : numsLen(list.size())
{
numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(list.begin() + i);
}
}
//拷贝构造
template <typename tp>
numbers<tp>::numbers(const numbers<tp> &nums) : numsLen(nums.numsLen)
{
numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(nums.numsPtr + i);
}
}
/*--------------------------------------------------------------------*/
//按索引获取元素;按索引修改元素的值
template <typename tp>
inline tp &numbers<tp>::at(const size_t index)
{
return numsPtr;
}
//对象的大小
template <typename tp>
inline size_t numbers<tp>::size() const
{
return numsLen;
}
/*--------------------------------------------------------*/
//重载下标运算符
template <typename tp>
inline tp &numbers<tp>::operator[](const size_t index)
{
return numsPtr;
}
/*--------------------------------------------------------*/
//添加元素
template <typename tp>
numbers<tp> numbers<tp>::operator+(const tp num)
{
numbers<tp> t_nums(numsPtr, numsLen);
if (t_nums.numsPtr != nullptr)
{
tp *t_numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(t_numsPtr + i) = this->at(i);
}
*(t_numsPtr + numsLen) = num;
t_nums.numsLen++;
delete[] t_nums.numsPtr;
t_nums.numsPtr = t_numsPtr;
}
else
{
t_nums.numsLen+=1;
t_nums.numsPtr = new tp{num};
}
return t_nums;
}
//合并对象
template <typename tp>
numbers<tp> numbers<tp>::operator+(const numbers<tp> &nums)
{
numbers<tp> t_nums(numsPtr, numsLen);
for (size_t i = 0; i < nums.numsLen; i++)
{
t_nums = t_nums + *(nums.numsPtr+i);
}
return t_nums;
}
/*--------------------------------------------------------*/
//深度拷贝
template <typename tp>
numbers<tp> &numbers<tp>::operator=(const numbers<tp> &nums)
{
if (numsPtr != nullptr)
{
numsLen = nums.numsLen;
delete[] numsPtr;
numsPtr = new tp;
for (size_t i = 0; i < numsLen; i++)
{
*(numsPtr + i) = *(nums.numsPtr + i);
}
}
else
{
numsLen = nums.numsLen;
numsPtr = new tp;
for (size_t i = 0; i < nums.numsLen; i++)
{
this->at(i) = *(nums.numsPtr + i);
}
}
return *this;
}
页:
[1]