Question & Answer: Background: C++…..

Background: C++

Add and Drop:

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Background: C++…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

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

Grand Paper Writers
Order NOW For A 10% Discount!
Pages (550 words)
Approximate price: -

Our Advantages

Plagiarism Free Papers

All papers are written by the best professional writers to ensure 100% originality. We always provide plagiarism reports whenever we deliver completed papers.

Free Revisions

All papers by Grand Paper Writers are completed and submitted on time. This timely delivery of papers gives you time to go through the paper before the official deadline.

Title-page

As an additional service, we will provide a title page that precedes the contents of your paper. Here, you will provide your personal details.

Bibliography

We also ensure that we provide an extra page for the references or bibliographies following referencing rules.

Originality & Security

At Grandpaperwriters.com, we guarantee students for the provision of security and original work. All your personal information is handled with confidentiality and is not shared with third parties. Additionally, we ensure that we provide original content with accompanying plagiarism reports to show originality.

24/7 Customer Support

Our customer support team is always available 24/7 to provide instant responses to any queries raised by students.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
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.

Our Services

Grandpaperwriters.com has the best professional essay writers for quality services.

Pricing

Flexible Pricing

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Communication

Admission Help & Client-Writer Contact

Grandpaperwriters.com provides an interactive portal where students can communicate directly to their writers.

Deadlines

Paper Submission

Here, a Grandpaperwriters.com, we do not compromise on the time of our clients. We always deliver all completed papers on or before the deadlines.

Reviews

Customer Feedback

Grandpaperwriters.com appreciates feedback from our clients to help us improve the delivery of essay writing services. As such, we are constantly changing our policies to ensure maximum customer and writer satisfaction.