Posted by MindBreaker at 3:02 AM
Object :
Create a three class, Teacher,Student, and parent class Person and inherit them using the simple concept of inheritance.
Source Code
#include <iostream>
#include <string>
using namespace std;
class person
{
private:
int age;
char name[25];
public:
person()
{}
void input(int a,char n[25])
{
age=a;
strcpy(name,n);
cout<<"Age : "<<age<<"Name : "<<name<<endl;
}
};
////////////////////////////////////////////////////////////////
class student:public person
{
int semester;
int roll;
public:
void getinput(int s,int r)
{
semester=s;
roll=r;
cout<<"Semester : "<<semester<<" Roll number : "<<roll<<endl;
}
/////////////////////////////////////////////////////////////
};class teacher:public person
{
int coursecode;
int dept;
public:
void setinput(int s,int r)
{
coursecode=s;
dept=r;
cout<<"Course code : "<<coursecode<<" Department number : "<<dept<<endl;
}
};
/////////////////////////////////////////////////////////////////
void main()
{
char select;
int ag,rol,sst,dept,ccd;
char name[25];
cout<<"Enter 1 for Student"<<endl;
cout<<"Enter 2 for Teacher"<<endl;
cin>> select;
switch(select)
{
case '1':
{
student stdent;
cout<<"Enter name"<<endl;
fflush(stdin);
cin.getline(name,25);
cout<<"Enter age"<<endl;
cin>>ag;
cout<<"Enter student roll number"<<endl;
cin>>rol;
cout<<"Enter student semester number"<<endl;
cin>>sst;
stdent.input(ag,name);
stdent.getinput(sst,rol);
break;
}
case '2':
{
teacher teach;
cout<<"Enter name"<<endl;
fflush(stdin);
cin.getline(name,25);
cout<<"Enter age"<<endl;
cin>>ag;
cout<<"Enter Deptment number"<<endl;
cin>>dept;
cout<<"Course code"<<endl;
cin>>ccd;
teach.input(ag,name);
teach.setinput(ccd,dept);
break;
}
}
}
Output:
Create a three class, Teacher,Student, and parent class Person and inherit them using the simple concept of inheritance.
Source Code
#include <iostream>
#include <string>
using namespace std;
class person
{
private:
int age;
char name[25];
public:
person()
{}
void input(int a,char n[25])
{
age=a;
strcpy(name,n);
cout<<"Age : "<<age<<"Name : "<<name<<endl;
}
};
////////////////////////////////////////////////////////////////
class student:public person
{
int semester;
int roll;
public:
void getinput(int s,int r)
{
semester=s;
roll=r;
cout<<"Semester : "<<semester<<" Roll number : "<<roll<<endl;
}
/////////////////////////////////////////////////////////////
};class teacher:public person
{
int coursecode;
int dept;
public:
void setinput(int s,int r)
{
coursecode=s;
dept=r;
cout<<"Course code : "<<coursecode<<" Department number : "<<dept<<endl;
}
};
/////////////////////////////////////////////////////////////////
void main()
{
char select;
int ag,rol,sst,dept,ccd;
char name[25];
cout<<"Enter 1 for Student"<<endl;
cout<<"Enter 2 for Teacher"<<endl;
cin>> select;
switch(select)
{
case '1':
{
student stdent;
cout<<"Enter name"<<endl;
fflush(stdin);
cin.getline(name,25);
cout<<"Enter age"<<endl;
cin>>ag;
cout<<"Enter student roll number"<<endl;
cin>>rol;
cout<<"Enter student semester number"<<endl;
cin>>sst;
stdent.input(ag,name);
stdent.getinput(sst,rol);
break;
}
case '2':
{
teacher teach;
cout<<"Enter name"<<endl;
fflush(stdin);
cin.getline(name,25);
cout<<"Enter age"<<endl;
cin>>ag;
cout<<"Enter Deptment number"<<endl;
cin>>dept;
cout<<"Course code"<<endl;
cin>>ccd;
teach.input(ag,name);
teach.setinput(ccd,dept);
break;
}
}
}
Output: