A single class can inherit from more than one base class - C++ Class

C++ examples for Class:Inheritance

Description

A single class can inherit from more than one base class

Demo Code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Bed// ww w.j a  v  a 2 s . c  o m
{
  public:
    Bed(){}
    void sleep(){ cout << "Sleep" << endl; }
    int weight;
};

class Sofa
{
  public:
    Sofa(){}
    void watchTV(){ cout << "Watch TV" << endl; }
    int weight;
};

class SleeperSofa : public Bed, public Sofa
{
  public:
    SleeperSofa(){}
    void foldOut(){ cout << "Fold out" << endl; }
};

int main(int nNumberofArgs, char* pszArgs[])
{
    SleeperSofa ss;

    ss.watchTV();     // calls Sofa::watchTV()

    ss.foldOut();     // calls SleeperSofa::foldOut()

    ss.sleep();       // calls Bed::sleep()

    return 0;
}

Result


Related Tutorials