What is compile time polymorphism?
When the compiler know about different usage of an item at the time of compilation then called as compile time polymorphism.
To achieve the compile time polymorphism C++ provides another concept called as overloading.
Overloading can be of two types
To achieve the compile time polymorphism C++ provides another concept called as overloading.
Overloading can be of two types
- Method Overloading
- Operator Overloading
Method Overloading
Multiple methods inside a class having the same name but different number of arguments or different type of arguments is called as method overloading.
Example
#include<iostream>
using namespace std;
class Demo
{
public:
static void area(int side)
{
cout <<"Area of square is "<<side*side << endl;
}
static void area(double radius)
{
cout <<"Area of circle is "<< 3.14*radius*radius << endl;
}
static void area(double length, double width)
{
cout <<"Area of rectangle is " << length*width << endl;
}
};
main()
{
Demo::area(5);
Demo::area(5.0);
Demo::area(5.0,6.0);
}
using namespace std;
class Demo
{
public:
static void area(int side)
{
cout <<"Area of square is "<<side*side << endl;
}
static void area(double radius)
{
cout <<"Area of circle is "<< 3.14*radius*radius << endl;
}
static void area(double length, double width)
{
cout <<"Area of rectangle is " << length*width << endl;
}
};
main()
{
Demo::area(5);
Demo::area(5.0);
Demo::area(5.0,6.0);
}
Output
Area of square is 25
Area of circle is 78.5
Area of rectangle is 30
Operator Overloading
When we can use the same operator for multiple purpose then called as operator overloading. For example, see the following program
#include<iostream>
using namespace std;
main()
{
int num=5;
cout << num << 3 << ( num << 3);
}
In this program << operator is working in two forms, first one to send the output to the monitor with cout and second one inside the bracket as bitwise left shift operator.
Output
5340
Using operator keyword
If you want to overload some operator you can use the operator keyword.
Every such operator has a function behind the scene without any name, also called as anonymous function.
Example: Without operator overloading
Create a class Size having two elements as inch and feet. Create a constructor to intialize data in the fields. Create a function addSize() which takes a Size type argument and adds in current size and returns new size. Create a method showSize() to display the size in feet and inch.
Solution
#include<iostream>
using namespace std;
class Size
{
private:
int inch,feet;
public:
Size(int feet, int inch)
{
this->feet=feet;
this->inch=inch;
}
void showSize()
{
cout <<"Size is "<<feet << " feet and "<< inch << " inch" << endl;
}
Size addSize(Size s)
{
int i=inch+s.inch;
int f=feet+s.feet;
if(i>=12)
{
i-=12;
f++;
}
return Size(f,i);
}
};
main()
{
Size x(8,7),y(6,9),z(7,10);
Size p=x.addSize(y);
Size q=p.addSize(z);
q.showSize();
}
Output
Size is 23 feet and 2 inch
Example: Using operator overloading
Create a class Size having two elements as inch and feet. Create a constructor to intialize data in the fields. Overload + operator which takes a Size type argument and adds in current size and returns new size. Create a method showSize() to display the size in feet and inch.
Solution
#include<iostream>
using namespace std;
class Size
{
private:
int inch,feet;
public:
Size(int feet, int inch)
{
this->feet=feet;
this->inch=inch;
}
void showSize()
{
cout <<"Size is "<<feet << " feet and "<< inch << " inch" << endl;
}
Size operator +(Size s)
{
int i=inch+s.inch;
int f=feet+s.feet;
if(i>=12)
{
i-=12;
f++;
}
return Size(f,i);
}
};
main()
{
Size x(8,7),y(6,9),z(7,10);
Size p=x + y + z; //using operator overloading
p.showSize();
}
Output
Size is 23 feet and 2 inch
When we can use the same operator for multiple purpose then called as operator overloading. For example, see the following program
#include<iostream>
using namespace std;
main()
{
int num=5;
cout << num << 3 << ( num << 3);
}
In this program << operator is working in two forms, first one to send the output to the monitor with cout and second one inside the bracket as bitwise left shift operator.
Output
5340
Using operator keyword
If you want to overload some operator you can use the operator keyword.
Every such operator has a function behind the scene without any name, also called as anonymous function.
Example: Without operator overloading
Create a class Size having two elements as inch and feet. Create a constructor to intialize data in the fields. Create a function addSize() which takes a Size type argument and adds in current size and returns new size. Create a method showSize() to display the size in feet and inch.
Solution
#include<iostream>
using namespace std;
class Size
{
private:
int inch,feet;
public:
Size(int feet, int inch)
{
this->feet=feet;
this->inch=inch;
}
void showSize()
{
cout <<"Size is "<<feet << " feet and "<< inch << " inch" << endl;
}
Size addSize(Size s)
{
int i=inch+s.inch;
int f=feet+s.feet;
if(i>=12)
{
i-=12;
f++;
}
return Size(f,i);
}
};
main()
{
Size x(8,7),y(6,9),z(7,10);
Size p=x.addSize(y);
Size q=p.addSize(z);
q.showSize();
}
Output
Size is 23 feet and 2 inch
Example: Using operator overloading
Create a class Size having two elements as inch and feet. Create a constructor to intialize data in the fields. Overload + operator which takes a Size type argument and adds in current size and returns new size. Create a method showSize() to display the size in feet and inch.
Solution
#include<iostream>
using namespace std;
class Size
{
private:
int inch,feet;
public:
Size(int feet, int inch)
{
this->feet=feet;
this->inch=inch;
}
void showSize()
{
cout <<"Size is "<<feet << " feet and "<< inch << " inch" << endl;
}
Size operator +(Size s)
{
int i=inch+s.inch;
int f=feet+s.feet;
if(i>=12)
{
i-=12;
f++;
}
return Size(f,i);
}
};
main()
{
Size x(8,7),y(6,9),z(7,10);
Size p=x + y + z; //using operator overloading
p.showSize();
}
Output
Size is 23 feet and 2 inch
Comments
Post a Comment