20120113

How to use Inheritance in C++

//Program in cpp to demonstrate inheritance, inherit.cpp

#include<iostream>
using namespace std;
class employee
{
protected:
int empid;
public:
void getempid(int a)
{
empid=a;
}
void showempid()
{
cout<<"********* Details of employee**********"<<"\n";
cout<<"Emp No is:"<<empid<<"\n";
}
};
class hike:public virtual employee
{
protected:
float hike1;
float hike2;
public:
void getHike(float h1, float h2)
{
hike1=h1;//line 43
hike2=h2;
}
void showHike(void)
{
cout<<"Hike in year 1 is :"<<hike1<<"\n";
cout<<"Hike in year 2 is:"<<hike2<<"\n";
}
};
class promotion:public virtual employee
{
protected:
float promotion_score;
public:
void getPromotion(float s)
{
promotion_score=s;
}
void showPromotionScore(void)
{
cout<<"Score in promotion is:"<<promotion_score<<"\n";
}
};

class result:public hike, public promotion
{
protected:
float total;
public:
//total=hike1+hike2+promotion_score;
void display();
};
void result::display()
{
total=hike1+hike2+promotion_score;
 showempid();
 showHike();
 showPromotionScore();
 cout<<"Total score is :"<<total<<"\n";
}
int main()
{
result Raheem;
Manoj.getempid(111);
Manoj.getHike(9.10, 9.5);
Manoj.getPromotion(8.2);
Manoj.display();
return 0;
}


How to use Inheritance in Cpp, C++, C plus plus with example

Previous                             Home                               Next

No comments:

Post a Comment