class Foo {
public:
Foo(); // 构造函数
Foo(string &s);
// ...
};
class Foo {
public:
Foo();
Foo(const Foo&); // 拷贝构造函数
// ...
};
class Foo {
public:
Foo();
Foo& operator=(const Foo&); // 赋值运算符
// ...
};
class Foo {
public:
~Foo(); // 析构函数
// ...
};
class DArray
{
private:
double *m_Data; // 存放数组的动态内存指针
int m_Size; // 数组的元素个数
int m_Max; // 预留给动态数组的内存大小
private:
void Init(); // 初始化
void Free(); // 释放动态内存
inline bool InvalidateIndex(int nIndex); // 判断下标的合法性
public:
DArray(); // 默认构造函数
DArray(int nSize, double dValue = 0); // 构造函数,设置数组大小,默认值为dValue
DArray(const DArray& arr); // 拷贝构造函数
DArray& operator=(const DArray& arr); // 拷贝赋值运算符
~DArray(); // 析构函数
void Print(); // 输出显式所有数组元素的值
int GetSize(); // 获取数组的大小(元素个数)
void SetSize(int nSize); // 重新设置数组的大小,若nSize小于原大小,截断;否则,新元素置0
double GetAt(int nIndex); // 获取指定位置元素
void SetAt(int nIndex,double dValue); // 重置指定元素的值
void PushBack(double dValue); // 追加一个新元素到数组末尾
void DeleteAt(int nIndex); // 删除指定位置地元素
void InsertAt(int nIndex, double dValue); // 插入一个新的元素到数组中
double operator[](int nIndex) const; // 重载下标运算符[]
};
void DArray::Init()
{
m_Size = 0; // 默认情况下数组不包含元素
m_Max = 1;
m_Data = new double[m_Max];
}
void DArray::Free()
{
delete [] m_Data;
}
bool DArray::InvalidateIndex(int nIndex)
{
if(nIndex>=0 && nIndex<m_Size)
return false;
else
return true;
}
// 默认构造函数
DArray::DArray()
{
Init();
}
// 构造函数
DArray::DArray(int nSize, double dValue)
{
if(nSize == 0)
Init();
else
{
m_Size = nSize;
m_Max = nSize;
m_Data = new double[m_Max];
for(int i=0; i<nSize; ++i)
m_Data[i]=dValue;
}
}
// 拷贝构造函数
DArray::DArray(const DArray& arr)
{
m_Size = arr.m_Size; /*复制常规成员*/
m_Max = arr.m_Max;
m_Data = new double[m_Max]; /*复制指针指向的内容*/
memcpy(m_Data, arr.m_Data, m_Size*sizeof(double));
}
// 拷贝赋值运算符
DArray& DArray::operator=(const DArray& arr)
{
if(this == &arr) /*自赋值*/
return *this;
m_Size = arr.m_Size;
m_Max = arr.m_Max;
/* 先将右侧对象拷贝到临时对象中,然后再销毁左侧对象*/
double *m_Temp = new double[m_Max];
memcpy(m_Temp, arr.m_Data, m_Size*sizeof(double));
delete [] m_Data;
m_Data = m_Temp;
return *this;
}
// 析构函数
DArray::~DArray()
{
Free();
}
// 打印数组
void DArray::Print()
{
if(m_Size == 0)
{
cout << "Error: The empty array can't be Printed." << endl;
exit(0);
}
else
{
for(int i=0; i<m_Size; ++i)
cout << m_Data[i] << " ";
cout << endl;
}
}
// 获取数组大小
int DArray::GetSize()
{
return m_Size;
}
// 重置数组大小
void DArray::SetSize(int nSize)
{
if(nSize < m_Size) /*截断*/
{
for(int i=nSize; i<m_Size; ++i)
m_Data[i] = 0;
}
if(m_Size<=nSize && nSize<=m_Max) /*新增元素置0*/
{
for(int i=m_Size; i<nSize; ++i)
m_Data[i] = 0;
}
if(nSize > m_Max) /*需要重新分配空间*/
{
m_Max = nSize;
double *temp = new double[m_Max];
memcpy(temp, m_Data, m_Size*sizeof(double));
for(int i=m_Size; i<nSize; ++i)
temp[i] = 0;
delete [] m_Data;
m_Data = temp;
}
m_Size = nSize; /*设置数组大小*/
}
// 获取指定位置元素
double DArray::GetAt(int nIndex)
{
if(InvalidateIndex(nIndex))
{
cout << "Error: the index of GetAt is invalid!" << endl;
exit(0);
}
return m_Data[nIndex];
}
// 设置指定位置元素的值
void DArray::SetAt(int nIndex, double dValue)
{
if(InvalidateIndex(nIndex))
{
cout << "Error: the index of SetAt is invalid!" << endl;
exit(0);
}
else
{
m_Data[nIndex] = dValue;
}
}
// 追加一个新元素到数组末尾
void DArray::PushBack(double dValue)
{
if(m_Size < m_Max)
{
m_Data[m_Size] = dValue;
}
else
{
m_Max = m_Max*2;
double* temp = new double[m_Max];
memcpy(temp, m_Data, m_Size*sizeof(double));
delete [] m_Data;
m_Data = temp;
m_Data[m_Size] = dValue;
}
++m_Size; /*数组大小加1*/
}
// 从数组中删除一个元素
void DArray::DeleteAt(int nIndex)
{
if(InvalidateIndex(nIndex))
{
cout << "Error: the index of DeleteAt is invalid." << endl;
exit(0);
}
else
{
for(int i=nIndex; i<m_Size; ++i)
m_Data[i] = m_Data[i+1];
m_Data[m_Size-1] = 0;
--m_Size;
}
}
// 插入一个新元素到指定位置
void DArray::InsertAt(int nIndex, double dValue)
{
if(nIndex<0 || nIndex>m_Size)
{
cout << "Error: the index of InsertAt is invalid!" << endl;
exit(0);
}
if(m_Size < m_Max) /* 未满,插入 */
{
for(int i=m_Size-1; i>=nIndex; --i)
m_Data[i+1] = m_Data[i];
m_Data[nIndex] = dValue;
}
else /* 重新分配空间 */
{
m_Max = m_Max*2;
double* temp = new double[m_Max];
memcpy(temp, m_Data, m_Size*sizeof(double));
delete [] m_Data;
m_Data = temp;
for(int i=m_Size-1; i>=nIndex; --i)
m_Data[i+1] = m_Data[i];
m_Data[nIndex] = dValue;
}
++m_Size; /* 数组大小加1 */
}
// 重载下标运算符[]
double DArray::operator[](int nIndex) const
{
if(nIndex<0 || nIndex>=m_Size)
{
cout << "Error: the index in [] is invalid!" << endl;
exit(0);
}
return m_Data[nIndex];
}
class String{
friend ostream& operator<< (ostream&,String&); //重载<<运算符
friend istream& operator>> (istream&,String&); //重载>>运算符
public:
String(); // 默认构造函数
String(const char* str); // 带参构造函数
String(const String& rhs); // 拷贝构造函数
String& operator=(const String& rhs); // 拷贝赋值运算符
String operator+(const String& rhs) const; //operator+
bool operator==(const String&); //operator==
bool operator!=(const String&); //operator!=
char& operator[](unsigned int); //operator[]
size_t size() const;
const char* c_str() const;
~String(); // 析构函数
private:
char *m_data; // 用于保存字符串
};
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有