Using inheritance in C++
Posted by mark123
Last Updated: October 16, 2012
//Furniture is a based class that has a derived class of Chair. Chair is a derived class that has a derived class called Rocking Chair.
//The following code shows how the hierarchy of the relationship/inheritance takes  
//place.
#ifndef FURNITUREH
#define FURNITUREH
#include<iostream>
#include<string>
using namespace std;
class Furniture
{
protected:
string material;
public:
Furniture()
{
material="none in Furniture";
}

virtual void info()=0;
};
#endif

#ifndef CHAIRH
#define CHAIRH
#include "Furniture.h"
#include <iostream>
using namespace std;
class Chair:public Furniture
{
protected:
int numfeet;
public: Chair()// Constructor for Chair Class
{
material="Mahagony";
numfeet=4;
}
void info()
{
      cout<<"Material of Chair is"<<material
 <<"number of feet is"<<numfeet<<endl;
}

};
#endif
#ifndef ROCKINGCHAIRH
#define ROCKINGCHAIRH
#include "Furniture.h"
#include "Chair.h"
#include "Furniture.h"
#include <iostream>
class RockChair:Chair
{
private:
double tiltangle;
public:
RockChair()
{
     material="Mahagony";
numfeet=4;
tiltangle=45.78;
cout<<"Call from Rocking Chair constructor"<<endl;
}

     void info()
{  
Chair::info();//Chair info method is inherited by Rocking Chair.
     
 <<"Has a tilt angle of"<<tiltangle<<endl;
}
};

#include "Furniture.h"
#include "Chair.h"
#include "Rocking Chair.h"
#include <iostream>
#include <cstdlib>
int main()
{
cout << "\nCreating a Chair object" << endl;
Chair *cObj = new Chair;
cObj->info();
cout << "\nCreating a RockingChair object" << endl;
RockChair *rObj = new RockChair;
rObj->info();
 
 

system("pause");
return 0;
}




Related Content