1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
template <class C>
C getMaxTemplate(C list[], int size){
C result = list[0];
for (int i = 0 ; i < size ; i ++)
{
if (list[i] > result)
result = list[i];
}
return result;
}
// (a) write a function template to return the max element in a given list
// hint: the list will have elements of generic type
//...
class studentType
{
private:
string name;
double gpa;
public:
studentType(string n = "default name", double gpa = -1.0)
{
name = n;
this->gpa = gpa;
}
// (b) overload the > operator as a member function.
// HINT: a student is > from another student if his/her gpa is higher
//...
bool operator>(studentType& s2)
{
// we have here:
// name (also called: this.name
// gpa (also called: this.gpa
// s2.name
// s2.gpa
if (this->gpa>s2.gpa)
return true;
else
return false;
}//ends bool operator
string getName()
{
return name;
}//ends getName()
double getGpa()
{
return gpa;
}//ends getGpa()
};//ends studentType class
ostream& operator<<(ostream &out, studentType B)
{
return;
};
int main()
{
int numbers[6] = {11, 33, 444, 55, 66 -100};
studentType students[3] = {studentType("john", 3.01),
studentType("alice", 3.9),
studentType("liz", 2.5)};
int maxNumber = getMaxTemplate(numbers, 3);
cout << maxNumber << endl;
// (c) call the getMaxTemplate function using students list
// and print the name and gpa of the student with the highest gpa
//...
studentType max = getMaxTemplate(students, 3);
cout << max.getName() << " " << max.getGpa() << endl;
cout<<getMaxTemplate(students,3)<<endl;
system("pause");
return 0;
}
|