Calling on Base constructors..help please!!

This is a discussion on Calling on Base constructors..help please!! within the C++ Programming forums, part of the General Programming Boards category; Code: Dog::Dog(int age, int weight): Mammal(age), its Breed (GOLDEN) Essentially what is this code here doing?...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Talking Calling on Base constructors..help please!!

    Code:
    Dog::Dog(int age, int weight):
    Mammal(age),
    its Breed (GOLDEN)
    Essentially what is this code here doing?
    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.

  2. #2
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    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 09: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.

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    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.

  4. #4
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    ???
    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.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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 05:17 AM.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    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.

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Code:
    Dog::Dog(int age, int weight):
    Mammal(age),
    itsBreed(GOLDEN)
    {
    	itsWeight = weight;
    	cout << "Dog(int, int) constructor...\n";
    }
    But my question is after this is called

    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?

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    >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.

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Originally posted by elchulo2002
    Code:
    Dog::Dog(int age, int weight):
    Mammal(age),
    itsBreed(GOLDEN)
    {
    	itsWeight = weight;
    	cout << "Dog(int, int) constructor...\n";
    }
    But my question is after this is called

    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?
    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.

    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.

  10. #10
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    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.

  11. #11
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  2. Calling virtuals inside constructors
    By cloudy in forum C++ Programming
    Replies: 10
    Last Post: 05-07-2009, 04:52 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Calling Base Class Operators
    By pianorain in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2005, 09:53 AM
  5. Replies: 4
    Last Post: 12-28-2002, 11:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21