Single level inheritance

Single level inheritance:

In single inheritance one class is derived from a parent class.In the fig class A is base class and class B is derived class. Hence inheritance works only in one direction. The derive class inherits all the elements of base class but the base class do not know anything about derive class

simple inheri

 

Example 1

#include<iostream.h>

#include<conio.h>

class empy

{

public: int eno;

char name[20],des[20];

void get()

{

cout<<“Enter a employee number:-“;

cin>>eno;

cout<<“Enter a employee name:-“;

cin>>name;

cout<<“Enter a designation-:”; cin>>des;

}

};

class salary:public empy

{

float bp1,hra1,da1,pf1,np1;

public: void get1()

{

cout<<“Enter a basic pay:-“;

cin>>bp1;

cout<<“Enter a Humen Resource Allowance:-“;

cin>>hra1;

cout<<“Enter a Dearness Allowance :-“;

cin>>da1;

cout<<“Enter a Profitablity Fund:-“;

cin>>pf1;

}

void calculate()

{

np1=bp1+hra1+da1-pf1;

}

void display()

{

cout<<eno<<“\t”<<name<<“\t”<<des<<“\t”<<bp1<<“\t”<<hra1<<“\t”<<da1<<“\t”<<pf1<<“\t”<<np1<<“\n”;

}

};

void main()

{

int j,n;

char ch;

salary s[10];

clrscr();

cout<<“Enter a number of employee:-“;

cin>>n;

for(i=0;i<n;i++)

{

s[j].get();

s[j].get1();

s[j].calculate();

}

cout<<“\ne_no \t e_name\t des \t bp1 \t hra1 \t da1 \t pf1 \t np1 \n”;

for(j=0;j<n;j++)

{

s[j].display();

}

getch();

}

Output
Enter a Number of employee:-1

Enter a employee No:- 1500

Enter a employee Name:- ramu

Enter a designation: Mang.

Enter a basic pay: 5000

Enter a HR allowance: 1000

Enter a Dearness allowance: 500

Enter a profitability Fund: 150

Example 2

#include<iostream.h>

#include<conio.h>

class base

{

public: int a1,b1;

public: void getdata();

};

class derive:public base

{

public: void getdata()

{

cout<<“enter the number”;

cin>>a1;

cout<<“enter the number”;

cin>>b1;

}

void putdata()

{

cout<<“\n enter first no “<<a1;

cout<<“\n enter 2nd number “<<b1;

}

};

void main()

{

derive s1; s1.getdata();

s1.putdata();

getch();

}

Output

enter the number 4

enter the number 5

enter first number 4

enter 2nd number  5

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *