⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list0911.cpp

📁 teach yourself C++ in 21 days 第五版
💻 CPP
字号:
//Listing 9.11 - Passing pointers to objects
  
#include <iostream>
  
using namespace std;
class SimpleCat
{
   public:
     SimpleCat();
     SimpleCat(SimpleCat&);
     ~SimpleCat();
  
     int GetAge() const { return itsAge; }
     void SetAge(int age) { itsAge = age; }
  
   private:
     int itsAge;
};
  
SimpleCat::SimpleCat()
{
    cout << "Simple Cat Constructor..." << endl;
    itsAge = 1;
}
  
SimpleCat::SimpleCat(SimpleCat&)
{
     cout << "Simple Cat Copy Constructor..." << endl;
}
  
SimpleCat::~SimpleCat()
{
    cout << "Simple Cat Destructor..." << endl;
}
  
const SimpleCat * const FunctionTwo 
    (const SimpleCat * const theCat);
  
int main()
{
    cout << "Making a cat..." << endl;
    SimpleCat Frisky;
    cout << "Frisky is " ;
    cout << Frisky.GetAge();
    cout << " years old" << endl;
    int age = 5;
    Frisky.SetAge(age);
    cout << "Frisky is " ;
    cout << Frisky.GetAge();
    cout << " years old" << endl;
    cout << "Calling FunctionTwo..." << endl;
    FunctionTwo(&Frisky);
    cout << "Frisky is " ;
    cout << Frisky.GetAge();
    cout << " years old" << endl;
    return 0;
}
  
// functionTwo, passes a const pointer
const SimpleCat * const FunctionTwo 
    (const SimpleCat * const theCat)
{
    cout << "Function Two. Returning..." << endl;
    cout << "Frisky is now " << theCat->GetAge();
    cout << " years old " << endl;
    // theCat->SetAge(8);   const!
    return theCat;
}

⌨️ 快捷键说明

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