overloading comes under compile time polymorphism. In Compile time polymorphism compiler is able to select the appropriate function for a particular call at the compile time itself.
In Compile time polymorphism there are two types of Overloading:
// Example of operator overloading (- operator)
#include <iostream>
class example
{
int a,b,c;
public:
void inputdata(int x, int y, int z);
void display();
void operator -();
};
void counter :: inputdata(int x, int y, int z)
{
a=x;
b=y;
c=z;
}
void counter :: display()
{
cout<<"x="<<a<<endl;
cout<<"y="<<b<<endl;
cout<<"z="<<c<<endl;
}
void counter :: operator -()
{
a=-a;
b=-b;
c=-c;
}
void main()
{
counter obj1;
clrscr();
cout<<"abj1:"<<endl;
obj1.inputdata(30,20,-10);
obj1.display();
-obj1;
cout<<"obj1:"<<endl;
obj1.display();
}
obj1; x=30 y=20 z=-10 obj1; x=-30 y=-20 z=10
int add(int a, int b); int add(int a, int b , int c); int add(float a, float b);
// function Overloading Example
#include <iostream>
#include<conio.h>
#include <math.h>
long add(long, long);
float add(float, float);
int main()
{
long a, b, x;
float c, d, y;
cout << "Enter two integers\n";
cin >> a >> b;
x = add(a, b);
cout << "Sum of integers: " << x << endl;
cout << "Enter two floating point numbers\n";
cin >> c >> d;
y = add(c, d);
cout << "Sum of floats: " << y << endl;
return 0;
}
long add(long x, long y)
{
long sum;
sum = x + y;
return sum;
}
float add(float x, float y)
{
float sum;
sum = x + y;
return sum;
}
Others
Languages
Frameworks
Web / Design
Mobile Technology
Sql & Technology
R4R