关于C++的一些基础知识,构造函数,赋值构造函数,运算符重载,析构函数,类模板等
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
using std::strlen;
class CBox
{
public:
//析构函数
~CBox(){}
//类的静态数据成员,类的静态数据成员不能在类中进行初始化,通常定义这个是为了统计对象的数量
static int objectCnt;
//构造函数
explicit CBox(double lv = 1.0 , double bv = 1.0 , double hv = 1.0):m_Length(lv) , m_Width(bv) , m_Height(hv)
{
//统计类对象的个数
objectCnt++;
}
//获取私有成员的成员函数
inline double GetLength()
{
return m_Length;
}
inline double SetLength(double length)
{
this->m_Length = length;
}
inline double GetWidth()
{
return m_Width;
}
inline double SetWidth( double width)
{
this->m_Width = width;
}
inline double GetHeight()
{
return m_Height;
}
inline double SetHeight(double height)
{
this->m_Height = height;
}
//volume成员函数
inline double volume() const
{
return m_Length*m_Width*m_Height;
}
//只能对类的成员函数指定const类型,对于普通的函数不能这么做
//对类的成员函数指定const类型,是为了使该函数中的this指针成为const
//compare 成员函数
//当前对象的体积大时返回真,否则返回假
// bool compare( const CBox &xBox) const
// {
// //此时该函数内的this指针是const,const成员函数不能调用同一个类中非const的成员函数
// //所以该类中的volume也必须是const类型
// //当某个对象声明为const类型后,该对象调用的成员函数必须也是const类型
// return this->volume() > xBox.volume();
// }
//此处还可以运用运算符重载来比较两个对象的体积
bool operator>(const CBox & aBox) const;
CBox operator + (const CBox &aBox) const;
//私有成员
private:
double m_Length;
double m_Width;
double m_Height;
//友元函数
friend double BoxSurface(CBox aBox);
};
//必须在类定义的外面对类的静态数据成员进行初始化
//必须添加类作用域符号 , 不用再写static关键字
//用来统计类对象的数量
int CBox::objectCnt(0);
//友元函数的实现,不用加类作用域符号
double BoxSurface(CBox aBox)
{
return 3.0*aBox.m_Height*aBox.m_Length*aBox.m_Width;
}
//实现成员函数
bool CBox::operator>(const CBox &aBox) const
{
return this->volume() > aBox.volume();
}
CBox CBox::operator+(const CBox &aBox) const
{
return CBox(this->m_Length > aBox.m_Length?this->m_Length:aBox.m_Length , this->m_Height >
aBox.m_Height? this->m_Height : aBox.m_Height , this->m_Width > aBox.m_Width? this->m_Width:aBox.m_Width);
}
//Messgae 类
class CMessage
{
public:
//析构函数
~CMessage();
//显示消息的成员函数
void showInfo() const
{
cout << endl << pmessage;
}
//构造函数,默认字符指针指向"Default Message"
CMessage(const char *text = "Default Message")
{
cout <<"CMessage开始调用构造函数啦"<< endl;
//如果不是默认的,则动态分配空间
pmessage = new char[strlen(text) + 1];
//将构造函数中形参text复制到pmessage中去
//将第三个参数中的值复制到第一个参数中去,第二个参数是长度
strcpy_s(pmessage , strlen(text) + 1 , text);
}
//复制构造函数,参数必须是const 引用的形式
CMessage(const CMessage & aMess)
{
cout <<"CMessage开始调用复制构造函数啦"<< endl;
size_t len = strlen(aMess.pmessage) + 1;
pmessage = new char[len];
strcpy_s(pmessage , len , aMess.pmessage);
}
//对赋值操作符进行重载
CMessage& operator = (const CMessage &aMess);
private:
//定义一个字符指针
char *pmessage;
};
//CMessage析构函数的实现
CMessage::~CMessage()
{
cout <<"CMessage开始调用析构函数啦"<< endl;
delete [] pmessage;
}
//对操作符重载成员函数的实现,返回引用
CMessage& CMessage::operator = (const CMessage &aMess)
{
cout << "CMessage开始调用赋值操作符啦"<<endl;
//检查赋值号右边的对象的地址是否和当前对象也就是左边的地址是否相同,如果相同直接返回
if(&aMess == this)
return *this;
//删除当前第一个对象分配的pmessage内存
delete [] this->pmessage;
pmessage = new char[strlen(aMess.pmessage) +1];
strcpy_s(pmessage , strlen(aMess.pmessage) +1, aMess.pmessage);
return *this;
}
//类模板例子
template<class T> class CSample
{
public:
//构造函数
CSample(const T values[] , int count);
CSample(const T &value);
CSample(){m_Free = 0;}
//成员函数
bool Add(const T &value) const;
T max() const;
private:
T m_Value[100];
int m_Free;
};
//成员函数的实现
//构造函数实现
template<class T> CSample<T> ::CSample(const T values[] , int count)
{
if(count >100)
count = 100;
m_Free = count;
cout << "CSample(const T values[] , int count) 构造 函数中的m_Free = " << m_Free <<endl;
for(int i=0 ; i<m_Free; ++i)
{
m_Value[i] = values[i];
}
}
template<class T> CSample<T> :: CSample(const T &value)
{
m_Value[0] = value;
m_Free = 1;
}
template<class T> bool CSample<T> :: Add(const T &value) const
{
Bool OK;
if( m_Free < 100)
OK = true;
else
OK = false;
if(OK)
{
m_Value[m_Free++] = value;
}
return OK;
}
template<class T> T CSample<T> :: max() const
{
T theMax = m_Value[0];
cout << "max() 函数中的m_Free = " << m_Free <<endl;
for(int i=0; i<m_Free; ++i)
{
if(m_Value[i] > theMax)
theMax = m_Value[i];
}
return theMax;
}
//主函数
int main()
{
CBox match;
cout << BoxSurface(match) << endl;
CMessage moto1("A miss is as good as a little");
CMessage moto2;
moto2 = moto1;
CMessage *pM(new CMessage("A cat can look at a queen"));
moto1.showInfo();
moto2.showInfo();
pM->showInfo();
cout << endl;
delete pM;
//定义类对象数组
CBox boxes[] = {
CBox(8.0,5.0,2.0),
CBox(5.0,4.0,6.0),
CBox(4.0,3.0,3.0)
};
CSample<CBox> myboxes(boxes , sizeof boxes / sizeof CBox);
CBox maxbox = myboxes.max();
cout << maxbox.volume() << endl;
cout << maxbox.objectCnt << endl;
system("pause");
return 0;
}