single inheritance example with c++

Create a base class teacher with two member data name and number of students,one public function for input.one derived the class principal and publicly one data member school name. one print function both the classes and print data member of both classes.

.

coding :

#include<iostream>
using namespace std;
class teacher
{
char name[50];
int numberofstudents;
public:
void getdata()
{
cout<<"enter name";
cin>>name;
cout<<"enter no.student";
cin>>numberofstudents;
}
void display()
{
cout<<"\n name:"<<name;
cout<<"\n totalno.ofstudents"<<numberofstudents;
}
};
class principal : public teacher
{
char sname[50];
public:
void getdata()
{
     cout<<"school name";
cin>>sname;
}
void display()
{
cout<<"your school name\n"<<sname;
}
};
int main()
{
principal p;
     cout<<"enter the data\n";
p.teacher :: getdata();
p.getdata();
cout<<"display data\n";
p.display();
p.teacher :: display();
return 0;
}


output:



0 comments:

Copyright © 2013 free coding