C + + inheritance of three

Source: Internet
Author: User

Multiple Inheritance "

A class has multiple inheritance relationships for direct base classes called multi-inheritance multi-inheritance declaration syntax

Class Derived classes Name: Access Control base class name 1, access control base class Name 2, ..., access control base class name n

{

data member and member function declarations

};

A derived class constructor for multiple base classes can initialize a data member with an initial invocation of the base class constructor. The order of execution is similar to a single-inheritance constructor.   The order in which multiple direct base class constructors are executed depends on the order of each inherited base class that is specified when the derived class is defined. A derived class object has members of multiple direct or indirect base classes. Access to different names does not appear to be two semantic. If a different base class has a member of the same name, the derived class object should be recognized when accessing it. Simple application with multiple inheritance
1 classBase12{ Public:3BASE1 (intX) {value =x;}4       intGetData ()Const{returnvalue;}5    protected:6       intvalue;7 };8 classBase29{ Public:TenBase2 (Charc) {letter=C;} One       CharGetData ()Const{returnLetter ;} A    protected: -       CharLetter ; - }; the classDerived: PublicBASE1, PublicBase2 -{Friend Ostream &operator<< (Ostream &,ConstDerived & ) ; -     Public : -Derived (int,Char,Double ) ; +       DoubleGetreal ()Const ; -    Private : +       DoubleReal; A};

2. Virtual base class

If a derived class derives from more than one base class, and these base classes have a common

base class, the name that is declared in the base class may have a

Ambiguity

For example:

1 classB Public:intb;} ;2 3 classB1: PublicBPrivate:intB1;} ;4 5 classB2: PublicBPrivate:intB2;} ;6 7 classE | PublicB1, PublicB28 9{ Public:intf ();Private:intD;} ;Ten  One  A  - /*c C; -  the c.  B Error -  - c.  B:: b; Error, where did you inherit it from?  -  + c.  B1:: b; Ok, inherited from B1. -  + c.  B2:: b; Ok, inherited from B2.*/ A  at  -  -#include <iostream> -  - using namespacestd; -  in intMain () -  to {c C; +  -C. B1:: b =5; C. B2:: b =Ten ; the  *cout <<"Path b1==>"<< c. B1:: b <<Endl; $ Panax Notoginsengcout <<"Path b2==>"<< c. B2:: b <<Endl; -  the}

When an object of Class C is created, the constructor of B is called two times: once by B1 and another by B2 to initialize the two Class B sub-objects contained in the Class C object

If there is a common base class on multiple inheritance paths, somewhere in the inheritance path

The common base class produces multiple base class sub-objects in the object of the derived class.

In order for this public base class to produce only one child object in the derived class, the base class must be

Declared as virtual inheritance, which makes this base class A virtual base class.

Virtual inheritance declaration uses the keyword virtual
1#include <iostream>2 using namespacestd;3 4 //test of virtual base class5 classA6 {7  Public:8A () {cout <<"class a\n"; }9 };Ten  One //If you do not add the virtual keyword here, the constructor for Class A is called two times A classB1:Virtual  PublicA - { -  Public: theB1 () {cout <<"class b1\n"; } - }; - //If you do not add the virtual keyword here, the constructor for Class A is called two times - classB2:Virtual  PublicA + { -  Public: +B2 () {cout <<"class b2\n";} A }; at classC: PublicB1, PublicB2 - { -  Public: -C () {cout <<"class c\n"; } - }; - voidMain () in { - c C; to}
Inheritance is an important method for object-oriented programming to realize software reuse.   A programmer can define a new derived class on the basis of an existing base class. A derived class with single inheritance has only one base class.   Multiple-inheritance derived classes have more than one base class.   The access of a derived class to a base class member is determined by the inheritance method and the member nature. When you create a derived class object, first call the base class constructor to initialize the base class member in the derived class.   The order of the destructor is called and the constructor is called in the reverse order.   C + + provides a virtual inheritance mechanism to prevent the two semantics of member access in class inheritance relationships. Multi-Inheritance provides a powerful feature of software reuse and adds complexity to the program.

C + + inheritance of three

Related Article

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.