// The Person class is modified to make getName // a virtual function. class Per
ID: 665223 • Letter: #
Question
// The Person class is modified to make getName
// a virtual function.
class Person{
protected:
string name;
public:
Person() { setName(""); }
Person(string pName) { setName(pName); }
void setName(string pName) { name = pName; }
// Virtual function.
virtual string getName() { return name; }
};
class Student:public Person
{
private:
Discipline major;
Person *advisor;
public:
Student(string sname, Discipline d, Person *adv)
: Person(sname)
{
major = d;
advisor = adv;
}
void setMajor(Discipline d) { major = d; }
Discipline getMajor() { return major; }
void setAdvisor(Person *p) { advisor = p; }
Person *getAdvisor() { return advisor; }
};
class Faculty:public Person
{
private:
Discipline department;
public:
Faculty(string fname, Discipline d) : Person(fname)
{
department = d;
}
void setDepartment(Discipline d) { department = d; }
Discipline getDepartment( ) { return department; }
};
class TFaculty : public Faculty
{
private:
string title;
public:
TFaculty(string fname, Discipline d, string title)
: Faculty(fname, d)
{
setTitle(title);
}
void setTitle(string title) { this->title = title; }
// Virtual function
virtual string getName( )
{
return title + " " + Person::getName();
}
};
// This demonstrates the polymorphic behavior
// of classes with virtual functions.
#include "inheritance.h"
#include <iostream>
using namespace std;
int main()
{
// Create an array of Person objects.
const int NUM_PEOPLE = 5;
Person *arr[NUM_PEOPLE] =
{
new TFaculty("Indiana Jones", ARCHEOLOGY, "Dr."),
new Student("Thomas Cruise", COMPUTER_SCIENCE, NULL),
new Faculty("James Stock", BIOLOGY),
new TFaculty("Sharon Rock", BIOLOGY, "Professor"),
new TFaculty("Nicole Eweman", ARCHEOLOGY, "Dr.")
};
// Print the names of the Person objects.
for (int k = 0; k < NUM_PEOPLE; k++)
{
cout << arr[k]->getName() << endl;
}
return 0;
}
Polymorphism and Virtual Functions
Get the source code and Compile main.cpp and inheritance.h as a project and Add a retiree faculty class with retirement date as a data member.
class RFaculty : public Faculty
{
}
c++ and output
Explanation / Answer
main.cpp
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.