You are on page 1of 3

Object Oriented Programming Lab Lab 4

Object Oriented Programming


Operator Overloading
Operator overloading is a compile-time polymorphism in which the operator is overloaded to
provide the special meaning to the user-defined data type. Operator overloading is used to overload
or redefines most of the operators available in C++. It is used to perform the operation on the user-
defined data types (Classes, Structures). It will become clear from following example.
Example
Constructor can be defined with default values so if the arguments are not passed, members can be
initialized with default values.

In above program, + operator is adding two primitive data types. Objects are non-primitive data
types. In C++, we can redefine the way operators work on non-primitive data types. If you see
commented portion, compiler don’t know how to add these two values because these are objects of
user-defined data types. As it can add 3+4+1+5 or 3+4 and 1+5 or 3+1 and 4+5 or any other
combination. So we can overload the operator for user-defined data types.

1|Page Furqan Nasir


Object Oriented Programming Lab Lab 4

Example

We have seen binary operator overloading. Operator that performs operation on two parameters.

Unary Operator Overloading


We also have operators that involve only one parameter e.g. Increment operator(++). These
operators are called unary operators.

2|Page Furqan Nasir


Object Oriented Programming Lab Lab 4

Example

3|Page Furqan Nasir

You might also like