list1015.cpp

来自「teach yourself C++ in 21 days 第五版」· C++ 代码 · 共 54 行

CPP
54
字号
// Listing 10.15 - Copy constructors

#include <iostream>

using namespace std;

class Cat
{
  public:
    Cat();              // default constructor
    // copy constructor and destructor elided!
    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 << "Whiskers' age: " << Whiskers.GetAge() << endl;
   cout << "copying Frisky to Whiskers...\n";
   Whiskers = Frisky;
   cout << "Whiskers' age: " << Whiskers.GetAge() << endl;
   return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?