P5.2 Given the following student class interface, a) implement all member functi
ID: 3806118 • Letter: P
Question
P5.2 Given the following student class interface, a) implement all member functions of the class, b) implement all non-member external functions of the class, and c) write a main() function to generate an output similar to the sample output appeared after the class interface. Notice that not all pre- and post-conditions for functions are provided; some are intentionally left out for you to fill in.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class student
{
public:
student();
student(string, int, double);
string getName();
// "Accessor" or "accessor function"
// Postcondition: returns the GPA of the calling object
int getCredits();
double getGPA();
void setName(string& str);
// "Mutator" or "mutator function"
// Postcondition: data member name of the calling object has been set to str
void setCredits(int);
void setGPA(double);
void setStuRec(string, int, double);
private:
string name; // Last name only
int credits;
double GPA;
};
// **** External functions for P4.2 ****
void inputStuRec(student& obj);
// prompts the user to enter values to fill obj
// Postcondition: obj is filled with user entered values
void outputStuRec(student& obj);
// Postcondition: contents of obj is displayed
int fillStuArray(student[], int size);
void sortStuArrayByName(student[], int n);
void displayStuReport(student[], int n);
void swap(student&, student&);
bool compareName(student& obj1, student& obj2);
bool compareGPA(student& obj1, student& obj2);
Explanation / Answer
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class student
{
public:
student()
{
name = "No name";
credits = 0;
GPA = 0.0;
}
student(string nm, int cd, double gpa)
{
name = nm;
credits = cd;
GPA = gpa;
}
string getName()
// "Accessor" or "accessor function"
// Postcondition: returns the name of the calling object
{
return name;
}
// "Accessor" or "accessor function"
// Postcondition: returns the credits of the calling object
int getCredits()
{
return credits;
}
// "Accessor" or "accessor function"
// Postcondition: returns the GPA of the calling object
double getGPA()
{
return GPA;
}
void setName(string& str)
// "Mutator" or "mutator function"
// Postcondition: data member name of the calling object has been set to str
{
name = str;
}
// "Mutator" or "mutator function"
// Postcondition: data member credits of the calling object has been set to cd
void setCredits(int cd)
{
credits = cd;
}
// "Mutator" or "mutator function"
// Postcondition: data member GPA of the calling object has been set to gpa
void setGPA(double gpa)
{
GPA = gpa;
}
// "Mutator" or "mutator function"
// Postcondition: Sets all the variables of the student record.
void setStuRec(string nm, int cd, double gpa)
{
setName(nm);
setCredits(cd);
setGPA(gpa);
}
private:
string name; // Last name only
int credits;
double GPA;
};
// **** External functions for P4.2 ****
void inputStuRec(student& obj)
// prompts the user to enter values to fill obj
// Postcondition: obj is filled with user entered values
{
string nm;
int cd;
double gpa;
cout<<"Enter the name of student: ";
cin>>nm;
cout<<"Enter the credits of student: ";
cin>>cd;
cout<<"Enter the gpa of the student: ";
cin>>gpa;
obj.setStuRec(nm, cd, gpa);
}
void outputStuRec(student& obj)
// Postcondition: contents of obj is displayed
{
cout<<"Name: "<<obj.getName()<<endl;
cout<<"Credits: "<<obj.getCredits()<<endl;
cout<<"GPA: "<<obj.getGPA()<<endl;
}
//Given the student array and size as input.
//Postcondition: fills the student array with size number of records.
int fillStuArray(student stu[], int size)
{
for(int i = 0; i < size; i++)
inputStuRec(stu[i]);
return size;
}
//Given the student array and size as input.
//Postcondition: sorts the student array of n records.
void sortStuArrayByName(student stu[], int n)
{
for(int i = 0; i < n-1; i++)
for(int j = 0; j < n - i - 1; j++)
{
if(stu[j].getGPA() > stu[j+1].getGPA())
swap(stu[j], stu[j+1]);
}
}
//Postcondition: Prints the array of records.
void displayStuReport(student stu[], int n)
{
for(int i = 0; i < n; i++)
{
outputStuRec(stu[i]);
cout<<endl;
}
}
void swap(student& stu1, student& stu2)
{
string tempS = stu1.getName();
string nm = stu2.getName();
stu1.setName(nm);
stu2.setName(tempS);
int tempI = stu1.getCredits();
stu1.setCredits(stu2.getCredits());
stu2.setCredits(tempI);
double tempD = stu1.getGPA();
stu1.setGPA(stu2.getGPA());
stu2.setGPA(tempD);
}
bool compareName(student& obj1, student& obj2)
{
return obj1.getName() == obj2.getName();
}
bool compareGPA(student& obj1, student& obj2)
{
return obj1.getGPA() == obj2.getGPA();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.