Answered Essay: 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;
}

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?