Multiple Inheritance - C++ Class

C++ examples for Class:Inheritance

Description

Multiple Inheritance

Demo Code

                                                                                                                                                                                                                                                                                                                           
class Base1 /*from w  w  w  .  j  av a 2 s  .  c om*/
{ 
public: 
    Base1( int parameterValue ) 
    { 
       value = parameterValue; 
    }
                                                                                                                                                                                                                                                                                                                           
    int getData() const 
    { 
       return value; 
    }
protected: // accessible to derived classes 
    int value; // inherited by derived class 
};
                                                                                                                                                                                                                                                                                                                           
class Base2 
{ 
public: 
    Base2( char characterData ) 
    { 
       letter = characterData; 
    } // end Base2 constructor 
                                                                                                                                                                                                                                                                                                                           
   char getData() const 
    { 
       return letter; 
    }
protected: // accessible to derived classes 
   char letter; // inherited by derived class 
}; 
                                                                                                                                                                                                                                                                                                                           
#include <iostream> 
using namespace std; 
                                                                                                                                                                                                                                                                                                                           
class Derived : public Base1, public Base2 
{ 
    friend ostream &operator<<( ostream &, const Derived & ) ; 
public: 
   Derived( int, char, double ); 
   double getReal() const; 
private: 
   double real; // derived class's private data 
};
                                                                                                                                                                                                                                                                                                                           
Derived::Derived( int integer, char character, double double1 ) 
     : Base1( integer ), Base2( character ), real( double1 ) { } 
                                                                                                                                                                                                                                                                                                                           
double Derived::getReal() const 
{ 
    return real; 
}
ostream &operator<<( ostream &output, const Derived &derived ) 
{ 
    output << "         Integer: " << derived.value << "\n  Character: " << derived.letter << "\nReal number: " << derived.real; 
    return output; // enables cascaded calls 
}
#include <iostream> 
using namespace std; 
                                                                                                                                                                                                                                                                                                                           
int main() 
{ 
    Base1 base1( 10 ), *base1Ptr = 0; // create Base1 object 
    Base2 base2( 'Z' ), *base2Ptr = 0; // create Base2 object 
   Derived derived( 7, 'A', 3.5 ); // create Derived object 
                                                                                                                                                                                                                                                                                                                           
   cout << "Object base1 contains integer " << base1.getData() 
       << "\nObject base2 contains character " << base2.getData() 
       << "\nObject derived contains:\n" << derived << "\n\n" ; 
                                                                                                                                                                                                                                                                                                                           
   // print data members of derived-class object 
   // scope resolution operator resolves getData ambiguity 
   cout << "Data members of Derived can be accessed individually:" 
       << "\n      Integer: " << derived.Base1::getData() 
       << "\n  Character: " << derived.Base2::getData() 
       << "\nReal number: " << derived.getReal() << "\n\n"; 
   cout << "Derived can be treated as an object of either base class:\n"; 
                                                                                                                                                                                                                                                                                                                           
   // treat Derived as a Base1 object 
    base1Ptr = &derived; 
   cout << "base1Ptr->getData() yields " << base1Ptr->getData() << '\n'; 
                                                                                                                                                                                                                                                                                                                           
   // treat Derived as a Base2 object 
    base2Ptr = &derived; 
   cout << "base2Ptr->getData() yields " << base2Ptr->getData() << endl; 
}

Result


Related Tutorials