class A
{
protected:
int y;
private:
int z;
public:
int x;
A(int a, int b, int c):x(a), y(b), z(c) // constructor but that colon and x(a), y(b). what does this do
{
}
void display()
{
cout << "x is " << x << endl;
cout << "y is " << y << endl;
cout << "z is " << z << endl;
}
};
class B : public A
{
private:
int num;
public:
B(int x, int y, int z, int extra): A(x, y, z), num(extra) // same here its a constructor but what does the stuff after the colon do?
{
}
void display()
{
cout << "x is " << x << endl;
cout << "y is " << y << endl;
}
};
Classes get their member variables initialised before the constructor starts (the opening brace), either by the member initializer list or default initialisation. When one has assignment statements in the constructor body, they get re-initialised - which is a little inefficient.