Function Overloading

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++.

Example:-

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class aptech
{
public:
sum(int a,int b)
{
return a+b;
}
sum(float a,float b)
{
return a+b;
}
sum(int a,float b)
{
return a+b;
}
sum(float a,int b)
{
return a+b;
}
sum(int a,int b,int c)
{
return a+b+c;
}
};
void main()
{
int x,y,x,ch;
float x1,y1;
aptech ap;
clrscr();
while(1)
{
cout<<"Enter 1 for 2 int sum\nEnter 2 for 2 float sum\nEnter 3 for 1 int and 1 float sum\nEnter 4 for 1 float and 1 int sum\nEnter 5 for 3 int sum\nEnter 6 for exit\nEnter your choice=";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter 2 int values=";
cin>>x>>y;
cout<<ap.sum(x,y)<<endl;
break;
case 2:
cout<<"Enter 2 float values=";
cin>>x1>>y1;
cout<<<<ap.sum(x1,y1)<<endl;
break;
case 3:
cout<<"Enter 1 int and 1 float values=";
cin>>x>>x1;
cout<<ap.sum(x,x1)<<endl;
break;
case 4:
cout<<"Enter 1 float and 1 int values=";
cin>>x1>>x;
cout<<ap.sum(x1,x)<<endl;
break;
case 5:
cout<<"Enter 3 int values=";
cin>>x>>y>>z;
cout<<ap.sum(x,y,z)<<endl;
break;
case 6:
exit(0);
}
}
getch();
}

Operator Overloading

C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. Operator overloading is a compile-time polymorphism. For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +. Other example classes where arithmetic operators may be overloaded are Complex Numbers, Fractional Numbers, Big integers, etc.

Example:-

#include<iostream.h>
#include<conio.h>
class aptech
{
public:
int a,b;
aptech(int x,int y)
{
a=x;
b=y;
}
display()
{
cout<<a<<endl<<b<<endl;
}
aptech operator -()
{
a=-a;
b=-b;
return aptech(a,b);
}
};
void main()
{
clrscr();
aptech a1(11,10);
aptech a2(-5,10);
-a1;
a1.display();
-a2;
a2.display();
getch();
}