You have been given a Person class and a Student class which inherits from it. C
ID: 3682122 • Letter: Y
Question
You have been given a Person class and a Student class which inherits from it. Create a simple Faculty class that also inherits from the Person class, such that both Student and Faculty pointers can be stored in a single vector. Your Faculty class should have a string for the name of the faculty member, and a string for the department they belong to. Your class should also implement the toString function required by the Person class. Follow the same format as the Student toString function. When your program is run, you will be given 2 sets of 3 inputs which you should use to create the appropriate objects: a string representing the type ("student" or "faculty"), a string for the name of the person, and either a double GPA for a student, or a string representing a department for a faculty. Create pointers to the appropriate objects and store them both in a single vector called my_list.
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Person
{
public:
Person(string name);
virtual string toString() = 0;
protected:
string name;
};
void printList(vector<Person*> list);
Person::Person(string name)
{
this->name = name;
}
class Student : public Person
{
public:
Student(string name, double GPA);
string toString();
private:
double GPA;
};
Student::Student(string name, double GPA) : Person(name)
{
this->GPA = GPA;
}
string Student::toString()
{
stringstream ss;
ss << "Name: " << name << endl;
ss << "GPA: " << GPA << endl;
return ss.str();
}
//
// Create your faculty class here
//
int main()
{
//Code here
printList(my_list);
return 0;
}
void printList(vector<Person*> list)
{
for (int i = 0; i < list.size(); i++)
{
cout << list[i]->toString();
}
}
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
class Person
{
public:
Person(string name);
virtual string toString() = 0;
protected:
string name;
};
void printList(vector<Person*> list);
Person::Person(string name)
{
this->name = name;
}
class Student : public Person
{
public:
Student(string name, double GPA);
string toString();
private:
double GPA;
};
Student::Student(string name, double GPA) : Person(name)
{
this->GPA = GPA;
}
string Student::toString()
{
stringstream ss;
ss << "Name: " << name << endl;
ss << "GPA: " << GPA << endl;
return ss.str();
}
// Faculty class created here
class Faculty : public Person
{
public:
Faculty(string name, string deptName);
string toString();
private:
string deptName;
};
Faculty::Faculty(string name, string deptName) : Person(name)
{
this->deptName = deptName;
}
string Faculty::toString()
{
stringstream ss;
ss << "Name: " << name << endl;
ss << "Department Name: " << deptName << endl;
return ss.str();
}
int main()
{
//Coded here
vector<Person*> my_list;
int i=0;
for(i=0;i<2;++i){ //Loop for 2 objects to be created, input data through terminal
string str,name,dept;
double gpa;
cin>>str; //Enter student or faculty
cin>>name; //Enter name of student or faculty as the case be
if(str=="student"){
cin>>gpa; //Enter GPA of student
Person *student=new Student(name,gpa); //pointer to object of Student
my_list.push_back(student);
}
else if(str=="faculty"){
cin>>dept; //Enter department name of faculty
Faculty *faculty=new Faculty(name,dept); //Pointer to object of Faculty
my_list.push_back(faculty);
}
}
printList(my_list);
return 0;
}
void printList(vector<Person*> list)
{
for (int i = 0; i < list.size(); i++)
{
cout << list[i]->toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.