Anda di halaman 1dari 13

Operator Overloading

Operator overloading is the ability for a language to redefine the way its operators behave
for certain objects. It allows the programmer to extend the language and give it new
abilities.

a1 = a2 + a3;

The above operation is valid, if a1, a2 and a3 are instances of in-built Data Types. But what
if those are, say objects of a Class; is the operation valid?

Yes, it is, if you overload the ‘+’ Operator in the class, to which a1, a2 and a3 belong.

Operator overloading is used to give special meaning to the commonly used operators (such
as +, -, * etc.) with respect to a class. By overloading operators, we can control or define
how an operator should operate on data with respect to a class.

What Operators can I Overload?

The C++ language has a large number of operators. C++ allows you to overload the
following list:

• Assignment operator (=)


• Arithmetic operators (+, -, *, /, %)
• Increment/Decrement operators (++, --)
• Operator-assignment (+=, -=, *=, /=, <<=, &=, |= etc.)
• Unary operators (-, +, !, ~ etc.)
• Relational (Comparision) operators (<, >, <=, ==, != etc.)
• Bitshift/Extraction operators (<<, >>)
• Subscript operator ([ ])
• Function call '()' operator
• Bit operators (&, |, ^, ~)
• Logical operators (&&, ||, !)
• Comma (,) operator
• Pointer to member (->)
• new and delete operators.

You CANNOT overload the following operators:

• Ternary expression operator (? :)


