C++ Lecture-24

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

01

02

LECTURE 24
04
Today’s Agenda
01 Operator Overloading

02 What is Operator Overloading?

03 Techniques of Overloading Operators

A program to overload unary operator ++


04 (pre increment) as member function

05 Predict the output/behavior of the given code


05

05
Operator Overloading

In the previous class, we discuss the 4 ways of adding two different objects of the same class and the
following are the calls of the methods

What if we do Let say D3 = D1 + D2;


the same thing
1. D3.add(D1, D2);
in a new way
2. D3 = D1.add(D2);
3. D3 = add(D1, D2);
4. add(D1, D2, D3); By default "+" cannot This the most suitable
add Distances, but if syntax in context of
we want to do this understanding,
In all the above 4 ways we added two objects writing and as well as
then C++ suggests us
and store there result in the third one
to apply OPERATOR reading
OVERLOADING
What is Operator Overloading?

Operator Overloading in C++ is a mechanism using which a programmer can redefine the built-in operators
of C++ language in such a way that these operators can now work upon objects of programmer-defined
classes much in the same way they work upon variables of primitives data types.

In other words, we can say that Operator Overloading increases the domain of operators from variables to
objects.

The most important benefit of Operator Overloading is simplicity in the readability of the call. That is,
function calls which are given using Operator Overloading are much easier to understand and write as
compared to the conventional way of giving function calls
Techniques of Overloading Operators

What will happen if this line will execute


It will add the data
members of the
objects D1 and D2
D3 = D1 + D2
(feet and inches of D1
and D2)

What will be the


operator + will do

And we know that the private data members can only be accessed by the member
functions or by the friend functions of the class
Operator Overloading

Since operators will have to access the private data members of the class while working on
objects, so either these operators have to make MEMBER FUNCTIONS or they have to be
made FRIEND FUNCTIONS

Syntax of Overloading Operators:

1. As members functions:

<return_type> operator <operator_symbol> (<argument>);

2. As friend functions:

friend <return_type> operator <operator_symbol> (<argument>);


A program to overload unary operator ++ (pre-increment)
as member function

class Counter
{ void Counter::operator++()
private: {
int count; ++count;
public: }
Counter()
{ int main()
count = 0; {
} Counter C1(10);
Counter(int c) C1.show();// => 10
{ ++C1;//C1.operator++();//This will be converted into this form
count = c; by the compiler during generation of machine code
} C1.show();// => 11
void show() return 0;
{ }.
cout<<count<<endl;
}
void operator++();
};
Output of The Previous Code

Output

10
11

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


Press any key to continue.
Guess The Output

class Counter
{ void Counter::operator++() What should be the
private: {
int count; ++count;
output?
public: }
Counter()
What will be the output? If everything is OK,
{ int main()
count = 0; { then the output should
}
Syntax Error Counter C1(10); be 11 and 11
Counter(int c) Counter C2;
{ C1.show();
count = c; C2.show();
} C2 = ++C1; C2 = C1.operator++();
void show() C1.show();
{ C2.show();
cout<<count<<endl; return 0;
} }
void operator++(); C2 = _;
};
End of Lecture 24
For any queries mail us @: scalive4u@gmail.com
Call us @ : 0755-4271659, 7879165533

Thank you

You might also like