#include <iostream>
using namespace std;
enum BREED {GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB};
class Mammal
{
public:
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 methods
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:
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 methods
void WagTail() const {cout << "Tail wagging...\n";}
void BegForFood() const {cout << "Begging for food...\n";}
private:
BREED itsBreed;
};
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 deconstructor...\n";
}
Dog::Dog():
Mammal(),
itsBreed(GOLDEN)
{
cout << "Dog constructor...\n";
}
Dog::Dog(int age):
Mammal(age),
itsBreed(GOLDEN)
{
cout << "Dog (int) constructor...\n";
}
Dog::Dog(int age, int weight):
Mammal(age),
itsBreed(GOLDEN)
{
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 called...\n";
}
Dog::Dog(int age, BREED breed):
Mammal(age),
itsBreed(breed)
{
cout << "Dog(int BREED) constructor called...\n";
}
Dog::~Dog()
{
cout << "Dog deconstructor...\n";
}
int main()
{
Mammal Mam1;
Dog Fido;
Dog Rover(5);
Dog Buster(6, 8);
Dog Yorkie (3, GOLDEN);
Dog Dobbie (4, 20, DOBERMAN);
Fido.Speak();
Fido.WagTail();
cout << "Mam1 is " << Mam1.GetAge() << " years old.\n";
cout << "Yorkie is " << Yorkie.GetAge() << " years old.\n";
cout << "Dobbie weighs " << Dobbie.GetWeight() << " pounds.\n";
return 0;
}
Inheritance/constructor question
Continuing to work my way through my C++ book and I have a question about the program below.
In all of the dog constructors there is the line "Mammal(age)" line. The Mammal(int) constructor is being called each time, but im not sure why its not getting itsAge also set to a new value each time due to passing in a new age with each new dog object creation.
Im sure I have something confused and if someone could point it out I would appreciate it. ;)
Why do you think it's not? The output looks correct, and when the two ages show up in the output they are different numbers as you would expect.
What confuses me is "Dog dobbie (4, 20, DOBERMAN);"
I figured the age for Mammal would end up being set to 4. I made the Mam1 object just to test that and output the age.
The output: Mam1 is 1 years old.
I figured the age for Mammal would end up being set to 4. I made the Mam1 object just to test that and output the age.
The output: Mam1 is 1 years old.
Mam1 is a separate object from dobbie. Creating different objects later won't change the age of the Mam1 object that you created first.
Remember, the classes are *type definitions*; the objects (class instances) are the variables.
Because a Dog ('derived class') IS-A Mammal ('base class'), the Dog structure includes a Mammal structure in its data (in order to support all its generic Mammal behaviour). The Dog definition does not include an 'age' member, because the Mammal base already provides one. By using that Mammal constructor in the initializer list, the Dog class specifies how that information should be set up, for the current Dog.
Because a Dog ('derived class') IS-A Mammal ('base class'), the Dog structure includes a Mammal structure in its data (in order to support all its generic Mammal behaviour). The Dog definition does not include an 'age' member, because the Mammal base already provides one. By using that Mammal constructor in the initializer list, the Dog class specifies how that information should be set up, for the current Dog.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement