You are asked to implement all the member and friend functions of the student cl
ID: 3541555 • Letter: Y
Question
You are asked to implement all the member and friend functions of the student class as well as five external functions.
1) You then run the provided main function that makes use of the student class and five external functions as a single file. 2) Break up the program into three separate files then compile and run the multi-file program again. The three separate files are a) student.h, consisting of only the definition or interface of the class, b) student.cpp, consisting of the implementations of all the functions, and c) main.cpp, the provided main() function.
class student{
friend istream & operator >>(istream &, student &);
// pre-condition: an istream and a student object are passed to the function by reference
// post-condition: data members of stduent object are assigned with values entered by the user, e.g., Obama 3.57
friend ostream & operator<<(ostream &, const student &);
// pre-condition: an ostream and a student object are passed to the function by reference
// post-condition: the function displays the student object in one row, e.g., Obama 3.57
public:
bool operator<(const student &) const;
// pre-condition: a student object is passed to the function by reference
// post-condition: compares the GPAs of 2 student , returns true is 1st (calling object) is less than 2nd
bool operator>(const student &) const;
// pre-condition: a student object is passed to the function by reference
// post-condition: Compares the names of 2 students, returns true if 1st (calling object) is greater than 2nd;
private:
string lname; // lname: last name only
double GPA; // ranges from 0 to 4
};
void displayStudentArray(student *, int n);
// pre-condition: an array of n student objects is passed to the function
// post-condition: the contents of the array are display, one student per row
int fillStudentArray(student *);
// pre-condition: a array of student type of size ARRAY_SIZE is passed to the function
// post-condition: array is filled with n (n <= ARRAY_SIZE) set of values entered by user; function returns n
void sortStudentArrayByLname(student *, int n);
// pre-condition: an defined array and n, number of students objects need to be sorted, are passed to the function
// post-condition: the array is sorted in alphabetical order
void sortStudentArrayByGPA(student *, int);
// pre-condition: an defined array and n, number of students objects need to be sorted, are passed to the function
// post-condition: the array is sorted in ascending order by GPA
void swap(student &s1, student &s2);
// pre-condition: two student objects are passed to the function by reference
// post-condition: contents are swapped between student object s1 and s2
const int ARRAY_SIZE = 10; // global constant
int main()
{
int n;
student s[ARRAY_SIZE]; // assuming up to 10 students (can be dynamically allocated)
n = fillStudentArray(s); // n holds actual # student records entered
cout << " Unsorted list: " << endl;
displayStudentArray(s, n);
sortStudentArrayByGPA(s, n);
cout << " List sorted by GPA: " << endl;
displayStudentArray(s, n);
cout << endl;
sortStudentArrayByLname(s, n);
cout << " List sorted by last name: " << endl;
displayStudentArray(s, n);
return 0;
}
Program 1.2: (optional extra 10 points)
Replacing the student array declaration with a vector and making whatever needed changes in your code, run the program again. Your program should produce similar output.
Program 1.3: (optional extra 10 points)
Replacing data member string lname; with char *lname in program 1.1; and making whatever needed changes in your code, run the program again using either single-file or multi-file approach. The program should produce similar output.
Explanation / Answer
Here you go..
http://pastebin.com/5j8dkNay
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.