#include <iostream>
using namespace std;
//class String
//{
//public:
// String(const char *str = NULL);
// 普通构造函数 // String(const String &other);
// 拷贝构造函数
// ~ String(void); // 析构函数
// String & operate =(const String &other); // 赋值函数
//private:
// char *m_data;
// 用于保存字符串
//};
class NumCal
{
public:~NumCal();// 析构函数
NumCal();// 普通构造函数
NumCal(const NumCal &temp);// 拷贝构造函数
NumCal &operator=(const NumCal &temp); // 赋值函数
NumCal &operator+(const NumCal &temp);
NumCal &operator-(const NumCal &temp);
NumCal &operator+=(const NumCal &temp);
NumCal &operator-=(const NumCal &temp);
public:
int i;
int j;
};
NumCal::~NumCal()
{
i=0;
j=0;
}
NumCal::NumCal()//构造函数
{
i=0;
j=0;
}
NumCal::NumCal(const NumCal &temp)//赋值构造函数
{
i=temp.i;
j=temp.j;
}
NumCal &NumCal::operator+(const NumCal &temp)
{
i=i+temp.i;
j=j+temp.j;
return *this;
}
NumCal &NumCal::operator-(const NumCal &temp)
{
i=i-temp.i;
j=j-temp.j;
return *this;
}
NumCal &NumCal::operator=(const NumCal &temp)
{
i=temp.i;
j=temp.j;
return *this;
}
NumCal &NumCal::operator+=(const NumCal &temp)
{
i=i+temp.i;
j=j+temp.j;
return *this;
}
NumCal &NumCal::operator-=(const NumCal &temp)
{
i=i-temp.i;
j=j-temp.j;
return *this;
}
int main()
{
NumCal a,b,c;
a.i=3;
a.j=4;
b.i=6;
b.j=6;
a+=b;
cout<<a.i<<a.j<<endl;
}
类的构造、析构、赋值构造、拷贝构造函数
最新推荐文章于 2021-08-24 23:33:26 发布