📄 copyconstructors.cpp
字号:
#include <iostream>
class CAT
{
public:
CAT();
CAT(const CAT&);
~CAT();
int GetAge() const { return *itsAge;}
int GetWeight() const {return *itsWeight;}
void SetAge(int age) {*itsAge = age;}
private:
int *itsAge;
int *itsWeight;
};
CAT::CAT()
{
itsAge = new int;
itsWeight = new int;
*itsAge = 5 ;
*itsWeight = 9 ;
}
CAT::CAT(const CAT &rhs)
{
itsAge = new int;
itsWeight = new int;
*itsAge = rhs.GetAge() ;
*itsWeight = rhs.GetWeight() ;
}
CAT::~CAT()
{
delete itsAge;
itsAge = 0 ;
delete itsWeight;
itsWeight = 0 ;
}
/*CAT::~CAT()
{}*/
int main()
{
CAT frisky;
std::cout << "frisky's age: " << frisky.GetAge() << "\n";
std::cout << "Setting frisky to 6...\n";
frisky.SetAge(6);
std::cout << "Creating boots from frisky\n";
CAT boots(frisky);
std::cout << "frisky's age: " << frisky.GetAge() << "\n";
std::cout << "boots' age:" << boots.GetAge() << "\n";
std::cout << "Setting frisky to 7...";
frisky.SetAge(7);
std::cout << "frisky's age: " << frisky.GetAge() << "\n";
std::cout << "boots' age:" << boots.GetAge() << "\n";
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -