Essentially what is this code here doing?Code:Dog::Dog(int age, int weight): Mammal(age), its Breed (GOLDEN)
Essentially what is this code here doing?Code:Dog::Dog(int age, int weight): Mammal(age), its Breed (GOLDEN)
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
Shall I show the whole source code for better understanding? I guess so.
EDIT
Code://Listing 12.4 Overloading constructors in derived classes #include <iostream> enum BREED { GOLDEN, 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 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: // 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 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 destructor...\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...\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() { Dog fido; Dog rover(5); Dog buster(6,8); Dog yorkie (3,GOLDEN); Dog dobbie (4,20,DOBERMAN); fido.Speak(); rover.WagTail(); cout << "Yorkie is " << yorkie.GetAge() << " years old\n"; cout << "Dobbie weighs "; cout << dobbie.GetWeight() << " pounds\n"; int x; cin>>x; return 0; }
Last edited by incognito; 01-07-2002 at 10:57 PM.
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
So would Mammal's weigth be 20 pounds?
because of this
Code:Dog::Dog(int age, int weight): Mammal(age), itsBreed(GOLDEN) { itsWeight = weight; cout << "Dog(int, int) constructor...\n"; }
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
???
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
Recognise that itsAge & itsWeight are protected member variables of Mammal, so are inherited with the public inheritance between Dog and Mammal.
Essentally, Dog inherits these variables from Mammal, and so passes the values from its constructor to the Mammal's constructor to intialise the variables. Its good practice to do so because you are meeting the needs of the mammal (base) class as well as the Dog (derived) class with 1 function call from the program thats initialising the object.
Last edited by Fordy; 01-08-2002 at 06:17 AM.
Dog::Dog(int age, int weight, BREED breed):
Mammal(age),
itsBreed(breed)
{
itsWeight = weight;
cout << "Dog(int, int, BREED) constructor...\n";
}
Lets take this as an example.
The Dogs constructor gets three values, age, weight and breed.
It calls the base classes constructor with age. It initializes itsBreed with breed. Inside it sets itsWeight to weight.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
But my question is after this is calledCode:Dog::Dog(int age, int weight): Mammal(age), itsBreed(GOLDEN) { itsWeight = weight; cout << "Dog(int, int) constructor...\n"; }
Dog::Dog(int age, int weight):
Mammal(age),
itsBreed(GOLDEN)
and then this
{
itsWeight = weight;
cout << "Dog(int, int) constructor...\n";
}
Wouldn't this make Mammal's age 20?
>Wouldn't this make Mammal's age 20?
No. Mammal has no age. Only objects of type Mammal have an age. There is no generic age for all mammals. Each object of Mammal has it's own age. So setting one to 20 will not influence any other.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
It would make Mammal's age equal to whatever you pass the Dog constructor function, it age == 20, the age data member will become 20.Originally posted by elchulo2002
But my question is after this is calledCode:Dog::Dog(int age, int weight): Mammal(age), itsBreed(GOLDEN) { itsWeight = weight; cout << "Dog(int, int) constructor...\n"; }
Dog:og(int age, int weight):
Mammal(age),
itsBreed(GOLDEN)
and then this
{
itsWeight = weight;
cout << "Dog(int, int) constructor...\n";
}
Wouldn't this make Mammal's age 20?
When coding a constructor it is possible to initalize variables after the paramater list, and before the function body
ClassName::ClassName(int var1, char var2) : datamember1(var1), datamember2(var2)
{
}
Notice that : after the parameters, that signals that you are goin to initalize the variables outside the function.
That could also be written as
ClassName::ClassName(int var1, char var2)
{
datamember1 = var1;
datamember2 = var2;
}
Using : is good practice when you have a class (the subclass) inside a class, and want to pass the constructor of the sub class some parameters. It is also good when you have inheratance, so the constructor of the base class gets called, and can pass it whatever parameters you want to.
That's what I thought would happen Matty but it doesn't even the book says " This takes two parameters. Once again it initializes its bass class by calling the appropiate constructor, but this time it also assigns weight to its base class's variable its weight." But if it did happen after Dog dobbie (4,20,DOBERMAN); After this was called and 4 would be passed to this Dog::Dog(int age, int weight, BREED breed):
Mammal(age),
itsBreed(breed)
{
itsWeight = weight;
cout << "Dog(int, int, BREED) constructor...\n";
}
which would assign 4 to the Mammals age, but it doesn't seem to work
This is the output I get.
OUTPUT:
Mammal constructor...
Dog constructor...
Mammal(int) constructor...
Dog(int) constructor...
Mammal(int) constructor...
Dog(int, int) constructor...
Mammal(int) constructor...
Dog(int, BREED) constructor...
Mammal(int) constructor...
Dog(int, int, BREED) constructor...
Mammal sound!
Tail wagging...
Mammal constructor...
Yorkie is 3 years old
Dobbie weighs 20 pounds
1 The mamal's age
nvoigt I don't really understand your explanation.
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.
Thanks guys I think I got it now.
There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!
"...The only real game I thank in the world is baseball..." --Babe Ruth
"Life is beautiful"-Don Corleone right before he died.
"The expert on anything was once a beginner" -Baseball poster I own.
Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.