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

📄 hour12_3b.cpp

📁 《24学时精通c++》的随书源码的下半部分。欢迎下载学习。
💻 CPP
字号:
 //Listing 12.3

 // Passing references to objects
 // was passreftoobj.cpp 

 #include <iostream>

 

 class SimpleCat

 {

 public:

     SimpleCat();

     SimpleCat(SimpleCat&);

     ~SimpleCat();

 

     int GetAge() const { return itsAge; }

     void SetAge(int age) { itsAge = age; }

 

 private:

     int itsAge;

 };

 

 SimpleCat::SimpleCat()

 {

     std::cout << "Simple Cat Constructor...\n";

     itsAge = 1;

 }

 

 SimpleCat::SimpleCat(SimpleCat&)

 {

     std::cout << "Simple Cat Copy Constructor...\n";

 }

 

 SimpleCat::~SimpleCat()

 {

     std::cout << "Simple Cat Destructor...\n";

 }

 

 const SimpleCat & FunctionTwo (const SimpleCat & theCat);

 

 int main()

 {

     std::cout << "Making a cat...\n";

     SimpleCat Frisky;

     std::cout << "Frisky is " << Frisky.GetAge() 

               << " years old\n";

 

     int age = 5;

     Frisky.SetAge(age);

     std::cout << "Frisky is " << Frisky.GetAge() 

               << " years old\n";

 

     std::cout << "Calling FunctionTwo...\n";
     std::cout << "address of Frisky " << &Frisky << "\n";

     FunctionTwo(Frisky);

     std::cout << "Frisky is " << Frisky.GetAge() 

               << " years old\n";

     return 0;

 }

 

 // functionTwo passes a ref to a const object

 const SimpleCat & FunctionTwo (const SimpleCat & theCat)

 {

     std::cout << "Function Two. Returning...\n";

     std::cout << "Frisky is now " << theCat.GetAge()

               << " years old \n";

     // theCat.SetAge(8);   const!
     std::cout << "address of Frisky in Function Two" << &theCat << "\n";

     return theCat;

 }

⌨️ 快捷键说明

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