Write a program that implements the concept of inheritance. For example: in the
ID: 3938657 • Letter: W
Question
Write a program that implements the concept of inheritance. For example: in the following scenario. Air university is inherited by both student and instructor class In main () you will enter the information from each person who want to get admission/join Air University. If he is a student then will save all his information (e.g. gpa, enroll course, any paid fees) and so for instructor. Second step is to display all the information accordingly if user chooses a choice to show all students/teacher information.Explanation / Answer
#include <iostream>
using namespace std;
class AirUniversity //base class
{
private:
int ID;
public:
void id(int ID)
{
this->ID = ID;
}
int getid()
{
return ID;
}
};
class Student:public AirUniversity //student class derived from AirUniversity class
{
private:
float GPA,amount;
string course;
public:
void gpa(float GPA)
{
this->GPA = GPA;
}
void enroll(string course)
{
this->course = course;
}
void pay(float amount)
{
this->amount = amount;
}
void display()
{
cout<<" Student Information";
cout<<" Id : "<<getid()<<" GPA :"<<GPA<<" Course name : "<<course<<" Fees : "<<amount;
}
};
class Instructor:public AirUniversity //Instructor class derived from AirUniversity class
{
private:
string Grade,course;
float pay;
public:
void grade(string Grade)
{
this->Grade = Grade;
}
void teach(string course)
{
this->course = course;
}
void getPaid(float pay)
{
this->pay = pay;
}
void display()
{
cout<<" Instructor Information";
cout<<" ID : "<<getid()<<" Grade :"<<Grade<<" Course to teach : "<<course<<" Salary : "<<pay;
}
};
int main()
{
Student s; //student class object
Instructor i; //Instructor class object
float GPA,amount,pay;
string course,Grade;
int ID,option;
cout<<" Enter student information";
cout<<" Enter student id";
cin>>ID;
s.id(ID);
cout<<" Enter GPA";
cin>>GPA;
s.gpa(GPA);
cout<<" Enter course name to enroll";
cin>>course;
s.enroll(course);
cout<<" Enter amount to pay";
cin>>amount;
s.pay(amount);
cout<<" Enter Instructor information";
cout<<" Enter Instructor id";
cin>>ID;
i.id(ID);
cout<<" Enter grade of instructor";
cin>>Grade;
i.grade(Grade);
cout<<" Enter course to teach";
cin>>course;
i.teach(course);
cout<<" Enter pay for instructor";
cin>>pay;
i.getPaid(pay);
cout<<" Do you want to display information about student or instructor <1-Student,2-Instructor>";
cin>>option;
if(option==1)
s.display();
else if(option ==2)
i.display();
else
cout<<" Invalid option";
return 0;
}
output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.