Answer to Question #288904 in C++ for Habiba

Question #288904



Define a Person class containing Name, Age and Id as attributes and constructor, parameterized


constructor, setters, getters, display as member functions.Student – containing program and cgpa as data-members and constructor, parameterized


constructor and setters, getters and display as over-ridden functions Course – containing title, creditHours and prerequisite as data members. Constructor,


parameterized constructor, setters, getters, and display as member function. Relate this class


with Student class (student is registered into a course).


In main( ), create a pointer of the Person class and use it to create different types of persons


(staff, student and faculty). If the Person is a student and is registered in a course, display


function must display all the details of that student.




1
Expert's answer
2022-01-19T10:34:35-0500




#include <iostream>
#include <string>
using namespace std;




class Course{
private:
	string title;
	int creditHours;
	string prerequisite;
public:
	Course(){}
	Course(string title,int creditHours,string prerequisite){
		setTitle(title);
		setCreditHours(creditHours);
		setPrerequisite(prerequisite);
	}
	void setTitle(string title){
		this->title=title;
	}
	void setCreditHours(int creditHours){
		this->creditHours=creditHours;
	}
	void setPrerequisite(string prerequisite){
		this->prerequisite=prerequisite;
	}


	string getTitle(){
		return this->title;
	}
	int getCreditHours(){
		return this->creditHours;
	}
	string getPrerequisite(){
		return this->prerequisite;
	}




	virtual void display(){
		cout<<"Title: "<<title<<"\n";
		cout<<"CreditHours: "<<creditHours<<"\n";
		cout<<"Prerequisite: "<<prerequisite<<"\n";
	} 
};




class Person{
private:
	string Name;
	int Age;
	int Id;
public:


	Person(){}


	Person(string Name,int Age,int Id){
		setName(Name);
		setAge(Age);
		setId(Id);
	}


	void setName(string Name){
		this->Name=Name;
	}
	void setAge(int Age){
		this->Age=Age;
	}
	void setId(int Id){
		this->Id=Id;
	}


	string getName(){
		return this->Name;
	}
	int getAge(){
		return this->Age;
	}
	int getId(){
		return this->Id;
	}




	virtual void display(){
		cout<<"Name: "<<Name<<"\n";
		cout<<"Age: "<<Age<<"\n";
		cout<<"Id: "<<Id<<"\n";
	} 


};




class Student:public Person{
private:
	int cgpa;
	Course* selectedCourse;
public:


	Student(){}


	Student(string Name,int Age,int Id,int cgpa):Person(Name,Age,Id){
		setCgpa(cgpa);
	}


	void setCgpa(int cgpa){
		this->cgpa=cgpa;
	}


	void setCourse(Course* selectedCourse){
		this->selectedCourse=selectedCourse;
	}


	int getCgpa(){
		return this->cgpa;
	}


	Course* getCourse(){
		return this->selectedCourse;
	}




	void display(){
		Person::display();
		cout<<"Cgpa: "<<cgpa<<"\n";
		cout<<"Course: \n";
		selectedCourse->display();
	} 
};






int main() {


	Person* person;
	Student* student=new Student("Peter",19,54558,4);
	Course* selectedCourse=new Course("C++",5,"prerequisite.com");
	student->setCourse(selectedCourse);
	person=student;
	person->display();
	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