Deep parsing of multiple inheritance _c languages for classes in C + +

Source: Internet
Author: User
Tags c constructor constructor inheritance multiple inheritance in c

Multiple inheritance for C + + classes
In the previous example, a derived class has only one base class, called a single inheritance. In addition, C + + supports multiple inheritance, that is, a derived class can have two or more base classes.
Multiple inheritance is easy to complicate code logic, confusion, has been controversial, small and medium-sized projects are less used, and later Java, C #, PHP, and so simply canceled more inheritance. Readers who want to learn C + + quickly do not have to read them carefully.
The syntax for multiple inheritance is also simple, separating multiple base classes with commas. For example, Class A, Class B, and Class C have been declared, so you can declare derived class D:

Class D:public A, Private B, protected c{
 //class D new addition member
}


D is a multiple-inherited derived class that inherits Class A in a common way, inherits Class B in a private way, and inherits the Class C in a protected way. D Gets the members in a, B, C according to different inheritance methods, and determines the access rights of the members of each base class in the derived class.
constructors under multiple inheritance

The constructors of a multiple-inheritance derived class are essentially the same as a single inheritance class, except that you want to include more than one base class constructor. Such as:

Class D constructor name (total parameter table column): A constructor (argument table column), Class B Constructor (argument table column), Class C Constructor (argument table column) {
 new member initialization statement
}


Each base class is arranged in any order.

Derived class constructors are executed in the same order that the constructor of the base class is called first, and then the derived class constructor is called. The order in which the base class constructors are called is the order in which the base class appears when the derived class is declared.

The following defines two base classes, the Basea class and the Baseb class, and then derives the sub class in multiple inheritance ways.

