Qn.Implement a super class Person. Make two classes, Student and Instructor, that inherit from
Person. A person has a name and a year of birth. A student has a course and an instructor has a
salary. Write the class definitions, the constructors, and the functions for all classes. Supply a test
program that test these classes and member functions.
#include<iostream>
#include <string>
using namespace std;
class Person{
private:
string name;
int yearBirth;
public:
Person(){}
Person(string name,int yearBirth){
this->name=name;
this->yearBirth=yearBirth;
}
void display(){
cout<<"Name: "<<name<<"\n";
cout<<"Year of birth: "<<yearBirth<<"\n";
}
};
class Student:public Person{
private:
string course;
public:
Student(){}
Student(string name,int yearBirth,string course):Person(name,yearBirth){
this->course=course;
}
void display(){
cout<<"Student: \n";
Person::display();
cout<<"Course: "<<course<<"\n";
}
};
class Instructor:public Person{
private:
double salary;
public:
Instructor(){}
Instructor(string name,int yearBirth,double salary):Person(name,yearBirth){
this->salary=salary;
}
void display(){
cout<<"Instructor: \n";
Person::display();
cout<<"Salary: "<<salary<<"\n";
}
};
int main()
{
Student s("Peter", 18,"C++");
Instructor i("Mike", 18,1800);
s.display();
i.display();
system("pause");
return 0;
}
Comments
Leave a comment