?? conanddest.cpp
字號:
//Listing 16.3 Constructors and destructors called.
#include <iostream>
enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };
class Mammal
{
public:
// constructors
Mammal();
~Mammal();
//accessors
int GetAge() const { return itsAge; }
void SetAge(int age) { itsAge = age; }
int GetWeight() const { return itsWeight; }
void SetWeight(int weight) { itsWeight = weight; }
//Other methods
void Speak() const { std::cout << "Mammal sound!\n"; }
void Sleep() const { std::cout << "shhh. I'm sleeping.\n"; }
protected:
int itsAge;
int itsWeight;
};
class Dog : public Mammal
{
public:
// Constructors
Dog();
~Dog();
// Accessors
BREED GetBreed() const { return itsBreed; }
void SetBreed(BREED breed) { itsBreed = breed; }
// Other methods
void WagTail() { std::cout << "Tail wagging...\n"; }
void BegForFood() { std::cout << "Begging for food...\n"; }
private:
BREED itsBreed;
};
Mammal::Mammal():
itsAge(1),
itsWeight(5)
{
std::cout << "Mammal constructor...\n";
}
Mammal::~Mammal()
{
std::cout << "Mammal destructor...\n";
}
Dog::Dog():
itsBreed(YORKIE)
{
std::cout << "Dog constructor...\n";
}
Dog::~Dog()
{
std::cout << "Dog destructor...\n";
}
int main()
{
Dog fido; // create a dog
fido.Speak();
fido.WagTail();
std::cout << "Fido is " << fido.GetAge() << " years old\n";
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -