?? copyconstructors.cpp
字號:
// Listing 13.3
// Copy constructors
#include <iostream>
class CAT
{
public:
CAT(); // default constructor
CAT (const CAT &); // copy constructor
~CAT(); // destructor
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;
}
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...\n";
frisky.SetAge(7);
std::cout << "frisky's age: " << frisky.GetAge() << "\n";
std::cout << "boot's age: " << boots.GetAge() << "\n";
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -