/********************************************************
How do you define an operator overload < to an object?
An object contains several kinds of information. Decide
which information is a criteria to put two objects in order.
After that implement it.
*********************************************************/
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student(int akey=0, string aname='\0', char agrade='\0')
{
key = akey;
name = aname;
Grade = agrade;
}
int key;
string name;
char Grade;
};
int operator< (Student x, Student y)
{
return x.key < y.key;
}
template <class T> //template class can be any data type
T Func(T x, T y)
{
if(x < y)
return x;
else
return y;
}
int main()
{
double A=5.3, B=1.7;
cout<<"Func(A, B): "<<Func(A, B)<<endl;
Student P(18,"John",'A'), Q(21,"Ruth",'B');
cout <<"Print information of P:"<<endl;
cout <<P.key <<" " <<P.name <<" " <<P.Grade<<endl;
Student R = Func(P, Q);
cout<<"Print the less object: "<<endl;
cout <<R.key <<" " <<R.name <<" " <<R.Grade<<endl;
system("pause");
return 0;
}