• Scope operator (::)
• Class member operator (.)
• Preprocessor symbol operator (# and ##)
• Pointer to member operator (.*)
• sizeof operator

Since the number of C++ operators is very large, it is easier to memorize the operators that
cannot be overloaded and assume that anything else can be overloaded.

1
Overloading Rules

The following rules must be noted:

• You cannot invent new operators that do not exist in the language. For instance, @ is
not a valid C++ operator and cannot be overloaded.
• Overloaded operators must follow the same syntax rules. For example:
• SomeClass x, y;
• x ++ 12; // Not allowed: ++ is not a binary operator.
• x++; // This is allowed
• % x; // Not allowed: % is not an unary operator.
• y = x % 3; // This is allowed
• You cannot change the way an operator works between native C++ types. For
instance, you cannot change how + works between a double and an integer variable,
as this operation is native to the compiler. You can however change how + works
between a Complex class and a double variable, since Complex isn't a native C++
type.
• The order of operator evaluation remains unchanged. You cannot change the operator
precedence rules.
• You cannot change the operator associativity rules either. For example, the unary
minus (-) operator associates from right to left.
• X = -Y; // Unary minus associates from right to left.
• X = Y-; // You CANNOT alter unary minus to go from left to right.
• Overloading some operators doesn't implictly define other operators for you. For
example, overloading the + and = operators for a class doesn't mean that the +=
operator is overloaded automatically for you. You will need to write separate overload
code for the += operator as well.

Unwritten Rule: Try to overload operators where they make sense. For example, virtually
everyone is used to seeing arithmetic symbols for matrix operations such as addition,
subtraction, multiplication etc., because this is how we were taught to represent them at
school (surely you weren't sleeping there, were you?). Therefore, it makes sense to overload
the arithmetic operators (+, -, *) for matrix objects. You should note that just because you
CAN overload the + sign to mean subtraction between two matrices, doesn't mean you
SHOULD actually do it. You should keep the overloaded operators analogous to their original
meaning. Call this the "behaves like an int" rule -- i.e. if an operator behaves a certain way
for an int, you should overload it to an analogous meaning for your class.

Operators are overloaded in c++ by creating operator functions: in 2 ways

1. As a member of a class 2. As a Friend Function of a class.

2
1. Creating operator functions using a Member Function of a class:

Operator functions are declared using the following general form:

ret-type operator#(arg-list);

and then define it as a normal member function.

Here: ret-type  is commonly the name of the class itself as the operations would
commonly return data (object) of that class type.

#  is replaced by any valid operator such as +, -, *, /, ++, -- etc.

// Eg program 1: to illustrate operator overloading : Binary Operator +

#include <iostream.h>

#include<conio.h>

class myclass{

int sub1, sub2;

public:

myclass(){} // default constructor

myclass(int x, int y){sub1=x; sub2=y;} // main constructor

myclass operator +(myclass); // notice the declaration

void show(){cout<<"\nSub1 = "<<sub1<<"\t Sub2 = "<<sub2;}

};

myclass myclass::operator +(myclass ob) // returns data of type myclass

myclass temp;

temp.sub1=sub1 + ob.sub1; // Add the data of the object that generated the call with

temp.sub2=sub2 + ob.sub2; // the data of the object passed to it and store in temp

return temp; // temp.sub2=ob1.sub2 + ob2.sub2;

3
}

void main()

clrscr();

myclass ob1(10,20);

myclass ob2(30,40);

myclass ob3;

ob3=ob1+ob2; // this is valid i.e. temp.sub2=ob1.sub2 + ob2.sub2;

ob1.show();

ob2.show();

ob3.show();

getch();

Please notice that the operator function  myclass operator +(myclass); is taking only one
argument when it’s operating on two objects (i.e. it’s a binary operator).

To understand this, first have a look at this line of code:

ob3 = ob1 + ob2;

Now assume that ‘operator+’ function is just a regular function which is called as below
when the respective operator (‘+’ in this case) is encountered with respect to the objects of
its class.

ob3 = ob1.operator+(ob2);

Something like this happens internally when an overloaded operator is encountered. As you
can understand from this, when an operator is encountered, the objects to the left (here
ob1) generates a call to the operator function to which ob2 is passed, the function does the
operation as defined and returns the result as an object which is then assigned to ob3.

Therefore when a binary operator is overloaded using a Member Function of a class, it takes
only one argument.

4
// Eg program 2: to illustrate operator overloading : Binary Operator +,-,=
#include <iostream.h>
#include<conio.h>

class ThreeD
{
int x, y, z;
public:
ThreeD() { x = y = z = 0; }
ThreeD(int i, int j, int k) { x = i; y = j; z = k; }

ThreeD operator+(ThreeD op2); // op1 is implied


ThreeD operator=(ThreeD op2); // op1 is implied
ThreeD operator-(ThreeD op2); // op1 is implied
void show() ;
};

ThreeD ThreeD::operator-(ThreeD op2) // Overload subtraction.


{
ThreeD temp;

temp.x = x - op2.x;
temp.y = y - op2.y;
temp.z = z - op2.z;
return temp;
}
ThreeD ThreeD::operator+(ThreeD op2) // Overload +
{
ThreeD temp;

temp.x = x + op2.x;
temp.y = y + op2.y;
temp.z = z + op2.z;
return temp;
}

5
ThreeD ThreeD::operator=(ThreeD op2) // Overload assignment
{
x = op2.x;
y = op2.y;
z = op2.z;
return *this; // i.e., return object that generated call
}
void ThreeD::show() // Show X, Y, Z coordinates.
{
cout << x << ", ";
cout << y << ", ";
cout << z << "\n";
}
void main()
{
clrscr();
ThreeD a(1, 2, 3), b(10, 10, 10), c;

cout << "Original value of a: ";


a.show();
cout << "Original value of b: ";
b.show();

c = a - b;
cout << "a - b: ";
c.show();

c = a + b;
cout << "a + b: ";
c.show();
c=a;
cout << "a = c: ";
c.show();
getch();
}

6
// Eg program3: to illustrate operator overloading : Binary Operator +,-,=,++
#include <iostream.h>
#include<conio.h>
class Point
{
int x, y;
public:
Point() {} // needed to construct temporaries
Point(int px, int py) { x = px; y = py; }

void show() { cout <<"\n x = " <<x << "\t y = "<< y ; }

Point operator+(Point op2);


Point operator-(Point op2);
Point operator=(Point op2);
Point operator++();
};

Point Point::operator+(Point op2) // Overload + for Point.


{
Point temp;

temp.x = op2.x + x;
temp.y = op2.y + y;
return temp;
}

Point Point::operator-(Point op2) // Overload - for Point.


{
Point temp;

// notice order of operands


temp.x = x - op2.x;
temp.y = y - op2.y;
return temp;
}

7
Point Point::operator=(Point op2) // Overload asignment for Point.
{
x = op2.x;
y = op2.y;
return *this; // i.e., return object that generated call
}

Point Point::operator++() // Overload prefix ++ for Point.


{
x++;
y++;
return *this; // i.e., return object that generated call
}

void main()
{
clrscr();

Point ob1(10, 20), ob2( 5, 30), ob3(90, 90);

ob1.show();
ob2.show();

++ob1;
ob1.show(); // displays 11 21

ob2 = ++ob1;
ob1.show(); // displays 12 22
ob2.show(); // displays 12 22

ob1 = ob2 = ob3; // multiple assignment


ob1.show(); // displays 90 90
ob2.show(); // displays 90 90

getch();
}

8
2. Creating operator overloading functions using a Friend Function of a class:

// Eg. Programme1: + is overloaded using friend function

#include <iostream.h>
#include<conio.h>
class Point {
int x, y;
public:
Point() {} // needed to construct temporaries
Point(int px, int py) { x = px; y = py; }

void show() { cout <<"\n x = "<< x << "\t y = "<< y ; }

friend Point operator+(Point op1, Point op2); // now a friend


Point operator-(Point op2);
Point operator=(Point op2);
Point operator++();
};

Point operator+(Point op1, Point op2) // Now, + is overloaded using friend function.
{
Point temp;

temp.x = op1.x + op2.x;


temp.y = op1.y + op2.y;

return temp;
}

Point Point::operator-(Point op2) // Overload - for Point.


{
Point temp;
temp.x = x - op2.x; // notice order of operands
temp.y = y - op2.y;
return temp;
}

9
Point Point::operator=(Point op2) // Overload assignment for Point.
{
x = op2.x;
y = op2.y;
return *this; // i.e., return object that generated call
}

Point Point::operator++() // Overload ++ for Point.


{
x++;
y++;
return *this;
}

void main()
{
clrscr();

Point ob1(10, 20), ob2( 5, 30);

ob1 = ob1 + ob2;


ob1.show();

getch();
}

// Eg. program 2: to illustrate operator overloading : Binary Operator +,-,=


#include <iostream.h>

#include<conio.h>

class Num

friend Num operator+(Num ob, int i);

friend Num operator+(int i, Num ob);

int count;

10
public:

Num(int cc=0){ count=cc; }

Num& operator=(int i);

void Show(){ cout <<"\n Count = "<<count; }

};

