Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

27-Module 7 - Polymorphism - Function Overloading-08-04-2024

Uploaded by

Shantanu Nanda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

27-Module 7 - Polymorphism - Function Overloading-08-04-2024

Uploaded by

Shantanu Nanda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

POLYMORPHISM

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.

The function would perform different operations depending on the


arguments in the function call.

The correct function to be invoked is determined by checking the number


and type of the arguments but not the function type.

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

Enter two integers


2
3
Sum of integers: 5
Enter two floating point numbers
2.2
1.2
Sum of floats: 3.4
Enter three floating point numbers
1.1
2.2
3.3
Sum of floats: 6.6

7
Operator Overloading
C++ has the ability to provide the operators with a special
meaning for the data types.

The mechanism of giving such a special meaning to an


operator is known as Operator Overloading.

The process of making an operator to exhibit different


behaviors in different instances is known as operator
overloading.

8
Defining Operator Overloading
The general form of an operator function is

Return_type classname :: operator op(arg)


{
// function body
}
- Operator function can be either member function or friend
function
- if it is friend function then it will have only one argument for
unary operators and two for binary operators
- if it is member function then it has no arguments for unary
operators and only one for binary operators
9
Operator Overloading

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

Enter first complex number:


Enter real and imaginary Parts respectively: 4
3
Enter second complex number:
Enter real and imaginary Parts respectively: 2
1
Output Complex number: 2+2i

15
How overloaded module works?

16
Operators that can be Overloaded

17
Operators that cannot be Overloaded

1. Scope Resolution Operator (::)


2. Pointer-to-member Operator (.*)
3. Member Access or Dot operator (.)
4. Ternary or Conditional Operator (?:)
5. Object size Operator (sizeof)
6. Object type Operator (typeid)

18
Operator Overloading using Friend

• Friend function using operator overloading offers better flexibility


to the class.
• These functions are not a members of the class and they do not
have 'this' pointer.
• When you overload a unary operator you have to pass one
argument.
• When you overload a binary operator you have to pass two
arguments.
• Friend function can access private members of a class directly.

friend return-type operator operator-symbol (Var 1, Var 2)


{
//Statements;
}

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

• Only existing operators can be overloaded


• The overloaded operator must have atleast one operand i.e. user-defined type
• Some operators cannot be overloaded
• The precedence of the operators remains same
• Overloaded operators cannot have default arguments.
• ifthe overloaded function is a friend function then it will have only one
argument for unary operators and two for binary operators
• if
the overloaded function is a member function then it has no arguments for
unary operators and only one for binary operators

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

You might also like