Inheritance Example 4 – C++ Language

#include <iostream>

using namespace std;
class teacher
{
private:
string name;
public:
void getdata();
void display();
};
class teacher1 : public teacher
{
private:
int id;
public:
void getdata();
void display();
};
class teacher2 : public teacher1
{
private:
float phone_no;
public:
void getdata();
void display();
};
void teacher::getdata()
{
cout<<“enter name”<<endl;
cin>>name;
}
void teacher::display()
{
cout<<name<<endl;
}
void teacher1::getdata()
{
cout<<“enter id”<<endl;
cin>>id;
}
void teacher1::display()
{
cout<<id<<endl;
}
void teacher2::getdata()
{
cout<<“enter phone number”<<endl;
cin>>phone_no;
}
void teacher2::display()
{
cout<<phone_no<<endl;
}
int main()
{
teacher2 t;
t.teacher::getdata();
t.teacher::display();
t.teacher1::getdata();
t.teacher1::display();
t.getdata();
t.display();
return 0;
}