Write
program to create a base class staff and derived class teacher,typist,officer and a sub derived class regular casual.
In this program we have familiar with the classes in the c++. In this we have used the inheritance in c++.Inheritance means we have create parent class
and child class of the parent class.parent class have the some properties we have to define by default similarly, child class have some properties and child and parent class don't have same properties but child class have all the properties of the parent class and have the additional has we define,In this we define parent class as employee and child class as manager in this sub-manager is the child class of manager. This program is very helpful for c++ devlopers.
Program Code:-
#include<iostream>
using
namespace std;
class
staff
{
public:
int c;
int sal;
int c;
int sal;
char name[30];
void get_data()
{
cout<<"Enter code name and
salary of staff:-";
cin>>c>>name>>sal;
}
void display_data()
{
cout<<"code:\t"<<c<<endl<<"name:\t"<<name<<endl<<"salary:\t"<<sal<<endl;
}
};
class
teacher : public staff
{
public:
char sub[10];
char pub[10];
char sub[10];
char pub[10];
};
class
typist : public staff
{
public:
int s;
int s;
};
class
officer : public staff
{
public:
char g;
char g;
};
class
regular : public typist
{
public:
};
class
casual : public typist
{
public:
int dailywage;
int dailywage;
};
int
main()
{
staff s;
teacher t;
typist ty;
officer o;
regular r;
casual c;
int n;
teacher t;
typist ty;
officer o;
regular r;
casual c;
int n;
do{
cout<<"choose any option given
below\n1.teacher\t2.typist\t3.officer:-";
cin>>n;
if(n==1)
{
cout<<"Enter teacher subject and
publication:-";
cin>>t.sub>>t.pub;
s.get_data();
s.display_data();
cout<<"subject:\t"<<t.sub<<endl<<"publication:\t"<<t.pub<<endl;
}
if(n==2)
{
cout<<"choose any option given
below\n.regular\t2.casual:-";
cin>>n;
cout<<"Enter speed of
typist:-";
cin>>ty.s;
if(n==1)
{
s.get_data();
s.display_data();
cout<<"speed:\t"<<ty.s<<endl;
}
if(n==2)
{
s.get_data();
s.display_data();
cout<<"speed:\t"<<ty.s<<endl;
}
}
if(n==3)
{
cout<<"Enter grade of
officer:-";
cin>>o.g;
s.get_data();
s.display_data();
cout<<"grade:\t"<<o.g<<endl;
}
cout<<"choose any one option\n1.continue\t2.exit:-";
cin>>n;
}while(n==1);
return 0;
}
Output:-
In this output we ask the user to enter any one of the option given below.The following options are teacher , typist, officer.In this teacher , typist, officer have all the properties of the base class staff.
The staff as following properties as like code,name,salary , but the teacher s additional properties subject and publication.similarly for remaining officer and typist.In the typist we have again two sub classes regular and casual.similarly its above statement is applied for this also.