CPP Polymophism and Multiple Inheritance

Multiple Inheritance is possible in C++

  • C++ supports the possibility to derive a class from more than one base class.
    A class model:
  • We can say that a PaperBox is made of paper and has a form of a Cuboid.
    To specify this in a C++ file:
    class PaperBox : public Cuboid, public Paper
  • Polymorphism can be used with Multiple Inheritance in the same manner as for a single Inheritance. (You should study the Constructors and Destructors calling sequence in example below as well).
    Multiple Inheritance example:
    #include <iostream>
    #include <string>
    
    enum Quality { BAD, GOOD, EXCELLENT};
    
    class Paper {
    public:
      Paper(Quality quality) {
        this->quality=quality;
        std::cout << "Paper constructor... \n";
      }
      virtual ~Paper() { std::cout << "Paper destructor... \n"; }
      virtual  void showInfo() {
        std::cout << "a ";
        std::cout << getQuality() ;
        std::cout << " paper Quality\n ";}
      void setQuality(Quality quality ) {
        this->quality=quality;  }
    private:
      Quality quality;
      std::string getQuality() {
        switch (quality) {
          case BAD : return "Bad";
          case GOOD : return "Good";
          case EXCELLENT : return "Excellent";
          default: return "Unknown";
        }
      }
    
    };
    
    class Cuboid {
    public:
      Cuboid(int width, int height, int length ) {
        std::cout << "Cuboid constructor... \n";
        this->width=width;
        this->height=height;
        this->length=length;
      }
      virtual ~Cuboid() { std::cout << "Cuboid destructor... \n"; }
      virtual  void showInfo() {
        std::cout << "the volume is ";
        std::cout << getVolume() << "\n"; }
      void setLength(int length) { this->length=length; }
      int getVolume() const { return length*width*length; }
    private:
      int width;
      int height;
      int length;
    };
    // PaperBox has Cuboid class and Paper class as Base classes;
    class PaperBox : public Cuboid, public Paper {
    public:
      PaperBox(int width, int height, int length ):
          Cuboid( width, height,length),Paper(BAD) {
            std::cout << "PaperBox constructor... \n";
          }
          ~PaperBox() { std::cout << "PaperBox destructor... \n"; }
          void showInfo() {
            std::cout << "I am a PaperBox and ";
            Cuboid::showInfo();
            std::cout << "with ";
            Paper::showInfo();
          }
    };
    
    int main()
    {
      Paper * box1= new PaperBox(4,5,6);
      std::cout << "\n" ;
      box1->showInfo();
      std::cout << "\n" ;
      box1->setQuality(EXCELLENT);
      // down cast to set length of the box;
      PaperBox * box1Casted = dynamic_cast<PaperBox*>(box1);
      box1Casted->setLength(7);
      box1Casted->showInfo();
      std::cout << "\n" ;
      delete box1;
      std::cout << "\n" ;
    
      Cuboid * box2= new PaperBox(2,3,4);
      std::cout << "\n" ;
      box2->showInfo();
      std::cout << "\n" ;
      box2->setLength(8);
      // down cast to set quality of the box;
      PaperBox * box2Casted = dynamic_cast<PaperBox*>(box2);
      box2Casted->setQuality(GOOD);
      box2Casted->showInfo();
      std::cout << "\n" ;
      delete box2;
      std::cout << "\n" ;
    
      return 0;
    }
    When we run this application, the result will be:
    Cuboid constructor...
    Paper constructor...
    PaperBox constructor...
    
    I am a PaperBox and the volume is 144
    with a Bad paper Quality
    
    I am a PaperBox and the volume is 196
    with a Excellent paper Quality
    
    PaperBox destructor...
    Paper destructor...
    Cuboid destructor...
    
    Cuboid constructor...
    Paper constructor...
    PaperBox constructor...
    
    I am a PaperBox and the volume is 32
    with a Bad paper Quality
    
    I am a PaperBox and the volume is 128
    with a Good paper Quality
    
    PaperBox destructor...
    Paper destructor...
    Cuboid destructor...

    You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.