0% found this document useful (0 votes)
46 views17 pages

Unit 2 OperatorOverloading

Operator overloading allows redefining how operators work for user-defined types. It allows operators like + and << to be used to concatenate strings and output objects to streams. Common operators that can be overloaded include arithmetic, comparison, stream insertion/extraction, function call, and subscript operators. Overloading is done by defining operator functions that take the left and right operands. Unary operators are overloaded as member functions, while binary operators are overloaded as member or non-member functions.

Uploaded by

mynavathir
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
46 views17 pages

Unit 2 OperatorOverloading

Operator overloading allows redefining how operators work for user-defined types. It allows operators like + and << to be used to concatenate strings and output objects to streams. Common operators that can be overloaded include arithmetic, comparison, stream insertion/extraction, function call, and subscript operators. Overloading is done by defining operator functions that take the left and right operands. Unary operators are overloaded as member functions, while binary operators are overloaded as member or non-member functions.

Uploaded by

mynavathir
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 17

UNIT - 2

Operator Overloading

1
Operator Overloading
• Introduction
– More than one definition either to a function or
an operator
– Function – Function overloading
– Operator – Operator overloading

2
Operator Overloading
• Intro to operator overloading
– Redefine the normal working of an operator
– Change the way that an operator works
– Used for user defined data types
– Eg : + is used to add two integer values
– But can also be used to concatenate two
strings.
(“Welcome” + “ C++” – Welcome C++ )
3
Introduction to C++

Operator
Overloading

4
What is..Operator Overloading
• Operator Overloading:

– Allows us to define the behavior of operators when


applied to objects of a class
– Already overloaded operators : cin and cout

– input_data is replaced by >>

– display is replaced by <<

– cin >> number1; cout << number1;

– assign or copy is replaced by = ; s2 = s1; 5


Operator Overloading - Syntax
return_type class_name  : : operator op(argument_list) 

// function body

6
Restrictions
• Operators do not lose their original meaning; instead, they have an additional
meaning when overloaded.
• Only existing operators with given restrictions can be overloaded.

• The precedence of the operators remains the same.

• Operators cannot change the number of arguments that were available in the
original form.
• Operators can only be overloaded for user-defined types.

• Except (), no other operator can have a default argument.

• Some operators can never be overloaded.

7
Operators that cannot be overloaded
The following operators cannot be overloaded:
• Dot operator for member access (.)

• Dereference member to class operator (.*)

• Scope resolution operator (::)

• Size of operator (sizeof)

• Conditional ternary operator (?:)

• Casting operators

• # and ## tokens for macro preprocessors

8
Operators that cannot be overloaded
The following operators cannot be overloaded through friend
functions:
• Assignment operator =

• Function call operator ( )

• Array subscript operator [ ]

• Access to class member using pointer to object operator ->

9
Operator Overloading – Eg.
class Complex { void print()
private: {
    int real, imag; cout << real << " + i"
public:
<< imag << endl;
    Complex(int r = 0, int i =0) 
}
{
real = r;   imag = i; };
}  int main()
    Complex operator + (Complex const & obj) {
{     Complex c1(10, 5), c2(2, 4);
         Complex res;     Complex c3 = c1 + c2;
         res.real = real + obj.real;     c3.print();
         res.imag = imag + obj.imag;
}
         return res;
    }    Output:
12 + i9 10
Overloading Unary Operators
• Operators that have a single argument are known as unary
operators.
• When these operators are overloaded as member
functions, it is not necessary to pass any argument
explicitly.
• The this pointer pointing to the invoking object is passed
as an implicit argument.

11
Unary Operator Overloading – Eg.
class Matrix void operator −()
{ int Element[3][3]; {
public: for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
Matrix(){}; // Default constructor
Element[i][j] = −Element[i][j];
Matrix(int TempMatrix[3][3]) }
{ for(int i = 0; i < 3; i++) void Display()
for(int j = 0; j < 3; j++) {
Element[i][j] = TempMatrix[i][j]; for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
}
{
void Read() cout << Element[i][j] << " ";
{ for(int i = 0; i < 3; i++) }
for(int j = 0; j < 3; j++) cout << "\n";
cin >> Element[i][j]; }
};
}
12
Unary Operator Overloading – Eg.
void main()
{ int ArrayOfInt[][3]={1,2,3,4,5,6,7,8,9};
Matrix M1(ArrayOfInt);
cout << "The fi rst matrix before negation \n";
M1.Display();
cout << "First matrix after negation \n"; −M1;
M1.Display();
Matrix M2;
cout << "Enter values for the second matrix \n";
M2.Read();
cout << "The second matrix before negation \n";
M2.Display(); −M2;
cout << "Second matrix after negation \n";
M2.Display();
}
13
Overloading ++ (Prefix / Postfix)
class Complex
{ float Real; float Imag;
public:
Complex (fl oat TempReal = 0, float TempImag = 0)
{
Real = TempReal; Imag = TempImag;
}
Complex Add(Complex Comp2)
{ float TempReal; float TempImag;
TempReal = Real + Comp2.Real;
TempImag = Imag + Comp2.Imag;
return Complex(TempReal, TempImag);
}

14
Overloading ++ (Prefix / Postfix)
Complex operator +(Complex Comp2)
{ float TempReal; float TempImag;
TempReal = Real + Comp2.Real;
TempImag = Imag + Comp2.Imag;
return Complex(TempReal, TempImag);
}
Complex operator ++() // This is prefi x
{
Real++; return Complex(Real, Imag);
}
Complex operator ++(int dummy) // This is postfi x
{
Imag++; return Complex(Real, Imag);
}
15
Overloading ++ (Prefix / Postfix)
void Display()
{
cout << Real << " + " << Imag << "i \n";
} };
void main()
{ Complex Comp1(10, 20); Complex Comp2(20, 30);
Complex CompResult1, CompResult2;
CompResult1 = Comp1.Add(Comp2);
CompResult1.Display();
CompResult2 = Comp1 + Comp2;
CompResult2.Display();
Comp1++; // The postfix version with dummy arguments Comp1.Display();
++Comp2; // The prefix version without any arguments Comp2.Display();
}
16
Overloading Binary Operators
• Operators that take two operands are known as binary
operators.
• They will have a single argument when defined as
member.
• The first argument to that operator is always the invoking
object.
• Eg: overloading the ‘+’ operator for complex numbers

17

You might also like