27-Module 7 - Polymorphism - Function Overloading-08-04-2024
27-Module 7 - Polymorphism - Function Overloading-08-04-2024
1
Introduction
2
Overloading in C++
- Overloading refers to the use of the same thing for different purposes.
3
Function Overloading
Function overloading is a feature in C++ where two or more functions can
have the same name but different argument lists.
4
Function Overloading long add(long c, long g)
{
long sum;
Example 01
sum = c + g;
return sum;
#include <iostream>
}
using namespace std;
long add(long, long);
float add(float c, float g)
float add(float, float);
{
int main()
float sum;
{
sum = c + g;
long a, b, c;
return sum;
float e, f, g;
}
cout << "Enter two integers\n";
cin >> a >> b;
Output
c = add(a, b);
Enter two integers
cout << "Sum of integers: " << c << endl;
2
cout << "Enter two floating point numbers\n";
3
cin >> e >> f;
Sum of integers: 5
g = add(e, f); Enter two floating point numbers
cout << "Sum of floats: " << g << endl; 2.2
} 1.2
5
Sum of floats: 3.4
Function Overloading
Example 02
#include <iostream>
long add(long c, long g)
using namespace std; {
long add(long, long); long sum;
float add(float, float); sum = c + g;
float add(float, float,float); return sum;
int main() }
{
long a, b, c; float add(float c, float g)
float e, f, g,h;
cout << "Enter two integers\n";
{
cin >> a >> b; float sum;
c = add(a, b); sum = c + g;
cout << "Sum of integers: " << c << endl; return sum;
cout << "Enter two floating point numbers\n"; }
cin >> e >> f; float add(float c, float g, float z)
g = add(e, f); {
cout << "Sum of floats: " << g << endl; float sum;
cout << "Enter three floating point numbers\n";
cin >> e >> f >> h;
sum = c + g + z;
g = add(e, f, h); return sum;
cout << "Sum of floats: " << g << endl; }
6
}
Function Overloading
Example 02
Output
7
Operator Overloading
C++ has the ability to provide the operators with a special
meaning for the data types.
8
Defining Operator Overloading
The general form of an operator function is
10
Overloaded Operator Call
11
Unary Operator Overloading
Example 03
#include <iostream>
using namespace std; int main()
class Test {
{ Test t;
private: ++t;
int count; t.Display();
public: return 0;
Test() }
{ count = 5;
}
void operator ++()
{ Output
count = count+1;
} Count: 6
void Display()
{
cout<<"Count: "<<count;
}
}; 12
Example 04
#include<iostream> void display()
using namespace std; {
class ID cout<<"\n A : "<<a;
{ cout<<"\n B : "<<b;
int a, b; }
public:
};
void accept()
{ int main()
cout<<"\n Enter Two Numbers : \n"; {
cout<<" "; ID id;
cin>>a; id.accept();
cout<<" "; --id;
cin>>b; Output
Output: cout<<"\n After Decrementing : ";
} Enter
Enter TwoTwoNumbers
Numbers: : id.display();
void operator--() { 33 44
++id;
a--; After Decrementing
After Decrementing : :
A : 2 ++id;
b--; A:2
B cout<<"\n\n After Incrementing :
} B :: 33
void operator++() { After Incrementing: : "; id.display();
After Incrementing
a++; A
A :: 44 return 0;
b++; BB :: 55 }
13
}
Binary Operator Overloading Example 05
#include <iostream>
temp.real = real - c2.real;
using namespace std;
temp.imag = imag - c2.imag;
class Complex
return temp;
{
}
private:
void output()
float real;
{
float imag;
cout << "Output Complex number:
public:
" << real << "+" << imag << "i";
Complex()
}
{
};
real =0;
int main()
imag=0; }
{
void input()
Complex c1, c2, result;
{
cout<<"Enter first complex number: \n";
cout << "Enter real and imaginary
c1.input();
Parts respectively: ";
cout<<"Enter second complex number:\n";
cin >> real;
c2.input();
cin >> imag;
result = c1 - c2;
}
result.output();
Complex operator - (Complex c2)
return 0;
{
} 14
Complex temp;
Binary Operator Overloading Example 05
Output
15
How overloaded module works?
16
Operators that can be Overloaded
17
Operators that cannot be Overloaded
18
Operator Overloading using Friend
19
Operator Overloading using Friend
Example 6
#include<iostream>
using namespace std;
class UnaryFriend
{
int a=10;
int b=20;
int c=30;
public:
void getvalues()
{
cout<<"Values of A, B & C\n";
cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl;
}
void show()
{
cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl;
}
void friend operator-(UnaryFriend &x);
};
20
Example 6 Continuation..
void operator-(UnaryFriend &x)
{
x.a = -x.a;
x.b = -x.b;
x.c = -x.c;
}
int main()
{
UnaryFriend x1;
x1.getvalues();
cout<<"Before Overloading\n";
x1.show();
cout<<"After Overloading \n";
-x1;
x1.show();
return 0;
}
21
Operator Overloading using Friend
#include<iostream> Example 7
using namespace std;
class Complex
{
int num1, num2;
public:
void accept()
{
cout<<"\n Enter Two Complex Numbers : ";
cin>>num1>>num2;
}
friend Complex operator+(Complex c1, Complex c2);
void display()
{
cout<<num1<<"+"<<num2<<"i"<<"\n";
}
};
22
Complex operator+(Complex c1, Complex c2)
{
Complex c;
Example 7 Continuation..
c.num1=c1.num1+c2.num1;
c.num2=c1.num2+c2.num2;
return(c);
}
int main()
{
Complex c1,c2, sum;
c1.accept();
c2.accept();
sum = c1+c2;
cout<<"\n Entered Values : \n";
cout<<"\t";
c1.display(); cout<<"\t";
c2.display();
cout<<"\n Addition of Real and Imaginary Numbers : \n";
cout<<"\t";
sum.display();
return 0;
}
23
List of operators where friend cannot be used
1. Assignment operator =
2. Function call ( )
3. Subscripting operator [ ]
4. Class member access operator ->
24
Rules for Operator Overloading
25
References
1. Herbert Schildt, C++: The Complete Reference, 4th edition, Mc
Graw Hill Education, 2017.
2. E.Balagurusamy, Object Oriented Programming with C++ (6th
edition), Mc Graw Hill Education, 2016.
26