#include <iostream>
using namespace std;
Base class
basea{
protected:
 int A;
 int b;
Public:
 basea (int, int);
Basea::basea (int A, int b): A (a), B (b) {}
//base class
baseb{
protected:
 int C;
 int D;
Public:
 baseb (int, int);
BASEB::BASEB (int c, int d): C (c), D (d) {}
//derived class
class Sub:public Basea, public baseb{
private:
 int e; Public
:
 Sub (int, int, int, int, int);
 void display ();
Sub::sub (int A, int b, int c, int d, int e): Basea (A, B), Baseb (c, D), E (e) {}
void Sub::d isplay () {
 cout<< " A= "<<a<<endl;
 cout<< "b=" <<b<<endl;
 cout<< "c=" <<c<<endl;
 cout<< "d=" <<d<<endl;
 cout<< "e=" <<e<<endl;
}
int main () {
 (new Sub (1, 2, 3, 4, 5))-> display ();
 return 0;
}

Run Result:

A=1
b=2
c=3
d=4
e=5

Member variables inherited from the base class Basea and Baseb are accessible in Sub::d isplay ().
Naming Conflicts

A naming conflict occurs when a member of the same name is in the two base class, and you cannot access the member directly, and you need to add the class name and the domain parser.

If there is a member function display () in the base class Basea and BASEB, the following statement is incorrect:

Sub obj;
Obj.display ();


Because there are display () in both Basea and BASEB, the system will not be able to determine which class function to call, so the error is not correct.

You should add the class name and the domain parser as follows:

Sub obj;
Obj. Basea::d isplay ();
Obj. BASEB::d isplay ();


This example shows that duplicate data is inherited from different base classes when multiple inheritance is used. If you have more than one base class, the problem is more pronounced, so when you design a derived class, consider its data members carefully, minimizing data redundancy.

Two semantic problems of multiple inheritance in C + +
Multiple inheritance can reflect the situation in real life, can effectively deal with some more complex problems, so that the program has flexibility, but multiple inheritance also caused some noteworthy problems, it increased the complexity of the program, so that the program writing and maintenance becomes relatively difficult, error-prone. One of the most common problems is the two semantic (ambiguous) problem resulting from the name of the inherited member.

If both Class A and Class B have member functions display and data member A, Class C is a direct derived class of Class A and Class B. The following 3 cases are discussed separately.

1) Two base classes have members with the same name

The code looks like this:

Class A
{public
:
 int A;
 void display ();
Class B
{public
:
 int A;
 void display ();
Class C:public A, public B
{public
:
 int b;
 void Show ();

If you define the Class C object Cl in the main function, and call the data member A and member function display:

 C cl;
 Cl.a=3;
 Cl.display ();


Because both base class A and base class B have data member A and member function display, the compilation system cannot discriminate which base class member is to be accessed, so the program compiles an error. So, how should we solve this problem? You can qualify with the base class name:

 Cl. A::a=3; Reference data member a CL of base class A in the CL object
 . A::d isplay (); Call the member function display of base class A in the CL object

If you are accessing display and a of base class A through derived class member functions in derived Class C, you can write directly without writing the object name

 A::a = 3; Refers to the current object
 A::d isplay ();

2) Two base classes and derived classes all have members with the same name

Replace the above Type C declaration with the following:

 Class C:public A, public B
 {
  int A;
  void display ();
 


If you define the Class C object Cl in the main function, and call the data member A and member function display:

 C cl;
 CL.A = 3;
 Cl.display ();


At this point, the program can be compiled, but also normal operation. What is the member of the class that is accessed during execution? The answer is: you are accessing a member of a derived class C. The rule is that a member with the same name as a base class is masked in a derived class, becomes "invisible," or that a newly added member of the derived class has a member with the same name in the base class. Therefore, if you access a member of the same name through an object name in a module that defines a derived class object, you are accessing a member of the derived class. Note: Different member functions only have the same name overwrite if the function name and parameter number are the same, and the type matches, if only the function name is the same and the parameter is different, the same name overrides and the function overload.

Some readers may not be able to understand the overriding name. To illustrate the problem, for example, China as a base, Sichuan is China's derived class, Chengdu is the derivation of Sichuan. The base class is relatively abstract, the derived class is relatively specific, the base class is in the outer layer, has a broader scope, the derived class is inside, and has a localized scope. If the "China" class has the average temperature of this attribute, Sichuan and Chengdu have the average temperature of this attribute, if there is no Sichuan and Chengdu, the two derived classes, the average temperature is obviously refers to the national average temperature. If in Sichuan, talking about the local average temperature is obviously the average temperature in Sichuan; If you talk about the local average temperature in Chengdu, it obviously means the average temperature in Chengdu. This means that the national "average temperature" in Sichuan province by the "average temperature" shielding, or, Sichuan's "average temperature" in the local shielding the national "average temperature." Sichuan people are most concerned about the temperature of Sichuan, of course, do not want to use the national temperature to cover the average temperature in Sichuan.

If you want to check the national average temperature in Sichuan, be sure to declare: I want to check the national average temperature. Similarly, to access members of base class A outside of a derived class, you should indicate the scope a, which is written in the following form:

 Cl. A::a=3; Represents the data member a CL in base class A in the derived class object Cl
 . A::d isplay (); Representation is a member function display in the base class A of the derived class object Cl

3 Class A and Class B are derived from the same base class

The code looks like this:

Class N
{public
:
 int A;
 void display () {cout<< "a::a=" <<a<<endl;}
};
Class A:public N
{public
:
 int al;
};
Class B:public N
{public
:
 int a2;
};
Class C:public A, public B
{public
:
 int A3;
 void Show () {cout<< "a3=" <<a3<<endl;}
}
int main ()
{
 c cl;//define Class C object CL
 //other code
}

Although no data member A and member function display are defined in Class A and Class B, they inherit data member A and member function display from class N, so that both the data member A and the member function display with the same name exist in Class A and Class B. They are copies of N-Class members. The data member A in Class A and Class B represents two different storage units and can hold different data separately. In a program, you can call the constructor of the base class N through the constructors of Class A and Class B to initialize the data member A of Class A and Class B, respectively.

How do you access a member of Class A that inherits from base class n? Obviously no use.

 CL.A = 3; Cl.display ();


Or

 Cl. N::a = 3; CL. N::d isplay ();


Because this still doesn't distinguish between the members that are inherited from base class N in Class A or the members inherited from base class N in Class B. You should use the direct derivation class name of Class N to indicate which base class member in the derived class of Class n is to be accessed. Such as

 Cl. A::a=3; Cl. A::d isplay (); To access the base class member in the derived class A of class n

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.