The operator has been used for more than one meaning.
That is, the same operator is used on the different places.
The symbol, *, is used as
Example of overload the addition:
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student(int akey=0, string aname='\0', double acash=0)
{
key = akey;
name = aname;
cash = acash;
}
int key;
string name;
double cash;
};
Student operator+ (Student x, Student y)
{
Student s(12345, "Combine", x.cash + y.cash);
return s;
}
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,"Ruth",102.5), Q(21,"John",56);
cout <<"Print information of P:"<<endl;
cout <<P.key <<" " <<P.name <<" " <<P.cash<<endl;
Student R = P+Q;
cout<<"Print the less object: "<<endl;
cout <<R.key <<" " <<R.name <<" " <<R.cash<<endl;
system("pause");
return 0;
}