Answer to Question #178790 in C++ for Ahmed

Question #178790

Base class has two pure virtual functions 

getData() and isoutstanding(). Parent class should include two member functionsto input() and display(),the name of personnel. Code includes two derived classes, student and instructor . Derived classes each contain a function called get () and outstanding(). get () function of Student class should input name of personnel and asks user to input GPA whereas outstanding function determines either GPA>3 or not.Similarly, get () function of instructor class should input name of personnel and asks user to input no of publications whereas Outstanding function determines either the no. of publications >50 or not. Student and instructor objects are casted into the person class type through array of pointers. Ask user either he is student/instructor, then get his data using get () and your program continues to ask to enter the data until the last personnel enters data. Once data entered, print names of all personnel along their outstanding.


1
Expert's answer
2021-04-06T12:51:07-0400
#include <iostream>
#include <string>


using namespace std;


class Person{
private:
	string name;
public:
	//Constructor
	Person(){}
	~Person(){}


	virtual void get(){
		cout<<"Enter name: ";
		getline(cin,name);
		
	}
	virtual bool outstanding(){
		return true;
	}
	virtual void display(){
		cout<<"The name: "<<name<<"\n";
	}
};
class Student:public Person{
private:
	int GPA;
public:
	//Constructor
	Student(){}
	~Student(){}


	void get(){
		Person::get();
		cout<<"Enter GPA: ";
		cin>>GPA;
		cin.ignore();
	}
	bool outstanding(){
		return (GPA>3);
	}
	void display(){
		Person::display();
		if(outstanding()){
			cout<<"GPA > 3\n";
		}else{
			cout<<"GPA: "<<GPA<<"\n";
		}
	}
};


class Instructor:public Person{
private:
	int publications;
public:
	//Constructor
	Instructor(){}
	~Instructor(){}


	void get(){
		Person::get();
		cout<<"Enter no of publications: ";
		cin>>publications;
		cin.ignore();
	}
	bool outstanding(){
		return (publications>3);
	}
	void display(){
		Person::display();
		if(outstanding()){
			cout<<"No of publications > 50\n";
		}else{
			cout<<"No of publications: "<<publications<<"\n";
		}
	}
};


int main (){
	//array of pointers
	Person** persons=new Person*[5];
	
	for(int i=0;i<5;i++){
		int choice=0;
		while(choice<1 || choice>2){
			cout<<"1. Add a new student\n";
			cout<<"2. Add a new instructor\n";
			cout<<"Your choice: ";
			cin>>choice;
			cin.ignore();
		}
		if(choice==1){
			Student* student=new Student();
			student->get();
			persons[i]=student;
		}
		if(choice==2){
			Instructor* instructor=new Instructor();
			instructor->get();
			persons[i]=instructor;
		}
	}
	for(int i=0;i<5;i++){
		persons[i]->display();
		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
APPROVED BY CLIENTS