Multiple inheritance and virtual inheritance __c++ of Ii. C + + class

Source: Internet
Author: User
multi-inheritance and virtual inheritance of Ii. C + + class

In the past study, we are always in touch with the inheritance of a single class, but in real life, some new things tend to have two or more than two attributes of things, in order to solve this problem, C + + introduced the concept of multiple inheritance,C + + allows a derived class to specify multiple base classes, Such an inheritance structure is called multiple inheritance .

For example, vehicles can derive from cars and boats, but with both cars and boats, amphibious vehicles must inherit from the common attributes of the vehicle class and the ship.

This is not difficult to come up with the following legend and code:

When a derived class uses multiple inheritance, the class names of all base classes must be listed after the derived class name and colon, and are separated by a tease.

Program Author: Junning
Site: www.cndev-lab.com
All manuscripts are copyrighted, if you want to reprint, please be sure that 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//ship
{
Public
Boat (int weight=0,float tonnage=0): Vehicle (weight)
{
Boat::tonnage = tonnage;
}
void ShowMe ()
{
cout<< "I am the ship." "<<endl;
}
Protected
float tonnage;
};

Class Amphibiancar:public Car,public boat//amphibious vehicle, embodiment of multiple inheritance
{
Public
Amphibiancar (int weight,int aird,float tonnage)
: Vehicle (weight), car (Weight,aird), boat (Weight,tonnage)
Multiple inheritance to be aware of calling the base class constructor
{

}
void ShowMe ()
{
cout<< "I am an amphibious vehicle. "<<endl;
}
};
int main ()
{
Amphibiancar A (4,200,1.35f);//Error
A.setweight (3);//Error
System ("pause");
}

The above code from the surface, see no obvious errors, but it is not able to compile. That's why.
This is a problem caused by the ambiguity of inheritance brought about by multiple inheritance. First look at the following diagram:

In the picture, where the crimson mark is the main problem, the amphibious vehicle class inherits the properties and methods from the car class and the boat class, and the cars class and boat are the base classes of the Amphibiancar class, Amphibiancar obtained the setweight () member function from two classes on the memory allocation when we called a. Setweight (3) The computer does not know how to choose a duplicate-owned class member function setweight () that belongs to two base classes respectively.

Because the existence of this ambiguity problem also leads to amphibiancar a (4,200,1.35f), execution fails, and the system generates vehicle "is not a base or a member of the error."

Take the above code as an example, we want to let the Amphibiancar class both get a vehicle copy, and also share the car class and boat class data members and member functions must be through C + + provided by the virtual inheritance technology to achieve.

We inherit the vehicle class from the car class and the boat class, and we can implement virtual inheritance with the virtual keyword in front of it, and then when the system encounters multiple inheritance, it automatically joins a vehicle copy. When a vehicle copy is requested again, it is ignored to ensure the uniqueness of the inherited class member function.

Revise the code below to observe the changes:

Program Author: Junning
Site: www.cndev-lab.com
All manuscripts are copyrighted, if you want to reprint, please be sure that the famous source and author

#include <iostream>
using namespace Std;

Class Vehicle
{
Public
Vehicle (int weight = 0)
{
Vehicle::weight = weight;
cout<< "Load 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, here is the virtual inheritance
{
Public
Car (int weight=0,int aird=0): Vehicle (weight)
{
Car::aird = Aird;
cout<< "Load Car class constructor" <<endl;
}
void ShowMe ()
{
cout<< "I'm a car." "<<endl;
}
Protected
int Aird;
};

Class Boat:virtual public vehicle//ship, here is the 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 am the ship." "<<endl;
}
Protected
float tonnage;
};

Class Amphibiancar:public Car,public boat//amphibious vehicle, embodiment of multiple inheritance
{
Public
Amphibiancar (int weight,int aird,float tonnage)
: Vehicle (weight), car (Weight,aird), boat (Weight,tonnage)
Multiple inheritance to be aware of calling the base class constructor
{
cout<< "Load Amphibiancar class constructor" <<endl;
}
void ShowMe ()
{
cout<< "I am an amphibious vehicle. "<<endl;
}
void Showmembers ()
{
cout<< "Weight:" <<weight<< "Dayton," << "Air Displacement:" <<aird<< "CC," << "Displacement:" << tonnage<< "Dayton" <<endl;
}
};
int main ()
{
Amphibiancar A (4,200,1.35f);
A.showme ();
A.showmembers ();
A.setweight (3);
A.showmembers ();
System ("pause");
}

Note the order in which class constructors are constructed.

Although virtual inheritance has something in common with virtual functions, it is important for readers to remember that there is absolutely no connection between them. http://pcedu.pconline.com.cn/empolder/gj/c/0503/579115_1.html

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.