Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

10.8 Prep Lab: Polymorphic class list You are teaching a class which contains a mix of undergraduate students and graduate students. Undergraduate students are objects

10.8 Prep Lab: Polymorphic class list

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 option: Create (using new) a Student object or a GradStudent object and add its pointer to the vector of Student pointers called classMembers.

Print option: 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 virtual 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

///////////////////////// end of example

//////////////////////// Below is the snippet of code given from the challenge

#include

#include

#include

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 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;

// 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;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions