📄 list0911.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 + -