Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
Practical Paper
Industrial Training
Function and Operator Overloading
In C++ programming, two functions can have same identifier(name) if either number of arguments or type of arguments passed to functions are different. These types of functions having similar name are called overloaded functions.
The meaning of operators are already defined and fixed for basic types like: int, float, double etc in C++ language. For example: If you want to add two integers then, + operator is used. But, for user-defined types(like: objects), you can define the meaning of operator, i.e, you can redefine the way that operator works. For example: If there are two objects of a class that contain string as its data member, you can use + operator to concatenate two strings. Suppose, instead of strings if that class contains integer data member, then you can use + operator to add integers. This feature in C++ programming that allows programmer to redefine the meaning of operator when they operate on class objects is known as operator overloading.
/* Example of function overloading */ int test() { } int test(int a){ } int test(double a){ } int test(int a, double b){ }
All 4 functions mentioned above are overloaded function. It should be noticed that, the return type of all 4 functions is same,i.e, int
. Overloaded function may or may not have different return type but it should have different argument(either type of argument or numbers of argument passed). Two functions shown below are not overloaded functions because they only have same number of arguments and arguments in both functions are of type int
.
/* Both functions has same number of argument and same type of argument*/ /* Hence, functions mentioned below are not overloaded functions. */ /* Compiler shows error in this case. */ int test(int a){ } double test(int b){ }
Example 1: Function Overloading
/*Calling overloaded function test() with different argument/s.*/
#include <iostream>
using namespace std;
void test(int);
void test(float);
void test(int, float);
int main() {
int a = 5;
float b = 5.5;
test(a);
test(b);
test(a, b);
return 0;
}
void test(int var) {
cout<<"Integer number: "<<var<<endl;
}
void test(float var){
cout<<"Float number: "<<var<<endl;
}
void test(int var1, float var2) {
cout<<"Integer number: "<<var1;
cout<<" And float number:"<<var2;
}
p>Output
Integer number: 5 Float number: 5.5 Integer number: 5 And float number: 5.5
In above example, function test()
is called with integer argument at first. Then, function test()
is called with floating point argument and finally it is called using two arguments of type int
and float
. Although the return type of all these functions is same, that is, void
, it's not mandatory to have same return type for all overloaded functions. This can be demonstrated by example below.
Example 2: Function Overloading
/* C++ Program to return absolute value of variable types
integer and float using function overloading */
#include <iostream>
using namespace std;
int absolute(int);
float absolute(float);
int main() {
int a = -5;
float b = 5.5;
cout<<"Absolute value of "<<a<<" = "<<absolute(a)<<endl;
cout<<"Absolute value of "<<b<<" = "<<absolute(b);
return 0;
}
int absolute(int var) {
if (var < 0)
var = -var;
return var;
}
float absolute(float var){
if (var < 0.0)
var = -var;
return var;
}
Output
Absolute value of -5 = 5 Absolute value of 5.5 = 5.5
In above example, two functions absolute()
are overloaded. Both take single argument but one takes integer type argument and other takes floating point type argument. Function absolute()
calculates the absolute value of argument passed and returns it.
Why Operator overloading is used in C++ programming?
You can write any C++ program without the knowledge of operator overloading. But, operator operating are profoundly used by programmer to make a program clearer. For example: you can replace the code like: calculation = add(mult(a,b),div(a,b));
with calculation = a*b+a/b;
which is more readable and easy to understand.
How to overload operators in C++ programming?
To overload a operator, a operator function is defined inside a class as:
The return type comes first which is followed by keyword operator, followed by operator sign,i.e., the operator you want to overload like: +, <, ++ etc. and finally the arguments is passed. Then, inside the body of you want perform the task you want when this operator function is called.
This operator function is called when, the operator(sign) operates on the object of that class class_name.
Example of operator overloading in C++ Programming
/* Simple example to demonstrate the working of operator overloading*/
#include <iostream>
using namespace std;
class temp
{
private:
int count;
public:
temp():count(5){ }
void operator ++() {
count=count+1;
}
void Display() { cout<<"Count: "<<count; }
};
int main()
{
temp t;
++t; /* operator function void operator ++() is called */
t.Display();
return 0;
}
Output
Count: 6
Explanation
In this program, a operator function void operator ++ ()
is defined(inside class temp), which is invoked when ++ operator operates on the object of type temp. This function will increase the value of count by 1.
Things to remember while using Operator overloading in C++ language
- Operator overloading cannot be used to change the way operator works on built-in types. Operator overloading only allows to redefine the meaning of operator for user-defined types.
- There are two operators assignment operator(=) and address operator(&) which does not need to be overloaded. Because these two operators are already overloaded in C++ library. For example: If obj1 and obj2 are two objects of same class then, you can use code
obj1=obj2;
without overloading = operator. This code will copy the contents object of obj2 to obj1. Similarly, you can use address operator directly without overloading which will return the address of object in memory. - Operator overloading cannot change the precedence of operators and associativity of operators. But, if you want to change the order of evaluation, parenthesis should be used.
- Not all operators in C++ language can be overloaded. The operators that cannot be overloaded in C++ are ::(scope resolution), .(member selection), .*(member selection through pointer to function) and ?:(ternary operator).
Following best practice while using operator overloading
Operator overloading allows programmer to define operator the way they want but, there is a pitfall if operator overloading is not used properly. In above example, you have seen ++ operator operates on object to increase the value of count by 1. But, the value is increased by 1 because, we have used the code:
void operator ++() { count=count+1; }
void operator ++() { count=count-100; }
But, it does not make any sense to decrease count by 100 when ++ operator is used. Instead of making code readable this makes code obscure and confusing. And, it is the job of the programmer to use operator overloading properly and in consistent manner.
Again, the above example is increase count by 1 is not complete. This program is incomplete in sense that, you cannot use code like:
t1=++t
It is because the return type of operator function is void. It is generally better to make operator work in similar way it works with basic types if possible.
Here, are the examples of operator overloading on different types of operators in C++ language in best possible ways: