Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ with SLT Part 1 In this part, you design and implement all the classes in th

ID: 3735022 • Letter: C

Question

C++ with SLT
Part 1

In this part, you design and implement all the classes in the UML diagram on the last page. Polymorphism is emphasized.

a) general specifications:

Data members of Student class are protected
All member functions are public; constructors may have several parameters
In Student class, functions print() and tuition() are virtual and tuition() is pure (they must be overridden in DCL)
Print functions must print all the data members in a nice format (e.g. add “endl”; “student name: ***”, etc)

b) call print() and tuition() for the following three students using both static and dynamic binding:

under_rate = 380.0; grad_rate = 500;

“Mary”, “000111222”, “Junior”, 12 credits, her tuition, her gpa (4.0)
“David”, “111222333”, graduate student, 9 credits, his tuition, his thesis (“How to learn data structures using C++/STL?”), his gpa (3.7)
“Jason”, “222333444”, graduate assistant, 9 credit hours, his tuition, his thesis (“Design of efficient algorithms”), his gpa (3.9) his task (“Grading 330 projects and implementing a data mining algorithm”), his hourPay ($20), his superviser (“Dr. Smith”)
These are just facts data; your final output may look differently (but nicely).

Part 2 (45 points)

This part involves I/O streams, files, and STL.

Suppose that input undergraduate roster is a text file in which fields are separated by white-space “ ” and records by “ ”. For example, the first record (the fields are all in the following order: name, ssn, year, credits, gpa) could be

“Mary”, “000111222”, “Junior”, 12, 4.0

(10) Scan (read) the file and construct students one by one. Notice that, you should check the input data according to part a.
(5) Store the students in a list container (can insert one by one).
(5) After all students are scanned and pushed into the list, delete the last student and insert him/her to the front as the first student.
(10) Then print out on screen all the students using list iterator in the following format:

Header line:

Name SSN Year Credits Tuition GPA

……

In each line (including the header), each field’s width is 10 except “Name” whose width is 20. The last two fields are right-justified and have two fraction digits. Tuition must be padded with $ sign to the left. All other fields are left-justified.

Then initialize a vector that contains the above list data and sort the students according to GPA. You must use generic sort algorithm (therefore need to overload “<” operator). After sorting, output the sorted students using the above format.

PS:The project mainly covers three features: STL, inheritance and polymorphism

Explanation / Answer

#pragma once
#include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <string.h>
#include <cstring>
#include <sstream>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;
void part1();
void part2();
void printHeader();

class Student {
protected:
   char name[21];
   char ssn[10];
   float gpa;
   int credits;
public:
   Student(string name_, string ssn_, int credits_, float gpa_) {
       strcpy_s(name, name_.c_str());
       strcpy_s(ssn, ssn_.c_str());
       gpa = gpa_;
       credits = credits_;
   }
   virtual void print() {
       cout << "Name: " << name << endl;
       cout << "SSN: " << ssn << endl;
       cout << "GPA: " << gpa << endl;
       cout << "Credits: " << credits << endl;
   }
   virtual float tuition() = 0;
};

class Undergrad : public Student {
protected:
   float undergrad_rate;
   char* year;
public:
   Undergrad(string name_, string ssn_, char* year_, int credits_, float undergrad_rate_, float gpa_) : Student(name_, ssn_, credits_, gpa_) {
       undergrad_rate = undergrad_rate_;
       year = year_;
   }
   char* get_year() {
       return year;
   }
   float get_rate() {
       return undergrad_rate;
   }
   void set_year(char* newYear) {
       year = newYear;
   }
   float get_gpa() {
       return gpa;
   }
   virtual void print() override {
       Student::print();
       cout << "Undergraduate rate: " << undergrad_rate << endl;
       cout << "Year: " << year << endl;
   }
   virtual float tuition() {
       return (undergrad_rate * credits);
   }
   bool operator < (Undergrad& u)
   {
       return this->get_gpa() < u.get_gpa();
   }
   void printSorted() {
       cout.width(20);
       cout << left << name;
       cout.width(10);
       cout << left << ssn;
       cout.width(1);
       cout << left << " ";
       cout.width(10);
       cout << left << year;
       cout.width(1);
       cout << left << " ";
       cout.width(10);
       cout << left << credits;
       cout.width(1);
       cout << " $";
       cout.width(10);
       cout << right << tuition();
       cout.width(10);
       cout << right << gpa;
       cout.width(1);
       cout << left << " ";
       cout << endl;
   }
};


