Sie sind auf Seite 1von 15

Overloading

means assigning different meanings to the operator depending on context. Like function overloading we can perform operator loading i.e. existing operators can be overloaded. Existing operator like + can be used for addition of two integers. Same + operator can be used for addition o f two float no's and also used for concatenation of two strings .

Overloading

cannot alter the basic template of operator i.e basic meaning of operator cannot be changed. The operators that are predefined in C++ can be overloaded but new operators cannot be assigned. Special operator function is used to overload any operator. syntax: return type operator OP(argument list) { function body }

Return

type is any data type of value returned, operator is special member function name and OP is that operator is being overloaded. The operators like : :,?, :, ., * cannot be overloaded, they having special meaning in C++. Operator function must be either member function or friend function. The operators which are to be overloaded are unary and binary operators. In friend function with unary operator will have one argument in operator function and in member function with unary operator it has no argument.

In

friend function with binary operator will have two argument in operator function and in member function with unary operator it has one argument.

Unary

operator overloading requires one operator and one operand.

When

unary operator overloading can be performed with operator function as a member function, no argument is to be passed.
overloading unary operator with friend function ,one argument is to be passed in operator function.

While

Let

us consider if unary minus(-) operator is used then only one operand is used to overload that operand. Normally - sign is used to change the sign of a variable or a no. Here we are overloading - operator . In member function no argument is passed ,hence object itself is used to invoke member function and it acts as a argument, it wont return value. Example:

Class abc { int x,y; public: abc(int a,int b) { x= a; y=b; } Void operator - () { x=-x; y=-y; } Void display() { cout<<\n x=<<x; cout<<\n y=<<y; } };

Void main() { abc s(2,-3); // creating obj of parametarized constructor s.display(); -s; // calling operator member function s.display(); getch); }

In

unary operator using friend function it should have only one argument which should be the object of the class.Hence object is to be passed as a argument. Unary operator overloading using friend function : Ex.class abc { int x,y; public: abc(int a,int b) { X=a; Y=b; }

Friend void operator (abc &s) { s.x= -s.x; s.y= -s.y; } Void display() { cout<<x= <<x; cout<<y=<<y; } };

Void main() { abc s(2,-3); s.display(); -s; //overloading- operator s.display(); getch(); }

Binary

operators can be overloaded using member function. Following are the rules: 1. While overloading binary operator the operator member function should have only only one argument. 2. This passed argument should be object of the clas. 3. Two operands are used while calling operator. 4. While calling binary operator we have to write: C3=c1+c2;

C1

is the object on LHS of operator which takes responsibility of invoking operator member function. C2 is the object on RHS of operator acts as argument which is passed to operator function. C3 is object where result is stored after overloading. Example:

Class abc { int x,y; Public: abc() { } abc(int a,int b) { X=a; Y=b; } abc operator +(abc &s)

Das könnte Ihnen auch gefallen