Base class and derived class

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.