details for students and staff using inheritance. Student details: name, address, percentage marks. Staff details: name, address, salary.
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string name;
string address;
public:
Person(){}
virtual void get(){
cout<<"Enter the person name: ";
getline(cin,name);
cout<<"Enter the person address: ";
getline(cin,address);
}
virtual void display(){
cout<<"The person name: "<<name<<"\n";
cout<<"The person address: "<<address<<"\n";
}
};
class Student:public Person{
private:
double percentageMarks;
public:
Student (){}
virtual void get(){
Person::get();
cout<<"Enter the student percentage marks: ";
cin>>percentageMarks;
}
virtual void display(){
Person::display();
cout<<"The student percentage marks: "<<percentageMarks<<"\n";
}
};
class Staff :public Person{
private:
float salary;
public:
Staff(){}
virtual void get(){
Person::get();
cout<<"Enter the staff salary: ";
cin>>salary;
}
virtual void display(){
Person::display();
cout<<"The staff salary: "<<salary<<"\n";
}
};
int main(){
Person* persons[2];
Staff* staff=new Staff();
staff->get();
cout<<"\n";
fflush(stdin);
Student* student=new Student();
student->get();
persons[0]= staff;
persons[1]= student;
cout<<"\n";
for(int i=0;i<2;i++){
//display the information about the persons using Polymorphism
persons[i]->display();
cout<<"\n";
delete persons[i];
}
system("pause");
return 0;
}
Comments
Leave a comment