继承(一)
向基类构造函数传递变量:
#include <iostream>
using namespace std;
enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };
class Mammal
{
public:
// constructors
Mammal();
Mammal(int age);
~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 function members
void Speak() const { cout << "Mammal sound!\n"; }
void Sleep() const { cout << "Shhh. I'm sleeping. \n"; }
protected:
int itsAge;
int itsWeight;
};
class Dog : public Mammal
{
public:
// constructors
Dog();
Dog(int age);
Dog(int age, int weight);
Dog(int age, BREED breed);
Dog(int age, int weight, BREED breed);
~Dog();
// accessors
BREED GetBreed() const { return itsBreed; }
void SetBreed(BREED breed) { itsBreed = breed; }
// other function members
void WagTail() { cout << "Tail wagging\n"; }
void BegForFood() { cout << "Begging for food\n"; }
private:
BREED itsBreed;
};
///////////////////////////////////////////////////////////////
// implementation
Mammal::Mammal():
itsAge(1),
itsWeight(5) {
cout << "Mammal constructor\n";
}
Mammal::Mammal(int age):
itsAge(age),
itsWeight(5) {
cout << "Mammal(int) constructor\n";
}
Mammal::~Mammal() {
cout << "Mammal destructor\n";
}
Dog::Dog():
Mammal(),
itsBreed(YORKIE) {
cout << "Dog constructor\n";
}
Dog::Dog(int age):
Mammal(age),
itsBreed(YORKIE) {
cout << "Dog(int) constructor\n";
}
Dog::Dog(int age, int weight):
Mammal(age),
itsBreed(YORKIE) {
itsWeight = weight; // ?
cout << "Dog(int, int) constructor\n";
}
Dog::Dog(int age, int weight, BREED breed):
Mammal(age),
itsBreed(breed) {
itsWeight = weight;
cout << "Dog(int, int, BREED) constructor\n";
}
Dog::Dog(int age, BREED breed):
Mammal(age),
itsBreed(breed) {
cout << "Dog(int, BREED) constructor\n";
}
Dog::~Dog() {
cout << "Dog destructor\n";
}
int main(int argc, char *argv[])
{
Dog fido;
Dog rover(5);
Dog buster(6, 8);
Dog yorkie(3, YORKIE);
Dog dobbie(4, 20, DOBERMAN);
fido.Speak();
rover.WagTail();
cout << "Yorkie is " << yorkie.GetAge() << " years old\n";
cout << "Dobbie is " << dobbie.GetWeight() << " years old\n";
return 0;
}
using namespace std;
enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };
class Mammal
{
public:
// constructors
Mammal();
Mammal(int age);
~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 function members
void Speak() const { cout << "Mammal sound!\n"; }
void Sleep() const { cout << "Shhh. I'm sleeping. \n"; }
protected:
int itsAge;
int itsWeight;
};
class Dog : public Mammal
{
public:
// constructors
Dog();
Dog(int age);
Dog(int age, int weight);
Dog(int age, BREED breed);
Dog(int age, int weight, BREED breed);
~Dog();
// accessors
BREED GetBreed() const { return itsBreed; }
void SetBreed(BREED breed) { itsBreed = breed; }
// other function members
void WagTail() { cout << "Tail wagging\n"; }
void BegForFood() { cout << "Begging for food\n"; }
private:
BREED itsBreed;
};
///////////////////////////////////////////////////////////////
// implementation
Mammal::Mammal():
itsAge(1),
itsWeight(5) {
cout << "Mammal constructor\n";
}
Mammal::Mammal(int age):
itsAge(age),
itsWeight(5) {
cout << "Mammal(int) constructor\n";
}
Mammal::~Mammal() {
cout << "Mammal destructor\n";
}
Dog::Dog():
Mammal(),
itsBreed(YORKIE) {
cout << "Dog constructor\n";
}
Dog::Dog(int age):
Mammal(age),
itsBreed(YORKIE) {
cout << "Dog(int) constructor\n";
}
Dog::Dog(int age, int weight):
Mammal(age),
itsBreed(YORKIE) {
itsWeight = weight; // ?
cout << "Dog(int, int) constructor\n";
}
Dog::Dog(int age, int weight, BREED breed):
Mammal(age),
itsBreed(breed) {
itsWeight = weight;
cout << "Dog(int, int, BREED) constructor\n";
}
Dog::Dog(int age, BREED breed):
Mammal(age),
itsBreed(breed) {
cout << "Dog(int, BREED) constructor\n";
}
Dog::~Dog() {
cout << "Dog destructor\n";
}
int main(int argc, char *argv[])
{
Dog fido;
Dog rover(5);
Dog buster(6, 8);
Dog yorkie(3, YORKIE);
Dog dobbie(4, 20, DOBERMAN);
fido.Speak();
rover.WagTail();
cout << "Yorkie is " << yorkie.GetAge() << " years old\n";
cout << "Dobbie is " << dobbie.GetWeight() << " years old\n";
return 0;
}