Num& Num::operator=(int i)

count=i;

return *this;

Num operator+(Num ob, int i) // This handles ob+int.

Num temp;

temp.count=ob.count+i;

return temp;

Num operator+(int i, Num ob) // This handles int+ob.

Num temp;

temp.count=ob.count+i;

return temp;

void main()

clrscr();

11
Num obj;

obj=10;

obj.Show();

obj=10+obj;

obj.Show();

obj=obj+12;

obj.Show();

getch();

// Eg. program 3: Make op-- a friend; use reference

#include <iostream.h>
#include<conio.h>
class Point
{
int x, y;
public:
Point() {}
Point(int px, int py) { x = px; y = py; }
void show() { cout <<"\n x = "<< x << "\n y = "<< y<<"\n"; }
Point operator=(Point op2);
friend Point operator++(Point &op);
friend Point operator--(Point &op);
};

Point Point::operator=(Point op2) // Overload assignment for Point.


{
x = op2.x;
y = op2.y;
return *this; // i.e., return object that generated call
}

12
Point operator++(Point &op) // Now a friend; use a reference parameter.
{
op.x++;
op.y++;
return op;
}
Point operator--(Point &op) // Make op-- a friend; use reference.
{
op.x--;
op.y--;

return op;
}

void main()
{
clrscr();

Point ob1(10, 20), ob2;

ob1.show();
++ob1;
ob1.show(); // displays 11 21

ob2 = ++ob1;
ob2.show(); // displays 12 22

--ob2;
ob2.show(); // displays 11 21

getch();
}

13

Anda mungkin juga menyukai