Constructor in a base class, what exactly does this syntax mean?

Thanks for looking over this snipit of code I can't figure this out, on line ten is the constructor for the Fish class and it takes a bool as a parameter, my question is what the second half of the constructor is after the ":". Yet again thanks for the help.

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
58
#include <iostream>

class Fish
{
private:
    bool FreshWaterFish;

public:
    //Fish constructor
    Fish(bool IsFreshWater) : FreshWaterFish(IsFreshWater){}

    void Swim()
    {
        if(FreshWaterFish)
            std::cout << "Swims in lake" << std::endl;
        else
            std::cout << "Swims in sea" << std::endl;
    }
};

class Tuna: public Fish
{
public:
    Tuna(): Fish(false) {}

    void Swim()
    {
        std::cout << "Tuna swims real fast" << std::endl;
    }
};

class Carp: public Fish
{
public:
    Carp(): Fish(false) {}

    void Swim()
    {
        std::cout << "Carp swims real slow" << std::endl;
    }
};

int main()
{
    Carp myLunch;
    Tuna myDinner;

    std::cout << "Getting my food to swim" << std::endl;

    std::cout << "Lunch: ";
    myLunch.Swim();

    std::cout << "Dinner: ";
    myDinner.Fish::Swim();

    return 0;
}
It is an initialization list. Here is an article on it, but a quick Google should help you.
http://www.cprogramming.com/tutorial/initialization-lists-c++.html
To access the function of the base class use this BaseClass::Function(), like so:
1
2
3
4
5
6
7
8
9
10
11
class Tuna: public Fish
{
public:
    Tuna(): Fish(false) {}

    void Swim()
    {
        std::cout << "Tuna swims real fast" << std::endl;
        Fish::Swim(); // Note: this will show the message from the base class
    }
};
Thanks, I'm still not understanding completely even after reading that article,
: FreshWaterFish(IsFreshWater){} still trounces me in my endeavors I just don't understand what it does because when I "//" that second half of line ten it still will compile and run the code just the same....any help?
: FreshWaterFish(IsFreshWater){}
-> it sets FreshWaterFish to the value of IsFreshWater

if you comment out the initialization FreshWaterFish has an undefined value.
It's just not predictable what the value of FreshWaterFish has, but otherwise there are no problems
Hey thanks a lot mate, that little part has been troubling me for a god bit.
Topic archived. No new replies allowed.