Initialization of composition and inheritance in C + +

Source: Internet
Author: User

In C + +, the initialization of a single class is simple enough to skip the word.

When a class inherits another class, or there is a class in a class

Class a{

、、、

};

Class B

{

、、、

};

Class c:classa{

Private

B BB;

int x

Public

C (int II): B (ii), BB (ii), X (ii) {};

};

At this point we initialize the settings using C (int II): B (ii), BB (ii), X (ii) There are a few things that must be said clearly:

1. The parameter is passed by C (INT ii) to the class or variable after the colon.

2. The class or variable following the colon is separated by commas

The parameters in 3.C (int II) must be the same as after the colon. Here is the II

4. The inherited Class B (ii) and in B (ii) must have a definition in class B

5. The containing class is defined by the class object, where the class defines the object is BB so it is BB (ii) but must also have a B (int) definition in b

Specific examples are as follows:

#include <iostream>using namespace Std;class x{private:int i;public:          X (int II): I (ii) {        };  void print ()           {           cout<<i<<endl;                      }}; class y{      private:          int A;          Public:          Y (int II): A (ii) {        };          void print ()           {           cout<<a<<endl;                      }           };  Class Z:public y{      private:          int i; X xx;         Public:          Z (int II): Y (ii), XX (ii), I (ii) {        };             void print ()           {           cout<<i<<endl;           Y::p rint ();           Xx.print ();           }};  int main () {Z zzz; Zzz.print (); return 0;}


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.