C++ Overloading (Function and Operator)
If we create two or more members having the same name but different in number or type of parameter, it is known as C++ overloading.
Types of overloading in C++ are:
- Function overloading
- Operator overloading

1). Function Overloading
Function Overloading is defined as the process of having two or more function with the same name, but different in parameters is known as function overloading in C++. In function overloading, the function is redefined by using either different types of arguments or a different number of arguments. It is only through these differences compiler can differentiate between the functions.
The advantage of Function overloading is that it increases the readability of the program because you don't need to use different names for the same action.
2).Operators 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 type. For example, C++ provides the ability to add the variables of the user-defined data type that is applied to the built-in data types.
The advantage of Operators overloading is to perform different operations on the same operand.
Below are some criteria/rules to define the operator function:-
- In case of a non-static function, the binary operator should have only one argument and unary should not have an argument.
- In the case of a friend function, the binary operator should have only two argument and unary should have only one argument.
- All the class member object should be public if operator overloading is implemented.
- Operators that cannot be overloaded are . .* :: ?:
- Operator cannot be used to overload when declaring that function as friend function = () [] ->.
Example:
Following program is overloading unary operators: increment (++) and decrement (--).
#include<iostream.h>
using namespace std;
class IncreDecre
{ int a, b;
public:
void accept()
{ cout<<"\n Enter Two Numbers : \n";
cout<<" ";
cin>>a;
cout<<" ";
cin>>b;
}
void operator--() //Overload Unary Decrement
{ a--;
b--;
}
void operator++() //Overload Unary Increment
{ a++;
b++;
}
void display()
{ cout<<"\n A : "<<a;
cout<<"\n B : "<<b;
}
};
int main()
{ IncreDecre id;
id.accept();
--id;
cout<<"\n After Decrementing : ";
id.display();
++id;
++id;
cout<<"\n\n After Incrementing : ";
id.display();
return 0;
}
Output:-
Enter Two Number:
20
30
After Decrementing:
A:19
B:29
After Incrementing:
A:21
B:31
ii). Overloading Binary Operator:- In binary operator overloading function, there should be one argument to be passed. It is overloading of an operator operating on two operands.
Example:-
#include <iostream>
using namespace std;
class Box {
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
public double getVolume(void) {
return length * breadth * height;
}
void setLength( double len ) {
length = len;
}
void setBreadth( double bre ) {
breadth = bre;
}
void setHeight( double hei ) {
height = hei;
}
// Overload + operator to add two Box objects.
Box operator+(const Box& b) {
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
};
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Box Box3; // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// Add two object as follows:
Box3 = Box1 + Box2;
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
Output:-
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
Manipulating of strings using Operator
Manipulating of strings in C++ by operator overloading using character arrays, pointers and string functions. There are no operators for manipulating the strings. There are no direct operator that could act upon the strings or manipulate the strings.
Although there are these limitations exist in C++, it permits us to create our own definitions of operators that can be used to manipulate the strings very much similar to the decimal number. We can manipulate strings by operator overloading as this is not achieved by operators only.
Example:-
#include<iostream>
using namespace std;
int main ()
{
string First = "This is First String and ";
string Second = "This is Second String.";
string Third = First + Second;
cout << Third;
return 0;
}
Type Conversion in C++
A type cast is basically a conversion from one type to another. There are two types of type conversion:
1. Implicit Type Conversion Also known as ‘automatic type conversion’.
- Done by the compiler on its own, without any external trigger from the user.
- Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data.
- All the data types of the variables are upgraded to the data type of the variable with largest data type.
- It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float).
unsigned int -> long -> unsigned ->
long long -> float -> double -> long double
#include <iostream>
using namespace std;
int main()
{
int x = 10;
char y = 'a';
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
cout << "x = " << x << endl
<< "y = " << y << endl
<< "z = " << z << endl;
return 0;
}
Output:-
x = 107
y = a
z = 108
2.Explicit Type Conversion: This process is also called type casting and it is user-defined. Here the user can typecast the result to make it of a particular data type.
In C++, it can be done by two ways:
- Converting by assignment: This is done by explicitly defining the required type in front of the expression in parenthesis. This can be also considered as forceful casting.
(type) expression
where type indicates the data type to which the final result is converted.
Example:
#include <iostream>
using namespace std;
int main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
cout << "Sum = " << sum;
return 0;
}
Output:-
Sum = 2
0 Comments