Sign in to follow this  
Kenny77

Simple Inheritance Question

Recommended Posts

I've been reading the book Sams Teach Yourself Cpp In An Hour A Day, and so far it has been amazing, but there's one part in the chapter on inheritance that it doesn't fully explain.
// Using a derived object
#include <iostream>
using std::cout;
using std::cin; using std::endl;

enum BREED { GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };

class Mammal
{
    public:
        // Constructors
        Mammal() : itsAge(2), itsWeight(5) {}
        ~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!" << endl; }
        void Sleep() const { cout << "Shh. I'm sleeping." << endl; }
        
    protected:
        int itsAge;
        int itsWeight;
};

class Dog : public Mammal
{
    public:
    
        // Constructors
        Dog() : itsBreed(GOLDEN) {}
        ~Dog() {}
        
        // Accessors
        BREED GetBreed() const { return itsBreed; }
        void SetBreed(BREED breed) { itsBreed = breed; }
        
        // Other Methods
        void WagTail() const { cout << "Tail wagging..." << endl; }
        void BegForFood() const { cout << "Begging for food..." << endl; }
        
    private:
         BREED itsBreed;       
};

int main()
{
  Dog Fido;
  Fido.Speak();
  Fido.WagTail();
  cout << "Fido is " << Fido.GetAge() << " years old." << endl;
  system("PAUSE"); return 0;
}

What's going on with the constructors in the Mammal and Dog class (the ': itsBreed(Golden)')? Is there a name for this function/operator?

Share this post


Link to post
Share on other sites
Those are initialization lists.

For now, you can think of them as working like this:


Mammal() : itsAge(2), itsWeight(5) {}

// "Essentially" does this:
Mammal()
{
itsAge = 2;
itsWeight = 5;
}




However, that's not fully accurate and there's certain things you can do with them that you can't do when assigning the values inside the constructor definition.

Share this post


Link to post
Share on other sites
When declaring a variable, like a int or whatever, you normally declare it like this:
int myInt;
However, int's can also be declared like this:
int myInt(5);
Which uses the int's constructor to set the int to the value of 5.

In classes, like the class 'Dog', you have constructors and destructors. These are like functions, but they get called at the time the instance of your class is created, and the at the time it's destroyed or goes out of scope.
Dog() //constructor
~Dog() //destructor
Constructors can have parameters, like regular functions.
int myInt(5); //Using the int's constructor, and passing in a parameter of '5'.
Now, in your example, 'itsBreed' is of type 'BREED' which is a enum.
itsBreed(GOLDEN); //Using the constructor of 'BREED' and passing in a parameter of 'GOLDEN'

Now, with class constructors, there's this thing called an 'initialization list'.
They look like this:
constructor(parameters) : memberVariable(parameters), memberVariable(parameters)
Look at 'Mammal' in your example:
Mammal() : itsAge(2), itsWeight(5) {}
This initializes or constructs itsAge to 2, and itsWeight to 5, before the Mammal() constructor even runs.
In this particular case, it's sort of the same as going like this:
Mammal()
{
itsAge = 2;
itsWeight = 5;
}
But it does it before the Mammal() constructor function is ran.
If I have this:
Mammal() : myInt(123)
{
myInt = 456;
}
myInt would first be constructed and set to 123, then Mammal's constructor would run, and set myInt to 456.

Why is this important? Well, let's pretend Mammal takes a parameter in it's constructor. If Dog inherits Mammal, the only way we can set Mammal's constructor parameter, is by using the initialization list.
Dog() : Mammal(myParameter)
Since Dog inherits from Mammal, Mammal's constructor runs first. We can use the initialization list to manually choose how to construct Mammal, before Dog is constructed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this