C++ Class Inheritance Adding Virtual Inheritance, in multiple inheritance

Description

C++ Class Inheritance Adding Virtual Inheritance, in multiple inheritance

#include <cstdio>
#include <cstdlib>
#include <iostream>
#define TRYIT false
using namespace std;

class Furniture{/*from w ww.  ja  v a2  s  .c  om*/
  public:
    Furniture(int w) : weight(w) {}
    int weight;
};

class Bed : public Furniture
{
  public:
    Bed(int weight) : Furniture(weight) {}
    void sleep(){ 
       cout << "Sleep" << endl; 
    }
};

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

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

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

    cout << "Weight = " << ss.weight << endl;

    SleeperSofa* pSS = &ss;
    Sofa* pSofa = (Sofa*)pSS;
    Furniture* pFurniture = (Furniture*)pSofa;
    cout << "Weight = " << pFurniture->weight << endl;

    return 0;
}



PreviousNext

Related