class Grad : public Student {
protected:
   char* thesis;
   float grad_rate;
public:
   Grad(string name_, string ssn_, char* year, int credits_, float grad_rate_, char* thesis_, float gpa_) : Student(name_, ssn_, credits_, gpa_) {
       thesis = thesis_;
       grad_rate = grad_rate_;

   }
   char* get_thesis() {
       return thesis;
   }
   float get_rate() {
       return grad_rate;
   }
   void set_thesis(char* newThesis) {
       thesis = newThesis;
   }
   void print() {
       Student::print();
       cout << "Graduate rate: $" << get_rate() << endl;
       cout << "Thesis: " << get_thesis() << endl;
   }
   float tuition() {
       return grad_rate * credits;
   }
};
class GradAsst : public Grad {
protected:
   float hourPay;
   char* superviser;
   char* task;
   float gpa;

public:
   GradAsst(string name_, string ssn_, char* year_, int credits_, float grad_rate_, char* thesis_, float gpa_, char* task_, float hourPay_, char* superviser_)
       : Grad(name_, ssn_, year_, credits_, grad_rate_, thesis_, gpa_) {
       superviser = superviser_;
       task = task_;
       hourPay = hourPay_;

   }
   char* get_superviser() {
       return superviser;
   }
   float get_hourPay() {
       return hourPay;
   }
   void set_superviser(char* newSuperv) {
       superviser = newSuperv;
   }
   void set_hourPay(float newPay) {
       hourPay = newPay;
   }
   char* get_task() {
       return task;
   }
   void set_task(char* newTask) {
       task = newTask;
   }
   void print() {
       Grad::print();
       cout << "Hourly Pay: $" << get_hourPay() << endl;
       cout << "Superviser: " << get_superviser() << endl;
       cout << "Task: " << get_task() << endl;
   }
   float tuition() {
       return grad_rate * credits;
   }
};

void part1() {
   float undergrad_rate = 380.0;
   float grad_rate = 500.0;
   //static part
   Undergrad uStudent("Mary", "000111222", "Junior", 12, undergrad_rate, (float)4.0);
   Grad gStudent("David", "111222333", "Graduate", 9, grad_rate, "How to learn data structures using C++ / STL?", (float)3.7);
   GradAsst gAsst("Jason", "222333444", "Graduate Assistant", 9, grad_rate, "Design of efficient algorithms", (float)3.9, "Grading 330 projects and implementing a data mining algorithm", 20, "Dr. Fu");
   uStudent.print();
   cout << "Undergraduate tuition: $" << uStudent.tuition() << " " << endl;
   gStudent.print();
   cout << "Graduate tuition: $" << gStudent.tuition() << " " << endl;
   gAsst.print();
   cout << "Graduate assistant tuition: $" << gAsst.tuition() << " " << endl;

   //dynamic part
   Student *studentptr = &uStudent;
   studentptr->print();
   cout << "Undergraduate tuition: $" << studentptr->tuition() << " " << endl;
   studentptr = &gStudent;
   studentptr->print();
   cout << "Undergraduate tuition: $" << studentptr->tuition() << " " << endl;
   studentptr = &gAsst;
   studentptr->print();
   cout << "Undergraduate tuition: $" << studentptr->tuition() << " " << endl;

   studentptr = 0;
   delete studentptr;
}

void part2() {
   //data declarations
   ifstream inFile("Records.txt");
   string currLineSegment;
   vector<string> seglist;
   string name;
   string ssn;
   char* year;
   int credits;
   float gpa;
   list<Undergrad> students;

   //seperate data using space as a deliminator
   while (getline(inFile, currLineSegment, ' ')) {
       //checks to see if there's a at the end of the line.
       //if there's a , break that loop and continue to construct another student.
       if (currLineSegment.find(' ') != string::npos) {
           seglist.push_back(currLineSegment.substr(0, currLineSegment.find(' ')));
           seglist.push_back(currLineSegment.substr(currLineSegment.find(' ') + 1, currLineSegment.length()));
           continue;
       }
       seglist.push_back(currLineSegment);
   }
   //Adding populating fields from strings from file
  
       while (!seglist.empty()) {
           istringstream(seglist.back()) >> gpa;
           seglist.pop_back();
           istringstream(seglist.back()) >> credits;
           seglist.pop_back();
           year = _strdup(seglist.back().c_str());
           seglist.pop_back();
           ssn = seglist.back();
           seglist.pop_back();
           string lastName = seglist.back();
           seglist.pop_back();
           name = seglist.back();
           seglist.pop_back();
           name += " ";
           name += lastName;
           Undergrad student(name, ssn, year, credits, 380.0, gpa);
           students.push_back(student); // add this student to the back of list
       }
   //move the backmost student to the front
   Undergrad temp = students.back();
   students.pop_back();
   students.push_front(temp);

   //copy the unsorted students to the soon to be sorted students vector.
   //Also prints unsorted students.
   vector<Undergrad> sortedStudents;
   printHeader();
   for (list<Undergrad>::iterator it = students.begin(); it != students.end(); ++it) {
       sortedStudents.push_back(*it);
       it->printSorted();
   }
   //Ascending sorting of student GPAs.
   sort(sortedStudents.begin(), sortedStudents.end());
   cout << " " << "-------Now printing sorted students-------" << " " << endl;
   printHeader();
   //Print sorted students.
   for (Undergrad u : sortedStudents)
   {
       u.printSorted();
   }
};

void printHeader() {
   cout.width(20);
   cout << left << "Name";
   cout.width(10);
   cout << left << "SSN ";
   cout.width(1);
   cout << left << " ";
   cout.width(10);
   cout << left << "Year";
   cout.width(1);
   cout << left << " ";
   cout.width(10);
   cout << left << "Credits";
   cout.width(1);
   cout << right << " ";
   cout.width(10);
   cout << right << "Tuition";
   cout.width(10);
   cout << right << "GPA";
   cout.width(1);
   cout << left << " ";
   cout << endl;
};

int main() {
   part1();
   part2();

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote