Design Classes and Create Objects For this lab you will create a new program tha
ID: 3703028 • Letter: D
Question
Design Classes and Create Objects For this lab you will create a new program that creates a class named Student that contains the following data about a student: 1. Name 2. Student ID number 3. GPA 4. Expected grade in this course 5. Full time or part time. Create five student objects from this class and pass data to fill in the class data above. Besids creating the object, you will write a menu-driven program that performs the following tasks: 1. Look up and prin the student GPA 2. Add a new student to the class 3. Change the GPA of a student 4. Change the expected grde of a student 5. Print the data of all the students in a tabular format 6. Quit the program and Save all the files that you created in this program with appropriate file names.
Explanation / Answer
// Design Classes and Create Objects For this lab you will create
// a new program that creates a class named Student that contains the
// following data about a student: 1. Name 2. Student ID number 3. GPA 4. Expected grade in this course 5. Full time or part time.
// Create five student objects from this class and pass data to fill in the class data above.
// Besids creating the object, you will write a menu-driven program that performs the following tasks:
// 1. Look up and prin the student GPA
// 2. Add a new student to the class
// 3. Change the GPA of a student
// 4. Change the expected grde of a student
// 5. Print the data of all the students in a tabular format
// 6. Quit the program and Save all the files that you created in this program with appropriate file names.
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
#include <limits>
using namespace std;
class Student{
private:
string name;
string studentID;
double gpa;
double expectedGpa;
string studentType;
public:
Student()
{
name = "";
studentID= "";
gpa= 0.0;
expectedGpa = 0.0;
studentType ="";
}
Student(string tname, string tstudentID, double tgpa, double texpectedGpa, string tstudentType);
//getter
string getName();
string getStudentID();
double getGpa();
double getExpectedGpa();
string getStudentType();
//setter
void setName(string );
void setStudentID(string);
void setGpa(double);
void setExpectedGpa(double);
void setStudentType(string);
void display();
};
Student::Student(string tname, string tstudentID, double tgpa, double texpectedGpa, string tstudentType){
name = tname;
studentID= tstudentID;
gpa= tgpa;
expectedGpa = texpectedGpa;
studentType =tstudentType;
}
string Student::getName(){
return name;
}
string Student::getStudentID(){
return studentID;
}
double Student::getGpa(){
return gpa;
}
double Student::getExpectedGpa(){
return expectedGpa;
}
string Student::getStudentType(){
return studentType;
}
void Student::setName(string tname){
name = tname;
}
void Student::setStudentID(string tstudentID) {
studentID= tstudentID;
}
void Student::setGpa(double tgpa){
gpa= tgpa;
}
void Student::setExpectedGpa(double texpectedGpa){
expectedGpa = texpectedGpa;
}
void Student::setStudentType(string tstudentType){
studentType =tstudentType;
}
void Student::display(){
cout << name << " " << studentID << " " << gpa << " " << expectedGpa << " " << studentType << endl;
}
//Display the menu
void showMenu(){
cout << "Select one of the folllowing options" <<endl;
cout << "(1) Look up and print the student GPA" <<endl;
cout << "(2) Add a new student to the class " <<endl;
cout << "(3) Change the GPA of a student" <<endl;
cout << "(4) Change the expected grde of a student " <<endl;
cout << "(5) Print the data of all the students in a tabular format" <<endl;
cout << "(6) quit the Program" <<endl;
cout << "Input Selection ";
}
void Lookup(vector<Student> &list, string searchName){
vector<Student>::iterator it;
int found = 0;
for ( it = list.begin(); it != list.end(); ++it ) {
// print out their info
if(it->getName() == searchName ){
found = 1;
cout << "Student "<< searchName << " is in the list" <<endl;
it->display();
}
}
if(found == 0){
cout << "Student "<< searchName << " is not in the list" <<endl;
}
}
void addStudent(vector<Student> &list){
string name;
string studentID;
double gpa;
double expectedGpa;
string studentType;
cout << "Enter Name" <<endl;
cin >> name;
cout << "Enter StudentID" <<endl;
cin >> studentID;
cout << "Enter gpa" <<endl;
cin >> gpa;
cout << "Enter ExpectedGPA" <<endl;
cin >> expectedGpa;
cout << "Enter StudentType" <<endl;
cin >> studentType;
Student studentObject(name, studentID, gpa, expectedGpa, studentType);
list.push_back(studentObject);
cout << "new Student is added" << endl;
}
void ChangeGpa(vector<Student> &list, string searchName){
vector<Student>::iterator it;
int found = 0;
double gpa;
for ( it = list.begin(); it != list.end(); ++it ) {
// print out their info
if(it->getName() == searchName ){
found = 1;
cout << "Student "<< searchName << " is in the list" <<endl;
it->display();
cout << "Enter new GPA" <<endl;
cin >> gpa;
it->setGpa(gpa);
cout << "Update Details of " << searchName << endl;
it->display();
}
}
if(found == 0){
cout << "Student "<< searchName << " is not in the list" <<endl;
}
}
void ChangeExpectedGpa(vector<Student> &list, string searchName){
vector<Student>::iterator it;
int found = 0;
double gpa;
for ( it = list.begin(); it != list.end(); ++it ) {
// print out their info
if(it->getName() == searchName ){
found = 1;
cout << "Student "<< searchName << " is in the list" <<endl;
it->display();
cout << "Enter new ExpectedGPA" <<endl;
cin >> gpa;
it->setExpectedGpa(gpa);
cout << "Update Details of " << searchName << endl;
it->display();
}
}
if(found == 0){
cout << "Student "<< searchName << " is not in the list" <<endl;
}
}
void displayList(vector<Student> &list){
vector<Student>::iterator it;
for ( it = list.begin(); it != list.end(); ++it ) {
// print out their info
it->display();
}
}
int main(){
vector<Student> list;
Student Steve("Steve" ,"111", 3.5 , 3, "PartTime");
Student Mark("Mark" ,"112", 3.2 , 3, "PartTime");
Student Andrew("Andrew" ,"113", 3.3 , 3, "FullTime");
Student Matt("Matt" ,"114", 3.1 , 3, "PartTime");
Student John("John" ,"115", 3.7 , 3, "PartTime");
list.push_back(Steve);
list.push_back(Mark);
list.push_back(Andrew);
list.push_back(Matt);
list.push_back(John);
// Now setup an iterator loop through the vector
vector<Student>::iterator it;
for ( it = list.begin(); it != list.end(); ++it ) {
// print out their info
it->display();
}
int option;
string name;
do{
showMenu();
cin >> option;
if (cin.fail())
{
cout << "ERROR -- You did not enter an integer";
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
continue;
}
switch(option) {
case 1 :
cout << "Enter Student Name" << endl;
cin >> name;
Lookup(list, name);
break;
case 2 :
cout << "Adding new Student" << endl;
addStudent(list);
break;
case 3 :
cout << "Enter Student Name" << endl;
cin >> name;
ChangeGpa(list, name);
break;
case 4 :
cout << "Enter Student Name" << endl;
cin >> name;
ChangeExpectedGpa(list, name);
break;
case 5 :
displayList(list);
break;
case 6 :
cout << "Quiting the program" << endl;
break;
default :
cout << "Invalid option" << endl;
}
} while(option != 6);
}