heapaccess.cpp
来自「《24学时精通c++》的光盘内容」· C++ 代码 · 共 28 行
CPP
28 行
// 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;
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 + -
显示快捷键?