0% found this document useful (0 votes)
8 views

C++ Lecture 24

Uploaded by

Apoorv Malviya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C++ Lecture 24

Uploaded by

Apoorv Malviya
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

01

02

LECTURE 24
04
Today’s Agenda
01 Overloading of Binary Operator

02 How the expressions solved by the compiler

03 Overloading of Relational Operator

Practical Examples
04

05 Assignment
05

05
Overloading of Binary Operators

Distance Distance::operator+(const Distance & Q)


1. Overloading Operator + as Member {
Distance Temp;
Function Temp.feet = feet + Q.feet;
Temp.inches = inches + Q.inches;
if(Temp.inches >= 12)
class Distance
The compiler will interpret {
{ the following call Temp.feet = Temp.feet + Temp.inches / 12;
private: Temp.inches = Temp.inches % 12;
int feet, inches; D3 = D1 + D2; }
public: as return Temp; Whenever in C++ we overload a binary
void get() }
{ D3 = D1.operator+(D2); int main()
operator as a member function then
cout<<"Enter feet and inches: "; { the compiler will interpret the
cin>>feet>>inches; Distance D1, D2, D3; expression in the following manner i.e.,
} D1.get(); the object placed on the left-hand side
void show() D2.get();
of the binary operator will be treated
{ D3 = D1 + D2;
cout<<feet<<", "<<inches<<endl; D1.show(); as a calling object and the object
} D2.show(); placed on the right-hand side will be
Distance operator+(const Distance &); D3.show(); treated as the value passed as an
}; return 0; argument to the respective expression
}
Output of The Previous Code

Output

Enter feet and inches: 10 8


Enter feet and inches: 9 7
10, 8
9, 7
20, 3

Process returned 0 (0x0) execution time : 7.703 s


Press any key to continue.
Overloading of Binary Operators

Distance operator+(const Distance & P, const Distance & Q)


2. Overloading operator+ as a friend {
Distance Temp;
function Temp.feet = P.feet + Q.feet;
Temp.inches = P.inches + Q.inches;
class Distance if(Temp.inches >= 12)
{ {
private: Temp.feet = Temp.feet + Temp.inches / 12;
int feet, inches; Temp.inches = Temp.inches % 12;
public: }
void get() return Temp;
{ }
cout<<"Enter feet and inches: "; int main()
cin>>feet>>inches; {
Now, if the operator is
} Distance D1, D2, D3; declared as a friend function,
void show() D1.get();
{ D2.get();
the compiler will interpret the
cout<<feet<<", "<<inches<<endl; D3 = D1 + D2; call as:
} D1.show();
friend Distance operator+(const Distance &, const Distance &); D2.show(); D3 = operator+(D1, D2);
}; D3.show();
return 0;
}
Output of The Previous Code

Output

Enter feet and inches: 10 8


Enter feet and inches: 9 7
10, 8
9, 7
20, 3

Process returned 0 (0x0) execution time : 4.966 s


Press any key to continue.
According to you, how will the following call be handled by the compiler:

Assume, that we have overloaded operator+ as Member Function

D4 = D1 + D2 + D3;

D1.operator+(D2) + D3

Temp + D3

Temp.operator+(D3);

Temp + D3
How the following line is actually handled by the compiler?

This << is overloaded operator


int a = 10;

cout<<a;

cout.operator<<(a);
How the following line is actually handled by the compiler?

This >> is overloaded operator

int a;

cin>>a;

cin.operator>>(a);
Overloading Relational Operators

Comparing 2 objects of
class Distance
feet 1

D1
inches 0

feet 0
D2

inches 12
Overloading Relational Operators
class Distance int Distance::operator==(const Distance & Q)
{ {
private: int x, y;
int feet, inches; x = feet * 12 + inches;
public: y = Q.feet * 12 + Q.inches;
void get() if(x == y)
{ return 1;
cout<<"Enter feet and inches: "; else
cin>>feet>>inches; return 0;
} }
void show() int main() if(D1.operator == (D2))
{ {
cout<<feet<<", "<<inches<<endl; Distance D1, D2;
} D1.get();
int operator==(const Distance &); D2.get();
}; D1.show();
D2.show();
if(D1 == D2)
cout<<"Distances are equal"<<endl;
else
cout<<"Distances are not equal"<<endl;
return 0;
}
Output of The Previous Code

Output

Enter feet and inches: 0 12


Enter feet and inches: 1 0
0, 12
1, 0
Distances are equal

Process returned 0 (0x0) execution time : 8.928 s


Press any key to continue.
Assignment

#include <iostream> int main()


{
using namespace std; String S1 = "Hello";
String S2 = "How are you?";
class String String S3;
{
.
S3 = S1 + S2; “Hello”
private: S1.show();
char str[20]; S2.show(); “How are you?”
public: S3.show();
//you have to code it return 0;
}; }
“Hello How are you?”
End of Lecture 24
For any queries mail us @: [email protected]
Call us @ : 0755-4271659, 7879165533

Thank you

You might also like