大家能否帮我把这个关于赋值运算符的程序填写完整???
以下这个程序是我从书上抄来的,为了节省内存取消了复制构造函数和析构函数,
因为我是一个初学者,所以我很想看看完整的程序什么样子的,还烦请大家帮我
把复制构造函数和析构函数添上去,谢谢!!!
#include <iostream>
using namespace std;
class CAT
{
public:
CAT();
int GetAge() const { return *itsAge;}
int GetWeight() const { return *itsWeight;}
void SetAge(int age) { *itsAge=age;}
CAT &operator=(const CAT &);
private:
int *itsAge;
int *itsWeight;
};
CAT::CAT()
{
itsAge=new int;
itsWeight=new int;
*itsAge=5;
*itsWeight=9;
}
CAT &CAT::operator=(const CAT & rhs)
{
if(this==&rhs)
return *this;
*itsAge=rhs.GetAge();
*itsWeight=rhs.GetWeight();
return *this;
}
int main()
{
CAT frisky;
cout<<"frisky's age: "<<frisky.GetAge()<<endl;
cout<<"Setting frisky to 6...\n";
frisky.SetAge(6);
CAT whiskers;
cout<<"whisker' age: "<<whiskers.GetAge()<<endl;
cout<<"copying frisky to whiskers...\n";
whiskers=frisky;
cout<<"whiskers' age: "<<whiskers.GetAge()<<endl;
return 0;
}