Question & Answer: You are teaching a class which contains a mix of undergraduate students and graduate students. Undergraduate students are objects…..

Polymorphic class list (C++)

You are teaching a class which contains a mix of undergraduate students and graduate students. Undergraduate students are objects of the class Student. Graduate students are objects of the class GradStudent which is derived from the class Student. In your class you want to keep both types of students in a single list (a vector) without having to worry about which specific type the student is. GradStudents are Students but they have the additional data member of researchArea.

Your program will loop asking for one of three options (add, print, or quit).

Add user option: You will create (using new) a Student object or a GradStudent object and add its pointer to the vector classMembers.

Print user option: You will print the entire class last, using the appropriate ToStr member function. You should not test whether a particular pointer in the class list points to a Student or a GradStudent. The correct ToStr function will be called automatically if you have properly defined functions for ToStr.

We have provided most of the code already for you. You just need to fill in appropriate code at the locations marked with comments.

Note: In Student the name data member is declared as protected. We usually declare it is private meaning only member functions of Student could directly access name and all other functions would need to use an accessor function from Student to get access to that data. When declared as protected, that means it is also directly accessible by member functions in derived classes, but invisible to all other functions.

Here is an example of what a run would look like:

Option: add
Name: Mary
If grad student enter a one word research area, else enter "NO": MachineLearning

Option: add
Name: Bob
If grad student enter a one word research area, else enter "NO": NO

Option: print
Mary is a graduate student researching the area of MachineLearning.
Bob is an undergraduate student.

Option: quit

Given code:

#include <iostream>

#include <string>

#include <vector>

using namespace std;

class Student {

public:

Student(string initialName = “No Name”) {

name = initialName;

}

// Enter definition for a virtual ToStr member function here.

protected:

string name;

};

class GradStudent : public Student {

public:

// Enter definition for a paramteriezed contructor function here that sets both data members.

// Enter definition for a virtual ToStr member function here.

private:

string researchArea;

};

int main() {

string usrOption;

string inpName;

string inpResearch;

vector<Student*> classMembers;

do {

cout << “Option: “;

cin >> usrOption;

cout << endl;

if (usrOption == “print”) {

// Enter code here to print each member pointed to from classMembers using their respective ToStr member functions.

}

}

else if (usrOption == “add”) {

cout << “Name: “;

cin >> inpName;

cout << endl;

cout << “If grad student enter a one word research area, else enter “NO”: “;

cin >> inpResearch;

cout << endl;

if (inpResearch == “NO”) {

// Enter code here to create new objects (using new) for either a Student or a GradStudent

// and add the pointer to the classMember vector.

}

}

} while (usrOption != “quit”);

return 0;

}

Expert Answer

 

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

class Student {
public:
Student(string initialName = “No Name”) {
name = initialName;
}

virtual void ToStr(){
cout << name << ” is undergraduate student” << endl;
}

protected:
string name;
};

class GradStudent : public Student {
public:
GradStudent(string nm, string ar): Student(nm){

researchArea = ar;
}

void ToStr(){
cout << name << ” is graduate student researching the area of ” << researchArea << endl;
}

private:
string researchArea;
};

int main() {
string usrOption;
string inpName;
string inpResearch;
Student *stu;
vector<Student*> classMembers;

do {
cout << “Option: “;
cin >> usrOption;
cout << endl;
if (usrOption == “print”) {
for (int i = 0; i<classMembers.size(); i++){
classMembers[i]->ToStr();
}
}
else if (usrOption == “add”) {
cout << “Name: “;
cin >> inpName;
cout << endl;
cout << “If grad student enter a one word research area, else enter “NO”: “;
cin >> inpResearch;
cout << endl;
if (inpResearch != “NO”) {
classMembers.push_back(new GradStudent(inpName,inpResearch));
}
else {
classMembers.push_back(new Student(inpName));
}
}

} while (usrOption != “quit”);
return 0;
}

Still stressed from student homework?
Get quality assistance from academic writers!