what does " : y(0)" mean?
When an object is created, all of the data members must be created (and initialized) before you get to the opening brace of the constructor. The colon is the start of the
initialization list. Here you can call constructors for your data members. In this case, a value of zero is to be placed into the y member when the y member is created.
y is a pointer to a double but as yet there is no double to point to so y has been initialized to zero.
Here's another example. A class Person that has a class Date as a member:
-
class Date
-
{
-
public:
-
Date(int m, int d, int y);
-
};
-
class Person
-
{
-
private:
-
Date birthdate
-
string name;
-
public:
-
Person(string n, int month, int day, int year);
-
};
-
When you create a Person object you want to initialize the Date member at the time the Date member is created. To do that yout need to call the Date constructor from the Person constructor and you need to initialize the string member so the string constructor needs to be called also:
-
int main()
-
{
-
Person p("John", 10, 31, 1975);
-
}
-
-
//where the Person constructor is:
-
-
Person::Person(string n, int month, int day, int year) : birthdate(month, day, year), name(n)
-
{
-
-
}
-
}