The C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. An overloaded declaration is a declaration that is declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation).
When you call an overloaded function or operator,
the compiler determines the most appropriate definition to use, by comparing
the argument types you have used to call the function or operator with the parameter
types specified in the definitions. The process of selecting the most appropriate
overloaded function or operator is called overload resolution.
Function Overloading in C++
You can have multiple definitions for the same function name in
the same scope. The definition of the function must differ from each other by
the types and/or the number of arguments in the argument list. You cannot
overload function declarations that differ only by return type.
Operator overriding
In derived class when method name, number or argument, type of
arguments and return type same as parent class method called as operator overriding.
Example of operator overriding
#include<iostream.h>
#include<conio.h>
class A{
public:virtual void display(){cout<<"A \n";
}
};
class B:public A{
public:
void display(){
cout<<"B \n";
}
};void main(){A *p;A obj;B obj1;
clrscr();
p=&obj;
p->display();
p=&obj1;
p->display();
getch();
}
Output
A
B
Operators Overloading in C++
If we create two or more members having same name but different
number or type parameter it is know as operator overloading. You can redefine
or overload most of the built-in operators available in C++.
Thus, a programmer can use operators with user-defined types as
well. Overloaded operators are functions with special names the keyword
operator followed by the symbol for the operator being defined. Like any other
function, an overloaded operator has a return type and a parameter list.
Example of operator overloading
#include<iostream.h>
#include<conio.h>
class A{
public:
virtual void display(){
cout<<"A \n";
}
int display(int i){
return i*i;
}
float display(float a,float b){
return a+b;
}
};
void main(){
A obj;
clrscr();
obj.display();
cout<<obj.display(5)<<endl;
cout<<obj.display(1.3,3.4);
getch();
}
Output
A
25
4.7
Unary operator overloading
The unary operators operate on a single operand and following are
the examplesof Unary operators:
·
The
increment (++) and decrement (--) operators.
·
The
unary minus (-) operator.
The unary operators operate on the object for which they were
called and normally, this operator appears on the left side of the object, as
in !obj, -obj, and ++obj but sometime they can be used as postfix as well like
obj++ or obj--. Following example explain how minus (-) operator can be
overloaded for prefix as well as postfix usage.
Increment (++) and Decrement (- -) Operators
The increment (++) and decrement (--) operators are two important
unary operators available in C++.
Following example explain how increment (++) operator can be
overloaded for prefix as well as postfix usage. Similar way, you can overload
operator (--).
Example of unary operator overloading ( - )
#include<iostream.h>
#include<conio.h>
class A{
int x;
int y;
public:
A(int i , int j){
x=i;
y=j;
}
void display(){
cout<<x<<" "<<y<<endl;
}
void operator -(){
x=-x;
y=-y;
}
};
void main(){
A obj(5,6);
clrscr();
cout<<"Before operator overloading=";
obj.display();
-obj;
cout<<"After operator overlaoding=";
obj.display();
getch();
}
Output
Before operator overloading = 5 6
After operator overloading = -5 -6
Example of unary operator overloading ( pre ++ )
#include<iostream.h>
#include<conio.h>
class A{
int x;
int y;
public:
A(int i , int j){
x=i;
y=j;
}
void display(){
cout<<x<<" "<<y<<endl;
}
void operator ++(){
++x;
++y;
}
};
void main(){
A obj(5,6);
clrscr();
cout<<"Before operator overloading=";
obj.display();
++obj;
cout<<"After operator overlaoding=";
obj.display();
getch();
}
Output
Before operator overloading = 5 6
After operator overloading = 6 7
Binary operator overloading
The unary operators take two arguments and following are the
examples of Binary operators. You use binary operators very frequently like
addition (+) operator, subtraction (-) operator and division (/) operator.
Following example explains how addition (+) operator can be
overloaded. Similar way, you can overload subtraction (-) and division (/)
operators.
Example of binary operator overloading ( + )
#include<iostream.h>
#include<conio.h>
class A{
int x;
int y;
public:
A(int i , int j){
x=i;
y=j;
}
void display(){
cout<<x<<" "<<y<<endl;
}
void operator +(int p){
x=x+p;
y=y+p;
}
};
void main(){
A obj(5,6);
clrscr();
cout<<"Before operator overloading=";
obj.display();
obj+6;
cout<<"After operator overlaoding=";
obj.display();
getch();
}
Output
Before operator overloading = 5 6
After operator overloading = 11 12
Example of binary operator overloading ( + , - )
#include<iostream.h>
#include<conio.h>
class A{
int x;
int y;
public:
A(){}
A(int i , int j){
x=i;
y=j;
}
void display(){
cout<<x<<" "<<y<<endl;
}
A operator +(A p){
A t;
t.x=x+p.x;
t.y=y+p.y;
return t;
}
A operator -(A p){
A t;
t.x=x-p.x;
t.y=y-p.y;
return t;
}
};
void main(){
A obj(5,6),obj1(10,20),obj2(1,7);
A obj3;
clrscr();
obj3=obj+obj1-obj2;
obj.display();
obj1.display();
obj2.display();
obj3.display();
getch();
}
Output
5 6
10 20
1 7
14 19
0 Comments