1. The base class constructor is responsible for initializing inherited data members. The derived class constructor is mainly used to initialize new data members.
2. C ++ requires that the reference and pointer type match the value assignment type, but this rule is exceptional for inheritance. However, this exception is only one-way. You cannot assign the base class object and address to the reference and pointer of the derived class.
Class TableTennisPlayer
{
Private:
....
Public:
....
}
Class RatedPlayer: public TableTennisPlayer
{
Private:
...
Public:
...
}
Yes:
RatedPlayer rplayer (1140, "Malloy", "Duck ");
TableTennisPlayer & rp = rplayer;
Or:
TableTennisPlayer * rp = & rplayer;
No:
TableTennisPlay tplayer ("", "", true );
RatedPlayer & rp = tplayer;
RatedPlayer * rp = tplayer;
3. Member initialization list
Example: (separate the initialization list with commas (,) with a colon)
Queue: Queue (int qs): qsize (qs), front (NULL), real (NULL), item (0)
{
}
Note:
1. Only constructors can use this initialization list syntax.
2. This syntax must be used for const class members. In the above example, we previously defined the const int qsize in the private member of the Queue class;
3. This syntax must be used for class members referenced by the declarative statement:
Class Agency {...};
Class Agent
{
Private:
Agency & belong; // The reference type is Agency,
}
Cpp :( initialize the constructor)
Agent: Agent (Agency & a): belong (a); // initialize belong as a and use the member initialization list.