What i don't get?

There are a few things i don't understand
Can someone help me.

I Want to know what this line of code doing - Mammal():itsAge(2), itsWeight(5){}
I know Mammal is a construtor and want it does the next bit is fuzzy :itsAge(2), itsWeight(5){}
I know what the : derived means but not what this code is doing at the end

I mean this code :itsAge(2), itsWeight // not the full code down there

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>

using namespace std;

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

    class Mammal
    {
        public:
        // construtors
        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!\n"; }
        void Sleep() const { cout << "Shhh. I'm sleeping.\n"; }
        
        protected:
            int itsAge;
            int itsWeight;
    };
    
    class Dog : public Mammal
    {
        public:
            // construtors
            Dog():itsBreed(GOLDEN){}
            ~Dog(){}
            
            // accessor
            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;
    };

int main()
{
    Dog Fido;
    Fido.Speak();
    Fido.WagTail();
    cout << "Fodo is " << Fido.GetAge() << " Years old" << endl;
    char response;
    cin >> response;
    return 0;
}


Last edited on
the second question.
I unstand this clearly

int somevar = 0;
int & rRef = somevar;

But not how it works with functions
int & myFunction()

Does this act like a referance for a function or return the value by referance.
Question 1:
Mammal():itsAge(2), itsWeight(5){}
That is an initializer list, when calling the 'Mammal' constructor the member 'itsAge' is initialized with value '2' and 'itsWeight' with '5' as value.

Topic archived. No new replies allowed.