Headlines
Loading...
C++ programs -  HIERACHICAL INHERITANCE

C++ programs - HIERACHICAL INHERITANCE

 HIERACHICAL INHERITANCE

/* TO PRINT THE EMPLOYEE DETAILS */


#include <iostream.h>

#include <conio.h>

const int LEN = 80; //maximum length of names

class employee // Base class

{

private:

char name[LEN]; //employee name

unsigned long number; //employee number

public:

void getdata()

{

cout << "\n Enter name: "; cin >> name;

cout << " Enter number: "; cin >> number;

}

void putdata() const

{

cout << "\n Name: " << name;

cout << "\n Number: " << number;

}


};

class manager : public employee //manager class

{

private:

char title[LEN]; //"vice-president" etc.

double dues; //golf club dues

public:

void getdata()

{

employee::getdata();

cout << " Enter title: "; cin >> title;

cout << " Enter golf club dues: "; cin >> dues;

}

void putdata() const

{

employee::putdata();

cout << "\n Title: " << title;

cout << "\n Golf club dues: " << dues;

}


};

class scientist : public employee //scientist class

{


private:

int pubs; //number of publications

public:

void getdata()

{

employee::getdata();

cout << " Enter number of pubs: "; cin >> pubs;

}

void putdata() const

{

employee::putdata();

cout << "\n Number of publications: " << pubs;

}


};

class foreman : public employee //foreman class

{

private:

float quotas; //percent of quotas met successfully

public:

void getdata()

{

cout << "\n Enter quotas: "; cin >> quotas;

}

void putdata() const

{

cout << "\n Quotas: " << quotas;

}


};

void main()

{

employee e1;

manager m1;

foreman f1;

scientist s1;

clrscr();

cout << endl;

cout << "\nEnter data for Manager : ";

m1.getdata();

cout << endl;

cout << "\nEnter data for Scientist : ";

s1.getdata();

cout << endl;

cout << "\nEnter data for foreman : ";

f1.getdata();

cout << endl;


cout << "----------------------------------------";

cout << "\nData on Manager : ";

m1.putdata();

cout << endl;

cout << "\nData on Scientist : ";

s1.putdata();

cout << endl;

cout << "\nData on foreman : ";

f1.putdata();

cout << endl;

getch();

}


OUTPUT

Enter data for Manager :

Enter name: Nisha

Enter number: 30

Enter title: Asst

Enter golf club dues: 50000


Enter data for Scientist :

Enter name: Sunil

Enter number: 25

Enter number of pubs: 12


Enter data for foreman :

Enter quotas: 56

----------------------------------------

Data on Manager :

Name: Nisha

Number: 30

Title: Asst

Golf club dues: 50000

Data on Scientist :

Name: Sunil

Number: 25

Number of publications: 12

Data on foreman :

Quotas: 56


/* TO FIND AREA OF FIGURES */


SOURCE CODE

#include<iostream.h>

#include<conio.h>

class triangle // Base class

{

int base,ht; // base and height of triangle

public :

triangle() : base(0),ht(0){} //no args constructor

triangle(int x) // 1-arg constructor

{

base=ht=x;

}

triangle(int x,int y) // 2-args constructor

{

base=x;

ht=y;

}

void triarea() // to find area of the triangle

{

cout<<"\n\nArea of triangle = "<<0.5*base*ht;

}


};

class circle : public triangle //Derived class circle inherits from class triangle

{

int r; // radius of the circle

public :

circle() : r(0){}

circle(int x)

{

r=x;

}

circle(int x,int y,int z) : triangle(x,y)

// child uses values for itself and also passes it to the parent class

{

r=z;

}

void cirarea() // to find area of the circle

{

cout<<"\n\nArea of circle = "<<3.14*r*r;

}


};

class rectangle : public triangle //Derived class rectangle inherits from class triangle

{


int l,b; // length and breadth of the rectangle

public :

rectangle() : l(0),b(0){}

rectangle(int x)

{

l=b=x;

}

rectangle(int x,int y) : triangle(x,y)

// child uses values for itself and also passes it to the parent class

{

l=x;

b=y;

}

void rectarea() // to find area of the rectangle

{

cout<<"\n\nArea of rectangle = "<<l*b;

}


};

void main()

{

int k,l,m;

clrscr();

cout<<"\nTo calculate area for Rectangle and Triangle\n";

cout<<"\nEnter 2 values : "; // length,breadth for rectangle and base,height

for triangle

cin>>k>>l;

cout<<"\n\nTo calculate area for circle\n";

cout<<"\nEnter radius : "; //radius for the circle

cin>>m;

rectangle rt,rect(k,l);

rect.rectarea(); // calls rectarea from class rectangle

circle cir(k,l,m);

cir.cirarea(); // calls cirarea from class circle

cir.triarea(); // calls triarea from class triangle

getch();

}


OUTPUT

To calculate area for Rectangle and Triangle

Enter values : 5 10


To calculate area for circle

Enter radius : 5


Area of rectangle = 50

Area of circle = 78.5

Area of triangle = 25

*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***