Software & Finance





C++ - Multiple Inheritance





In this sample, Shape is the base class. Triangle and Circle are the derived classes derived from the base class Shape. It is called inheritance.

class Cone is derived from both two base classes Triangle and Circle. It is called Multiple Inheritance.

Source Code


 

class Shape

{

public:

    Shape()

    {

        std::cout << "Shape\n";

    }

    virtual void draw() { };

    virtual void display() { };

};

 

class Circle : public Shape

{

    int r;

public:

    Circle()

    {

        std::cout << "Circle\n\n";

    }

    virtual void draw() {};

    virtual void display() { };

};

 

class Triangle : public Shape

{

    int a,b,c;

public:

    Triangle()

    {

        std::cout << "Triangle\n\n";

    }

    virtual void draw() {};

    virtual void display() { };

};

 

class Cone : public Triangle, public Circle

{

public:

    Cone()

    {

        std::cout << "Cone\n\n";

    }

    virtual void draw() {};

    virtual void display() { };

 

};