Answered Essay: Background: C++

Background: C++

Add and Drop:

Programming Language: C++

In this lab you will allocate and free memory in a simple program where you can add or drop students from a class. We supply code for a simple student class with data members name and GPA. In main we declare a vector of pointers to the student class. You will fill in two sections of code.

1) You will define a student member function called ToString which returns a string containing basic student information. For a student named Jane with a GPA of 3.76 the string returned would be:

Jane has a GPA of 3.8

You will create the string using an ostringstream from sstream. Note that you need to setprecision to 1 for that stream.

2) You will write the main function as specified below.

Specification for main – “Add and Drop”

Menu

Implement a looping menu which has the options shown in the example below (add, drop, print, and quit). We assume throughout this program that users will always enter reasonable valid inputs, and thus you do not need to test for inappropriate input. Of course, in real code, we would never assume that, but since you have tested inputs many times in other labs, we make this simplifying assumption.

Add student

Query a name (assume a one-word name) and GPA and create a new student object (using “new”). Then add the pointer to that object to the students vector.

Print

Print each student using the “ToString” method for the basic student info. For each student also print an index number as shown below.

Drop student

Query an index number and erase the corresponding student pointer from the students vector. Before you erase the pointer you must delete the actual object in memory (using “delete”). Otherwise your program will have a memory leak.

Remember that your vector of pointers resides together on the stack of your main function. However, the objects you create with “new,” and point to from your vector, reside in arbitrary locations on the heap. Thus, it is essential that you free up the object memory in the heap with “delete.”

Execution Example

Enter Option: add
Student name: Barbara
Barbara's GPA: 3.88

Enter Option: add
Student name: George
George's GPA: 3.414

Enter Option: print
0: Barbara has a GPA of 3.9
1: George has a GPA of 3.4

Enter Option: drop
Index of student to drop: 1

Enter Option: print
0: Barbara has a GPA of 3.9

Enter Option: quit

Given code:

#include <iostream>

#include <string>

#include <vector>

#include <sstream>

#include <iomanip>

using namespace std;

class Student {

public:

Student(string name = “not intialized”, double GPA = 0.0);

void SetName(string studentName);

void SetGPA(double personName);

string ToString() const;

private:

string name;

double GPA;

};

Student::Student(string initialName, double initialGPA) {

name = initialName;

GPA = initialGPA;

}

void Student::SetName(string inputName) {

name = inputName;

}

void Student::SetGPA(double newGPA) {

GPA = newGPA;

}

//Insert the ToString member function here

int main() {

vector<Student*> students;

//Insert your code for main here

return 0;

}

Expert Answer

 

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>

using namespace std;

class Student {
public:
        Student(string name = "not intialized", double GPA = 0.0);
        void SetName(string studentName);
        void SetGPA(double personName);
        string ToString() const;
        void PrintItem();
private:
        string name;
        double GPA;
};

Student::Student(string initialName, double initialGPA) {
        name = initialName;
        GPA = initialGPA;
}

void Student::SetName(string inputName) {
        name = inputName;
}

void Student::SetGPA(double newGPA) {
        GPA = newGPA;
}

//Insert the ToString member function here FIXME**********************************
string Student::ToString() const {
   string AllStudentInfo = "";
   ostringstream StudentInfoOSS;
   
   return AllStudentInfo;
}

void Student::PrintItem() {
   cout << ": " << this->name << " has a GPA of " << fixed << setprecision(1)
        << this->GPA << endl;
   return;
}
//**********************************************************************************
void PrintAllItems(vector<Student*> students) { //FIXME
   int unsigned i = 0; // Loop index
   
   // For each item call class member function to print
   for (i = 0; i < students.size(); ++i) {
      cout << i;
      (*students.at(i)).PrintItem();
   }
   return;
}


int main() {
        vector<Student*> students;
   
   Student* newStudent = 0;
   Student* tmp = 0;
   
        //Insert your code for main here
        int runProgram = 1;
        string userOption = "";
        string studentName = "";
        double studentGPA = 0.0;
        int listPosition = 0;
        
        while (runProgram == 1) {
           
           cout << "Enter Option: ";
           cin >> userOption;
           cout << endl;
           
           if (userOption == "quit") {
              runProgram = 0;
           }
           
           if (userOption == "add") {
              cout << "Student name: ";
              cin >> studentName;
              cout << endl;
              cout << studentName << "'s GPA: ";
              cin >> studentGPA;
              cout << endl;
              cout << endl;
              
              newStudent = new Student(studentName, studentGPA);
         students.push_back(newStudent);
           }
           
           if (userOption == "drop") {
              cout << "Index of student to drop: ";
              cin >> listPosition;
              cout << endl;
              cout << endl;
            
           tmp = students.at(listPosition);
         delete tmp;
         students.erase(students.begin() + listPosition);
            
           }
           
           if (userOption == "print") {
              PrintAllItems(students);//FIXME
           }
          
          
           
        }

        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?