-
overload
given class sefinition:
class A
{
public:
//constructors
// other members
private:
int x;
int y;
};
Give declarations of operator functions for each of the following ways to overload operator+ you must state where the declaration goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded
a. as friend function
b. as member function
c. as non-friend, non-member function
NExt week i have exam and this is one of the question in the samplepaper .Please solve it.
-
Re: overload
Hey,
This sounds a little bit like homework. I don't think many people here will be willing to help you out unless you show some willing as to answering the question yourself. What exactly about the above do you not understand?
Gary
-
Re: overload
First off all.. That is C++.. This is a vb.net forum. We do have a C, C++ forum where you shouldve posted this.
a friend function is a global function that has access to class members. Therefore the operator will be global. This will overload the + operator for the whole application (well in simplest terms).
The member function will be inside the public section of the class. This overload will only work if overloading an object. For example overload the + is a unary operation. Therefore in your main() you can have this
Code:
myType x, a;
a = +x
or the - operator which means
Code:
myType x, a;
a = -x
however, IT DOES NOT OVERLOAD THIS:
Code:
myType x, y, z;
z = x + y;
To do that you must overload the binary operator.
You can guess the third question. (hint it goes in the class but figure out which section)
I highly recommend reading over your course outline/notes. If this is for an exam then you need to know this stuff really well.