In: Computer Science
1.) Modify your Student class to overload the following
operators: ==, !=, <, >, <=, >= as
member operators. The comparison operators will compare students by
last name first
name and id number. Also, overload << and >> as friend
operators in the Student class.
Overload << and >> operators in Roster class as well to
output Rosters in a nice format as
well as input Roster. Provide a function sort in a Roster class as
a private function to sort
out all the students in a Roster according to their Last Name,
First Name, and Student id
using the comparison operators that you overloaded in the Student
class.
2.) Modify your Roster class so that it will now store a dynamic
array of pointers to Student
objects. The Roster is identified by the course name, course code,
number of credits,
instructor name, and contains a list of students stored in an array
of pointers to Students.
The array must have the ability to grow if it reaches the capacity
(for that provide a
private function grow ()). Modify your functions to add a student
to a Roster, delete
student from a Roster, and search for a student to reflect the
changes.
Down below are the student.h and roster.h files. If you can make the changes in BOLD text, it will be easier for me to see the changes. Thanks!
//STUDENT.H
#ifndef STUDENT_H
#define STUDENT_H
class Student
{
private:
//member variables
std::string firstName, lastName, standing,
dateOfBirth, dateOfMatriculation;
int credits;
double gpa;
string id; //add id
//private member function to correct standing when
credits change
void correctStanding();
public:
//constructors
Student();
Student(std::string fn, std::string ln, std::string
dob, std::string dom, int c, double g);
//mutators
void setFirst(std::string);
void setLast(std::string);
void setDateOfBirth(std::string);
void setDateOfMatriculation(std::string);
void setCredits(int);
void setGPA(double);
//accessors
std::string getFirst();
std::string getLast();
std::string getDateOfBirth();
std::string getDateOfMatriculation();
std::string getStanding();
int getCredits();
double getGPA();
//console i/o functions
void input();
void output();
};
#endif
//END STUDENT.H
-----------------------------------------
//ROSTER.H
#ifndef ROSTER_H
#define ROSTER_H
#define MAX_CAPACITY 10
#include "student.h"
class Roster
{
public:
//Constructors
Roster();
Roster(std::string, std::string, std::string,
int);
//Accessors
std::string getCourseName() const;
std::string getCourseCode() const;
std::string getInstructor() const;
int getCredits() const;
int getNumStudents() const;
Student getStudent(int) const;
//Mutators
void setCourseName(std::string);
void setCourseCode(std::string);
void setInstructor(std::string);
void setCredits(int);
//Array Functions
void addStudent(Student);
void delStudent(int);
int findStudent(std::string, std::string);
void outputArray();
private:
std::string courseName, courseCode, instructor;
int credits, numStudents = 0;
Student studentList[MAX_CAPACITY];
};
#endif
//END ROSTER.H
----------------------------------
Code to copy:
student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Student
{
private:
//member variables
std::string firstName, lastName, standing,
dateOfBirth, dateOfMatriculation;
int credits;
double gpa;
std::string id; //add id
//private member function to correct standing when
credits change
void correctStanding();
public:
//constructors
Student();
Student(std::string fn, std::string ln, std::string
dob, std::string dom, int c, double g);
//mutators
void setFirst(std::string);
void setLast(std::string);
void setDateOfBirth(std::string);
void setDateOfMatriculation(std::string);
void setCredits(int);
void setGPA(double);
//accessors
std::string getFirst();
std::string getLast();
std::string getDateOfBirth();
std::string getDateOfMatriculation();
std::string getStanding();
int getCredits();
double getGPA();
//console i/o functions
void input();
void output();
//overload the following operators: ==, !=, <,
>, <=, >= as
//member operators.
bool operator==(Student &other);
bool operator!=(Student &other);
bool operator>(Student &other);
bool operator<(Student &other);
bool operator>=(Student &other);
bool operator<=(Student &other);
//overload << and >> as friend
operators in the Student class.
friend ostream& operator<<(ostream &out,
Student &other);
friend istream& operator >> (istream
&in, Student &other);
};
#endif
student.cpp
#include "student.h"
Student::Student(std::string fn, std::string ln, std::string
dob, std::string dom, int c, double g)
{
firstName = fn;
lastName = ln;
dateOfBirth = dob;
dateOfMatriculation = dom;
credits = c;
gpa = g;
correctStanding();
}
Student::Student()
{
firstName = "John";
lastName = "Doe";
dateOfBirth = "01/01/1970";
dateOfMatriculation = "05/25/1995";
credits = 0;
gpa = 4.0;
}
void Student::correctStanding()
{
if (credits < 0)
{
std::cout << "Illegal number
of credits in Student object\n";
exit(0);
}
int x = credits / 15;
switch (x)
{
case 0: standing = "Lower Freshman";
break;
case 1: standing = "Upper Freshman";
break;
case 2: standing = "Lower Sophomore";
break;
case 3: standing = "Upper Freshman";
break;
case 4: standing = "Lower Junior";
break;
case 5: standing = "Upper Junior";
break;
case 6: standing = "Lower Senior";
break;
default: standing = "Upper Senior";
}
}
//Mutators
void Student::setFirst(std::string fn)
{
firstName = fn;
}
void Student::setLast(std::string ln)
{
lastName = ln;
}
void Student::setDateOfBirth(std::string dob)
{
dateOfBirth = dob;
}
void Student::setDateOfMatriculation(std::string dom)
{
dateOfMatriculation = dom;
}
void Student::setCredits(int c)
{
credits = c;
correctStanding();
}
void Student::setGPA(double g)
{
gpa = g;
}
//Accessors
std::string Student::getFirst()
{
return firstName;
}
std::string Student::getLast()
{
return lastName;
}
std::string Student::getDateOfBirth()
{
return dateOfBirth;
}
std::string Student::getDateOfMatriculation()
{
return dateOfMatriculation;
}
std::string Student::getStanding()
{
return standing;
}
int Student::getCredits()
{
return credits;
}
double Student::getGPA()
{
return gpa;
}
//Console I/O Functions
void Student::input()
{
std::cout << "Enter the student's first name:
";
std::cin >> firstName;
std::cout << "Enter the student's last name:
";
std::cin >> lastName;
std::cout << "Enter the student's Date of Birth
(Format: dd/mm/yyyy): ";
std::cin >> dateOfBirth;
std::cout << "Enter the student's Date of
Matriculation (Format: dd/mm/yyyy): ";
std::cin >> dateOfMatriculation;
std::cout << "Enter the student's credit number:
";
std::cin >> credits;
std::cout << "Enter the student's GPA: ";
std::cin >> gpa;
std::cout << endl;
}
void Student::output()
{
std::cout << "First Name: " << firstName
<< std::endl
<< "Last Name: " <<
lastName << std::endl
<< "Date of Birth: " <<
dateOfBirth << std::endl
<< "Date of Matriculation: "
<< dateOfMatriculation << std::endl
<< "Credits: " <<
credits << std::endl
<< "Standing: " <<
standing << std::endl
<< "GPA: " << gpa
<< std::endl;
}
// overloaded operator ==
bool Student:: operator==(Student &other)
{
return((lastName == other.lastName) &&
(firstName == other.firstName) && (id == other.id));
}
// overloaded operator !=
bool Student:: operator!=(Student &other)
{
return(!(*this == other));
}
// overloaded operator >
bool Student::operator>(Student &other)
{
if (lastName > other.lastName)
return true;
else if (lastName < other.lastName)
return false;
else
{
if (firstName >
other.firstName)
return
true;
else if (firstName <
other.firstName)
return
false;
else
return(id >
other.id);
}
}
// overloaded operator <
bool Student:: operator<(Student &other)
{
if (lastName < other.lastName)
return true;
else if (lastName > other.lastName)
return false;
else
{
if (firstName <
other.firstName)
return
true;
else if (firstName >
other.firstName)
return
false;
else
return(id <
other.id);
}
}
// overloaded operator >=
bool Student:: operator>=(Student &other)
{
if (lastName >= other.lastName)
return true;
else
return false;
}
// overloaded operator <=
bool Student:: operator<=(Student &other)
{
if (lastName <= other.lastName)
return true;
else
return false;
}
//overload <<
ostream& operator<<(ostream &out, Student
&other)
{
out << "ID: " << other.id <<
std::endl
<< "First Name: " <<
other.firstName << std::endl
<< "Last Name: " <<
other.lastName << std::endl
<< "Date of Birth: " <<
other.dateOfBirth << std::endl
<< "Date of Matriculation: "
<< other.dateOfMatriculation << std::endl
<< "Credits: " <<
other.credits << std::endl
<< "Standing: " <<
other.standing << std::endl
<< "GPA: " << other.gpa
<< std::endl;
return out;
}
//overload >>
istream& operator >> (istream &in, Student
&other)
{
in >> other.id >> other.firstName >>
other.lastName >> other.dateOfBirth
>> other.dateOfMatriculation
>> other.credits >> other.gpa;
return in;
}
roster.h
#ifndef ROSTER_H
#define ROSTER_H
#define MAX_CAPACITY 10
#include "student.h"
class Roster
{
public:
//Constructors
Roster();
Roster(std::string, std::string, std::string,
int);
//Accessors
std::string getCourseName() const;
std::string getCourseCode() const;
std::string getInstructor() const;
int getCredits() const;
int getNumStudents() const;
Student getStudent(int) const;
//Mutators
void setCourseName(std::string);
void setCourseCode(std::string);
void setInstructor(std::string);
void setCredits(int);
//Array Functions
void addStudent(Student);
void delStudent(int);
int findStudent(std::string, std::string);
void outputArray();
//function to Overload <<
friend ostream& operator<<(ostream &out,
Roster &other);
//function to Overload >>
friend istream& operator >> (istream
&in, Roster &other);
private:
std::string courseName, courseCode, instructor;
int credits, numStudents = 0;
// a dynamic array of pointers to Student
objects.
Student *studentList;
//a function sort in a Roster class as a private
function to sort
//out all the students
void sort();
//private function grow ()
void grow();
};
#endif
roster.cpp
#include <iostream>
#include <cstdlib>
#include "roster.h"
//Default Constructor
Roster::Roster()
{
this->courseName = "CSCI";
this->courseCode = "211";
this->instructor = "Yosef Alayev";
this->credits = 3;
// Point rosterArray to a dynamic string array of
MAX_CAPACITY elements
studentList = new Student[MAX_CAPACITY];
}
//4 Parameter Constructor
Roster::Roster(std::string cN, std::string cC, std::string ins, int
cred)
{
this->courseName = cN;
this->courseCode = cC;
this->instructor = ins;
this->credits = cred;
// Point rosterArray to a dynamic string array of
MAX_CAPACITY elements
studentList = new Student[MAX_CAPACITY];
}
//Accessors
std::string Roster::getCourseName() const { return
this->courseName; }
std::string Roster::getCourseCode() const { return
this->courseCode; }
std::string Roster::getInstructor() const { return
this->instructor; }
int Roster::getCredits() const { return this->credits; }
int Roster::getNumStudents() const { return this->numStudents;
}
//Returns the student in the array at the given index.
//If no student exists at the index, outputs error text to console
and exits
Student Roster::getStudent(int index) const
{
if (index > this->numStudents)
{
std::cout << "Error: There is
no student at index " << index << std::endl;
exit(0);
}
else
{
return
this->studentList[index];
}
}
//Mutators
void Roster::setCourseName(std::string cN) { this->courseName =
cN; }
void Roster::setCourseCode(std::string cC) { this->courseCode =
cC; }
void Roster::setInstructor(std::string ins) { this->instructor =
ins; }
void Roster::setCredits(int cred) { this->credits = cred; }
//Array Functions
//(1)
//Adds a student in the earliest available index.
//If there is no available space, outputs error text to console and
exits
void Roster::addStudent(Student s)
{
if (this->numStudents == MAX_CAPACITY)
{
//The array must have the ability
to grow
//if it reaches the capacity
grow();
this->studentList[numStudents] =
s;
numStudents++;
}
else
{
this->studentList[numStudents] =
s;
numStudents++;
}
}
//(2)
//Removes the student at the given index, then shifts any
succeeding elements down.
//If no student at the index, outputs error text to the console and
exits
void Roster::delStudent(int index)
{
if (index > this->numStudents)
{
std::cout << "Error: There is
no student at index " << index << std::endl;
exit(0);
}
else
{
numStudents--;
for (int i = index; i <
this->numStudents; i++)
{
this->studentList[i] = this->studentList[i + 1];
}
}
}
//(3)
//Checks the studentList for a student with matching first and last
name.
//If the student exists in the array, returns the index in the
array.
//Otherwise, returns -1.
int Roster::findStudent(std::string fName, std::string lName)
{
int index = -1;
for (int i = 0; i < this->numStudents;
i++)
{
if
((this->studentList[i].getFirst() == fName) &&
(this->studentList[i].getLast() == lName))
{
index = i;
break;
}
}
return index;
}
//(4)
//Outputs the studentList to the console.
void Roster::outputArray()
{
for (int i = 0; i < numStudents; i++)
{
std::cout << "****Student "
<< (i + 1) << "****" << std::endl;
studentList[i].output();
std::cout << std::endl;
}
if (numStudents == 0) std::cout << "The Roster
is empty" << std::endl;
}
//function to Overload <<
ostream& operator<<(ostream &out, Roster
&other)
{
out << "Course Name: " << other.courseName
<< std::endl
<< "Course Code: " <<
other.courseCode << std::endl
<< "Instructor Name: "
<< other.instructor << std::endl
<< "Credits: " <<
other.credits << std::endl
<< "Number of students: "
<< other.numStudents << std::endl;
return out;
}
//function to Overload >>
istream& operator >> (istream &in, Roster
&other)
{
in >> other.courseName >> other.courseCode
>> other.instructor >> other.credits >>
other.numStudents;
return in;
}
// function sort to sort out all the students in a Roster
// according to their Last Name, First Name, and Student id
//using the comparison operators that you overloaded in the Student
class.
void Roster::sort()
{
int i, j, min;
Student temp;
for (i = 0; i < numStudents - 1; i++)
{
min = i;
for (j = i + 1; j < numStudents;
j++)
{
if
(studentList[j].operator<(studentList[min]))
{
min = j;
}
}
temp = studentList[i];
studentList[i] =
studentList[min];
studentList[min] = temp;
}
}
//grow() function doubles the size of the array.
void Roster::grow()
{
Student* newArr = new Student[MAX_CAPACITY * 2];
for (int i = 0 ;i < numStudents;i++)
{
newArr[i] = studentList[i];
}
delete[] studentList;
studentList = newArr;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "roster.h"
using namespace std;
int main()
{
//create a Roster object
Roster r1;
//create an array of student objects.
Student s[5];
//add the input
for (int i = 0;i < 5;i++)
{
s[i].input();
}
//add the students
for (int i = 0;i < 5;i++)
{
r1.addStudent(s[i]);
}
//print the output.
r1.outputArray();
return 0;
}
Sample output: