Graphic examples illustrate multiple inheritance and virtual inheritance of c ++ classes)

Source: Internet
Author: User

In the past, we have always been engaged in the inheritance of a single class, but in real life, some new things often have the attributes of two or more things. To solve this problem, c ++ introduces the concept of multi-inheritance,C ++ allows you to specify multiple base classes for a derived class. This inheritance structure is called multi-inheritance..

For example, a vehicle and a ship can be derived from a subclass. However, a vehicle and a ship share the same characteristics as an amphibious vehicle and must inherit the common attributes of a vehicle and a ship.
Therefore, it is difficult to come up with the following example:Code:

To use multiple inheritance for a derived class, you must list the class names of all the base classes after the derived class name and colon, and use them to separate them.

// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream>
Using namespace STD;
Class Vehicle
{
Public:
Vehicle (INT Weight = 0)
{
Vehicle: Weight = weight;
}
Void setweight (INT weight)
{
Cout <"Reset weight" <Endl;
Vehicle: Weight = weight;
}
Virtual void showme () = 0;
Protected:
Int weight;
};
Class car: public vehicle // car
{
Public:
Car (INT Weight = 0, int AIRD = 0): Vehicle (weight)
{
Car: AIRD = AIRD;
}
Void showme ()
{
Cout <"I'm a car! "<Endl;
}
Protected:
Int AIRD;
};
Class boat: public vehicle // boat
{
Public:
Boat (INT Weight = 0, float Tonnage = 0): Vehicle (weight)
{
Boat: Tonnage = tonnage;
}
Void showme ()
{
Cout <"I'm a ship! "<Endl;
}
Protected:
Float tonnage;
};
Class amphibiancar: Public car, public boat // dual-purpose vehicle, multiple inheritance embodiment
{
Public:
Amphibiancar (INT weight, int AIRD, float tonnage)
: Vehicle (weight), car (weight, AIRD), boat (weight, tonnage)
// Pay attention to calling the base class constructor for multiple inheritance
{
}
Void showme ()
{
Cout <"I'm a dual-purpose vehicle! "<Endl;
}
};
Int main ()
{
Amphibiancar A (4,200, 1.35f); // Error
A. setweight (3); // Error
System ("pause ");
}

On the surface, the above Code does not show obvious syntax errors, but it cannot be compiled. Why?
This is caused by the ambiguity of inheritance caused by multiple inheritance.

First look at the figure below:

In the figure, the primary problem lies in the deep red mark. The amphibious vehicle class inherits the attributes and methods from the car class and the boat class, and the car class and the boat class are the same as the base class of the amphibiancar class, on the memory allocation, amphibiancar obtains the setweight () member function from two classes. When we call. when setweight (3) is used, the computer does not know how to select the repeated class member function setweight () that belongs to two base classes ().

Because of this fuzzy problem, amphibiancar A (4,200, 1.35f) is also caused. If the execution fails, the system will generate a vehicle "not a base or member error.

The above code is used as an example. To make the amphibiancar class get a copy of vehicle, in addition, data members and member functions of the car and boat classes must be shared throughVirtual inheritanceTechnology.

We inherit the vehicle class from the car class and boat class, and add the virtual keyword before it to implement virtual inheritance. After using virtual inheritance, when the system encounters multiple inheritance, it will automatically add a copy of vehicle. When a copy of vehicle is requested again, it will be ignored to ensure the uniqueness of the inherited class member functions.
The modified code is as follows. Observe the changes:

// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.
# Include <iostream>
Using namespace STD;
Class Vehicle
{
Public:
Vehicle (INT Weight = 0)
{
Vehicle: Weight = weight;
Cout <"loading vehicle class constructor" <Endl;
}
Void setweight (INT weight)
{
Cout <"Reset weight" <Endl;
Vehicle: Weight = weight;
}
Virtual void showme () = 0;
Protected:
Int weight;
};
Class car: virtual public vehicle // car, where virtual inheritance
{
Public:
Car (INT Weight = 0, int AIRD = 0): Vehicle (weight)
{
Car: AIRD = AIRD;
Cout <"loading car class constructor" <Endl;
}
Void showme ()
{
Cout <"I'm a car! "<Endl;
}
Protected:
Int AIRD;
};
Class boat: virtual public vehicle // ship, where virtual inheritance
{
Public:
Boat (INT Weight = 0, float Tonnage = 0): Vehicle (weight)
{
Boat: Tonnage = tonnage;
Cout <"load boat class constructor" <Endl;
}
Void showme ()
{
Cout <"I'm a ship! "<Endl;
}
Protected:
Float tonnage;
};
Class amphibiancar: Public car, public boat // dual-purpose vehicle, multiple inheritance embodiment
{
Public:
Amphibiancar (INT weight, int AIRD, float tonnage)
: Vehicle (weight), car (weight, AIRD), boat (weight, tonnage)
// Pay attention to calling the base class constructor for multiple inheritance
{
Cout <"loading amphibiancar class constructor" <Endl;
}
Void showme ()
{
Cout <"I'm a dual-purpose vehicle! "<Endl;
}
Void showmembers ()
{
Cout <"Weight:" <weight <"pause," <"Air Displacement:" <AIRD <"cc," <"displacement: "<tonnage <" Don "<Endl;
}
};
Int main ()
{
Amphibiancar A (4,200, 1.35f );
A. showme ();
A. showmembers ();
A. setweight (3 );
A. showmembers ();
System ("pause ");
}

Observe the construction sequence of the class constructor.
Although Virtual inheritance is similar to virtual functions, readers must remember that there is absolutely no connection between them!

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.