C ++ multi-Inheritance

Source: Internet
Author: User

I. Problems with single inheritance
Suppose you have been using the animal class for a while, and divide the class hierarchy into birds and mammals. The bird class includes the member function fly. The horse class is derived from the mammal class. The horse class includes the member functions whinny and gallop.
Now we need a Pegasus object, Pegasus: an animal between a horse and a bird. It may contain member functions fly and whinny. This is a dilemma when using single inheritance.
Solution 1: derive Pegasus from the horse class and copy fly to Pegasus. In this case, the maintenance staff had to modify the fly, and they needed to modify the fly in two places. However, this method cannot be interpreted as
Bird object.
The second method is to rename the horse gallop method to move, and overwrite the move in Pegasus to complete the fly job. For example
Pegasus: Move (long distance)
{
If (distance> veryfar)
Fly (distance );
Else
Gallop (distance );
}
The disadvantage is that it is hard to do it when Pegasus wants to fly within a short distance.
The third method is to improve. Place the required functions in a high class hierarchy. If there are more common methods, the base class will be very large.
Method 3: downward conversion. Leave the fly method in Pegasus and call it only when the Pointer Points to the Pegasus object. Use dynamic_cast to check.
Ii. Multi-Inheritance
Syntax:
Class derivedclass: Public baseclass1, public baseclass2
When you create a derivedclass in the memory, both base classes become part of the Pegasus object.
There are multiple basic classes to handle:
1 What if two base classes have virtual functions or data of the same name? Full limitation
2 If you call multiple base class constructor?
Cpegasus (color, bool migration, hands height, int age ):
Chorse (color, height, age ),
Cbird (color, migration, age)
3 What if multiple base classes are derived from the same class?
Fully Qualified ppeg-> chorse: getcolor ()
4. inherit from the common base class. If you call the methods of the common base class
Fully Qualified chorse: getage ()

Or virtual inheritance

# Include <iostream> <br/> # include <string> <br/> using namespace STD; <br/> typedef int hands; <br/> Enum color {red, green, blue, yellow, white, black, brown }; </P> <p> class canimal <br/>{< br/> public: <br/> canimal (INT age) {itsage = age; cout <"Animal constructor" <Endl ;}< br/> virtual ~ Canimal () {cout <"Animal destructor" <Endl ;}< br/> virtual void setage (INT age) {itsage = age ;} <br/> virtual int getage () const {return itsage ;}< br/> PRIVATE: <br/> int itsage; </P> <p> }; <br/> class chorse: Public canimal <br/>{< br/> Public: <br/> chorse (color, hands height, int age): canimal (AGE) <br/> {itscolor = color; itsheight = height; cout <"horse constructor" <Endl ;}< br/> virtual void Whinny () const {cout <"horse whinny..." <Endl ;}< br/> virtual ~ Chorse () {cout <"horse destructor" <Endl ;}< br/> virtual hands getheight () const {return itsheight ;}< br/> virtual color getcolor () const {return itscolor ;}< br/> PRIVATE: <br/> color itscolor; <br/> hands itsheight; <br/>}; <br/> class cbird: public canimal <br/>{< br/> Public: <br/> cbird (color, bool migration, int age): canimal (AGE) <br/> {itscolor = color; itsmigration = migration; cout <"Bir D constructor "<Endl ;}< br/> virtual ~ Cbird () {cout <"bird destructor" <Endl ;}< br/> virtual void chirp () const {cout <"bird chirp... "<Endl ;}< br/> virtual void fly () const {cout <" bird fly... "<Endl ;}< br/> virtual bool getmigration () const {return itsmigration ;}< br/> virtual color getcolor () const {return itscolor ;} <br/> PRIVATE: <br/> color itscolor; <br/> bool itsmigration; <br/>}; <br/> class cpegasus: Public chorse, public CB IRD <br/>{< br/> Public: <br/> cpegasus (color, bool migration, hands height, int age): chorse (color, height, age ), <br/> cbird (color, migration, age) <br/> {cout <"Pegasus constructor" <Endl ;}< br/> virtual ~ Cpegasus () {cout <"Pegasus destructor" <Endl ;}< br/> virtual void chirp () const {whinny ();} <br/> virtual void fly () const {cout <"Pegasus fly... "<Endl ;}< br/> // virtual color getcolor () const {return chorse: getcolor () ;}< br/> virtual int getage () const {return chorse: getage () ;}< br/>}; <br/> int main () <br/>{< br/> cpegasus * ppeg = new cpegasus (red, true, 5, 10 ); <br/> cout <"This Pegasus's color is" <ppeg-> chorse: getcolor () <Endl; <br/> cout <"This Pegasus's age is" <ppeg-> getage () <Endl; <br/> Delete ppeg; <br/> return 0; <br/>}
Iii. Virtual inheritance
If horse and bird have the same base class, it is random to decide which method to use. However, it can be told that C ++ does not want to use two copies of the common base class, but just wants a copy of the common base class. It can make animal a base class for horse and bird. You only need to add the keyword virtual in the declaration, and animal does not need to be modified. Make a lot of modifications to Pegasus.




