Answer to Question #297454 in C++ for Rajen

Question #297454

details for students and staff using inheritance. Student details: name, address, percentage marks. Staff details: name, address, salary.

1
Expert's answer
2022-02-18T01:21:03-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog