hour10_3.cpp

来自「《24学时精通c++》的随书源码的下半部分。欢迎下载学习。」· C++ 代码 · 共 33 行

CPP
33
字号
 // Listing 10.2
 // Accessing members of objects on the heap
 #include <iostream>
 using std::endl;
 class SimpleCat
 {
 public:
     SimpleCat() {itsAge = 2; }
     ~SimpleCat() {}
     int GetAge() const { return itsAge; }
     void SetAge(int age) { itsAge = age; }
 private:
     int itsAge;
 };

 int main()
 {
     SimpleCat * Frisky = new SimpleCat;

   // -> below was replaced with (*  ).
   // To me, the points-to operator ( -> ) is clearly easier
   // to read and therefore more meaningful.
   
     std::cout << "Frisky is " << (*Frisky).GetAge()
               << " years old" << endl;

     (*Frisky).SetAge(5);
     std::cout << "Frisky is " << (*Frisky).GetAge()
               << " years old" << endl;

     delete Frisky;
     return 0;
 }

⌨️ 快捷键说明

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