Generally, the class constructor only initializes its own variables and their base classes, except for the base classes inherited by virtual machines. They are initialized by the final Derived classes. Therefore, animal is not initialized by horse and bird, but by Pegasus. Horse and bird must initialize animal in their constructor, but these Initialization is ignored when the Pegasus object is created.
Class chorse: virtual public canimal
Class cbird: virtual public canimal
Cpegasus (color, bool migration, hands height, int age): chorse (color, height, age ),
Cbird (color, migration, age), canimal (age * 2)

# Include <iostream> <br/> # include <string> <br/> using namespace STD; <br/> typedef int hands; <br/> Enum color {red, green, blue, yellow, white, black, brown }; </P> <p> class canimal <br/>{< br/> public: <br/> canimal (INT age) {itsage = age; cout <"Animal constructor" <Endl ;}< br/> virtual ~ Canimal () {cout <"Animal destructor" <Endl ;}< br/> virtual void setage (INT age) {itsage = age ;} <br/> virtual int getage () const {return itsage ;}< br/> PRIVATE: <br/> int itsage; </P> <p> }; <br/> // <br/> class chorse: virtual public canimal <br/>{< br/> Public: <br/> chorse (color, hands height, int age): canimal (AGE) <br/> {itscolor = color; itsheight = height; cout <"Horse constructor" <Endl ;}< br/> virtual void whinny () const {cout <"horse whinny... "<Endl ;}< br/> virtual ~ Chorse () {cout <"horse destructor" <Endl ;}< br/> virtual hands getheight () const {return itsheight ;}< br/> virtual color getcolor () const {return itscolor ;}< br/> PRIVATE: <br/> color itscolor; <br/> hands itsheight; <br/> }; <br/> // <br/> class cbird: virtual public canimal <br/>{< br/> Public: <br/> cbird (color, bool migration, int age): canimal (AGE) <br/> {itsco Lor = color; itsmigration = migration; cout <"bird constructor" <Endl ;}< br/> virtual ~ Cbird () {cout <"bird destructor" <Endl ;}< br/> virtual void chirp () const {cout <"bird chirp... "<Endl ;}< br/> virtual void fly () const {cout <" bird fly... "<Endl ;}< br/> virtual bool getmigration () const {return itsmigration ;}< br/> virtual color getcolor () const {return itscolor ;} <br/> PRIVATE: <br/> color itscolor; <br/> bool itsmigration; <br/>}; <br/> class cpegasus: Public chorse, public CB IRD <br/>{< br/> public: <br/> // <br/> cpegasus (color, bool migration, hands height, int age): chorse (color, height, age), <br/> cbird (color, migration, age), canimal (age * 2) <br/> {cout <"Pegasus constructor" <Endl ;}< br/> virtual ~ Cpegasus () {cout <"Pegasus destructor" <Endl ;}< br/> virtual void chirp () const {whinny ();} <br/> virtual void fly () const {cout <"Pegasus fly... "<Endl ;}< br/> // virtual color getcolor () const {return chorse: getcolor () ;}< br/> // virtual int getage () const {return chorse: getage () ;}< br/>}; <br/> int main () <br/>{< br/> cpegasus * ppeg = new cpegasus (red, true, 5, 10 ); <br/> cout <"This Pegasus's color is" <ppeg-> chorse: getcolor () <Endl; <br/> cout <"This Pegasus's age is" <ppeg-> getage () <Endl; <br/> Delete ppeg; <br/> return 0; <br/>}

Iv. Problems with multi-Inheritance
Development of Multi-inheritance class hierarchies is more difficult and risky than development ticket hierarchies, and debugging is more difficult. Languages such as Java and C # do not support multiple inheritance.
V. Hybrid (function)
A compromise between multi-inheritance and single-inheritance is the use of Mixin ). Classes for mixed classes add special functions without adding a large number of methods or data.
The only difference between a function class and other classes is that the function class has no or only a small amount of data.

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.