And it works! :) The problem Im having right now is operator overloading on the second part of the code. My teacher wrote what to do but Im not understanding. Its already past due so Im not in a rush. I rather learn it slowly than quickly finishing something without understanding it. Dont help me on the last part. Im pretty sure I can handle that :)
#include <iostream>
#include <string>
usingnamespace std;
template <class C>
C getMaxTemplate(C list[], C 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 g = -1.0)
{
name = n;
gpa = g;
}
// (b) overload the > operator as a member function.
// HINT: a student is > from another student if his/her gpa is higher
//...
string getName()
{
return name;
}
double getGpa()
{
return gpa;
}
};
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)};
cout << getMaxTemplate(numbers, 3) << endl;
// (c) call the getMaxTemplate function using students list
// and print the name and gpa of the student with the highest gpa
//...
return 0;
}
#include <iostream>
#include <string>
usingnamespace std;
template <class C>
C getMaxTemplate(C list[], C 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
//...
booloperator>(studentType& s2)
{
// we have here:
// name (also called: this.name
// gpa (also called: this.gpa
// s2.name
// s2.gpa
if (this.gpa>s2.gpa)
returntrue;
elsereturnfalse;
}
string getName()
{
return name;
}
double getGpa()
{
return gpa;
}
};
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)};
cout << getMaxTemplate(numbers, 3) << endl;
// (c) call the getMaxTemplate function using students list
// and print the name and gpa of the student with the highest gpa
//...
return 0;
}