Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Person class, such that both Student and Faculty pointers can be stored in a sin

ID: 3778989 • Letter: P

Question

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();
}

//


Student :: Student()

{

ID = 0;

name = "";

GPA = 0;

gender = ' ';

}

Student :: Student(int ID, string name, double GPA, char gender)

{

this -> ID = ID;

this -> name = name;

this -> GPA = GPA;

this -> gender = gender;

}

void Student :: setStudent(int ID, string name, double GPA, char gender)

{

this -> ID = ID;

this -> name = name;

this -> GPA = GPA;

this -> gender = gender;

}

int Student :: getID()

{

return ID;

}

string Student :: getName()

{

return name;

}

double Student :: getGPA()

{

return GPA;

}

char Student :: getGender()

{

return gender;

}

void Student :: print()

{

cout << "ID : " << ID << endl;

cout << "Name : " << name << endl;

cout << "GPA : " << GPA << endl;

cout << "Gender : " << gender << endl;

}

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();
Person(string name);
virtual string toString() = 0;
protected:
string name;
};
void printList(vector<Person*> list);
Person::Person()
{

}
Person::Person(string name)
{
this->name = name;
}

class Faculty : public Person
{
public:
Faculty(string name, string department);
string toString();
private:
string department;
};
Faculty::Faculty(string name, string department) : Person(name)
{
this->department = department;
}
string Faculty::toString()
{
stringstream ss;
ss << "Name: " << name << endl;
ss << "Department: " << department << endl;
return ss.str();
}

class Student : public Person
{
public:
Student(string name, double GPA);
Student();
Student(int ID, string name, double GPA, char gender);
void setStudent(int ID, string name, double GPA, char gender);
int getID();
string toString();
string getName();
double getGPA();
char getGender();
void print();
private:
double GPA;
int ID;
char gender;
};
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();
}
//

Student :: Student()
{
ID = 0;
name = "";
GPA = 0;
gender = ' ';
}
Student :: Student(int ID, string name, double GPA, char gender)
{
this -> ID = ID;
this -> name = name;
this -> GPA = GPA;
this -> gender = gender;
}
void Student :: setStudent(int ID, string name, double GPA, char gender)
{
this -> ID = ID;
this -> name = name;
this -> GPA = GPA;
this -> gender = gender;
}
int Student :: getID()
{
return ID;
}
string Student :: getName()
{
return name;
}
double Student :: getGPA()
{
return GPA;
}
char Student :: getGender()
{
return gender;
}
void Student :: print()
{
cout << "ID : " << ID << endl;
cout << "Name : " << name << endl;
cout << "GPA : " << GPA << endl;
cout << "Gender : " << gender << endl;
}
int main()
{
int index = 0;
string type;
string name;
Person *person;
vector<Person*> my_list;
for(index = 0 ;index<2; index++){
cout << "Enter if the type is student/faculty : ";
cin >> type;
cout << "Enter name : ";
cin >> name;
if(type == "student"){
double gpa;
cout << "Enter GPA : ";
cin >> gpa;
my_list.push_back(new Student(name, gpa));
} else if(type == "faculty"){
string department;
cout << "Enter department name : ";
cin >> department;
my_list.push_back(new Faculty(name, department));
} else{
cout << "You entered incorrectly. Please enter type as either student or faculty";
index--;
}
}
printList(my_list);
return 0;
}
void printList(vector<Person*> list)
{
for (int i = 0; i < list.size(); i++)
{
cout << list[i]->toString();
}
}

Output :

kumar@kumar-VassarLabs:~/Desktop/cpp$ ./a.out
Enter if if the type is student/faculty : student
Enter name : Kumar
Enter GPA : 2
Enter if if the type is student/faculty : faculty
Enter name : Kumar
Enter department name : cse
Name: Kumar
GPA: 2
Name: Kumar
Department: cse
kumar@kumar-VassarLabs:~/Desktop/cpp$

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote