samedi 28 mars 2015

operatpor overloading in c++


i was trying exercise on programmr.com. but i didn't get below example of basic operator overloading. can somebody please explain me below. challenge is:- "Overload the + operator for the class 'temp' to add and return the answer of two objects of class 'temp'."



#include <iostream>

using namespace std;

class temp
{
int value;

public:
temp(int v=0) : value(v) { }

//WRITE YOUR CODE HERE



//

int getVal()
{
return value;
}
};

int main()
{
int n1, n2;
cout << "Enter value for t1: ";
cin >> n1;
cout << "Enter value for t2: ";
cin >> n2;
temp t1(n1), t2(n2), t3;
t3 = t1+t2;
cout << "Their sum is " << t3.getVal();
return 0;
}



Aucun commentaire:

Enregistrer un commentaire