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

You have been given a Person class and a Student class which inherits from it. C

ID: 3681276 • 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

Hey heres the whole code.

#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: " << Person::name << endl;
   ss << "GPA: " << GPA << endl;

   return ss.str();
}

class Faculty : public Person
{
public:
   Faculty(string name, string dept);
   string toString();
   string dept;
};

Faculty::Faculty(string name, string dept) : Person(name)
{
   this->dept = dept;

}

string Faculty::toString()
{
   stringstream ss;
   ss << "Name: " << Person::name << endl;
   ss << "Department: " << dept << endl;

   return ss.str();
}


int main()
{
   char str;
   int n = 0;
  
   vector<Person*> my_list;
  
   while (n != 2)
   {
       cout << "student(s) or faculty(f)?:" << endl;
       cin >> str;
       if (str == 's')
       {
           string sname;
          
           double gpa;
           cout << "enter name and gpa: ";
           cin >> sname >> gpa;
           my_list.push_back(new Student(sname,gpa));
           n++;
       }
       if (str == 'f')
       {
           string fname, dep;
           cout << "Faculty name? : ";
           cin >> fname;
           cout << "Department? : ";
           cin >> dep;
           my_list.push_back(new Faculty(fname, dep));
           n++;
       }
       printList(my_list);
   }
   getchar();
   printList(my_list);

   return 0;
}

void printList(vector<Person*> list)
{
   for (unsigned i = 0; i < list.size(); i++)
   {
       cout << list[i]->toString();
   }
}

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