Inheritance : Hierarchical

#include <iostream.h>
#include <string.h>
#include <conio.h>
class Person
{
protected :
char name[30];
int age;
public :
void getPersonData( char *n, int a)
{
strcpy(name,n);
age = a;
}
};
class Manager : public Person
{
protected :
char department[30];
public :
void getDepartment( char * dpt)
{
Strcpy(department, dpt)
}
void showDetails( )
{
cout<<”Manager details :\n”;
cout<<”Name :”<<name<<”\n”;
cout<<”Age :”<<age<<”\n”;
cout<<”Department :”<<department<<”\n”;
}
};
class Scientist : public Person
{
private :
int publications;
public :
void getPublications( int pub)
{
publications = pub;
}
void showDetails( )
{
cout<<”Scientist details :\n”;
cout<<”Name :”<<name<<”\n”;
cout<<”Age :”<<age<<”\n”;
cout<<”Publications :”<<publications<<”\n”; }
};
void main( )
{
Manager m1;
Scientist s1;
m1.getPersonData(“Kumar”, 36);
m1.getDepartment(“Sales”);
s1.getPersonData(“Krishna”, 42);
s1.getPublications(23);
m1.showDetails( );
s1.showDetails( );
}
Output :
Manager details :
Name : Kumar
Age : 36
Department : Sales
Scientist details :
Name : Krishna
Age : 42
Publications : 23

0 comments: