0% found this document useful (0 votes)
3 views15 pages

11.Operatoroverloading Part1.Pptx

Operator overloading in C++ allows programmers to redefine operators to work with user-defined types, enhancing code readability and functionality. It can be implemented using member functions or friend functions, and supports both unary and binary operators. Certain operators, however, cannot be overloaded, and the document outlines various examples and syntax for implementing operator overloading.

Uploaded by

aragavendran2711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views15 pages

11.Operatoroverloading Part1.Pptx

Operator overloading in C++ allows programmers to redefine operators to work with user-defined types, enhancing code readability and functionality. It can be implemented using member functions or friend functions, and supports both unary and binary operators. Certain operators, however, cannot be overloaded, and the document outlines various examples and syntax for implementing operator overloading.

Uploaded by

aragavendran2711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Operator Overloading

Outline
❖ Why we need operator overloading?

❖ What is operator overloading?

❖ Operator overloading using member function

❖ Operator overloading using friend function

❖ What are the operators cannot be overloaded?

Operator overloading in C++


Why we need operator overloading?
❖ To redefine or overload the operators

❖ Thus, a programmer can use operators with user-defined types

Class Complex
{ private:
int real, img;
};
Int main()
{
Complex comp1,comp2,comp3;
cin>>comp1>>comp2; // extraction operator >> is overloaded
comp3=comp1+comp2; // arithmetic operator + is overloaded
cout<<comp3; // insertion operator << is overloaded
If (comp1==comp3) // relational operators == is overloaded
comp3 +=comp1 // arithmetic assignment operator += is overloaded
}
Operator overloading in C++
What is operator overloading?
❖ It is a type of compile time polymorphism in which an operator is overloaded to give
user defined meaning to it
❖ Operator overloading is a way of providing new implementation of existing operators
to work with user-defined data types
❖ A new implementation can be done by defining a special function called operator
function

❖ There are two types of operator overloading in C++

Binary Operator Overloading


Unary Operator Overloading

❖ Unary operator operates on single operand where as the binary operator operates on
two operands
❖ Hence two different operator function formats are required to overload unary and
binary operator

Operator overloading in C++


Cont…
❖ overloadable operators in c++

+ - * / % ^

& | ~ ! , =

< > <= >= ++ --

<< >> == != && ||

+= -= /= %= ^= &=

|= *= <<= >>= [] ()

-> ->* new new [] delete delete []

Operator overloading in C++


Operator overloading using member function
Syntax:
ReturnType classname :: operator OperatorSymbol(argument List)
{ // function body }

Number of arguments: Keyword Operator to be overloaded

Unary operator overloading -zero

Binary operator overloading – one


Example:

Unary operator overloading


void classname :: operator ++ ()
{ // function body }

Binary operator overloading


void classname :: operator * (classname obj)
{ // function body }
Operator overloading in C++
Unary operator overloading using member function
unary minus (-) operator
class Distance {
int main() {
private:
Distance D1(11, 10), D2(-5, 11);
int feet;
-D1; // D1.operator -();
int inches;
D1.displayDistance();
public:
-D2; // D2.operator -();
Distance(int f, int i) {
D2.displayDistance();
feet = f;
return 0;
inches = i;
}
}
Output:
void displayDistance() {
F: -11 I:-10
cout << "F:" << feet << "I:" << inches <<endl;
F: 5 I:-11
}
void operator- () {
feet = -feet;
inches = -inches;
}
};
Operator overloading in C++
Unary operator overloading using member function
negation (!)operator
class NUM
{ int main()
private: {
int n; NUM num(5);
public: cout << "Before calling Operator Overloading:";
NUM(int x) num.dispNum();
{ n=x; } !num;
void dispNum() cout << "After calling Operator Overloading:";
{ num.dispNum();
cout << "value of n is: " << n<<endl; return 0;
} }
void operator ! ( )
{ n=!n; }
};

Output:
Before calling Operator Overloading: value of n is: 5
After calling Operator Overloading: value of n is: 0
Operator overloading in C++
Unary operator overloading using member function
Prefix (++) Increment Operator
class Check int main()
{ {
private: Check obj;
int i; cout<<"obj:";
public: obj.Display();
Check(): i(0) { } ++obj; //obj.operator++();
void operator ++() cout<<"++obj:";
{ obj.Display();
++i; return 0;
} }
void Display()
{
cout << “ i=" << i << endl;
}
}; Output:
obj: i=0
++obj: i=1

Operator overloading in C++


Unary operator overloading using member function
Postfix ++ Increment Operator
class Check int main()
{ {
private: Check obj;
int i; cout<<"obj:";
public: obj.Display();
Check(): i(0) { } obj++; //obj.operator++(int);
void operator ++(int) cout<<"obj++:";
{ i++; } obj.Display();
void Display() return 0;
{ cout << “ i=" << i << endl; } }
};

❖ By using “dummy argument”


❖ This argument is a fake integer parameter that only serves to distinguish
the postfix version of increment/decrement from the prefix version
Output:
obj: i=0
obj++: i=1
Operator overloading in C++
Unary operator overloading using member function
Postfix ++ Increment Operator

Operator overloading in C++


Unary operator overloading using member function
Prefix, Postfix( -- )decrement Operator
class Check int main()
{ {
private: Check obj;
int i; cout<<"obj: ";
public: obj.Display();
Check():i(5){ } --obj; //obj.operator--();
void operator --() cout<<"--obj:";
{ --i; } obj.Display();
void operator --(int) obj--; //obj.operator--(int);
{ i--; } cout<<"obj--:";
void Display() obj.Display();
{ cout << " i=" << i << endl; } return 0;
}; }

Output:
obj: i=5
--obj: i=4
obj--: i=3
Operator overloading in C++
Unary operator overloading using member function
Prefix, Postfix( -- )decrement Operator

Output:
obj: i=5
--obj: i=4
obj--: i=4
Operator overloading in C++
()-Parentheses (function call) Operator Overloading using
member function
class Distance { void displayDistance() {
private: cout << "F: " << feet << " I:" << inches << endl;
int feet,inches;
}
public:
};
Distance():feet(0),inches(0)
int main() {
{}
Distance D1(11, 10), D2;
Distance(int f, int i) :feet(f),inches(i)
{ } cout << "First Distance : ";
Distance operator()(int f, int i ) { D1.displayDistance();
Distance D; D2 = D1(5, 10); // D1.operator(5,10);
D.inches = inches+i ; cout << "Second Distance D1+(5,10) :";
if(D.inches>=12) D2.displayDistance();
{ D.feet = feet+f+1; return 0;
D.inches-=12; }
}
else Output:
D.feet = feet+f; First Distance : F: 11 I:10
return D; Second Distance D1+(5,10) :F: 17 I:8
}
Operator overloading in C++
Subscript or array index operator ([ ] ) overloading using
member function
class Array void print()
{ { for(int i = 0; i < size; i++)
private: cout<<array[i]<<" ";
int *array; cout<<endl;
int size; }
public: };
Array(int *temp,int s) int main()
{ array=temp; { int a[] = {1, 2, 4, 5};
size=s; Array arr1(a, 4);
} arr1.print();
int &operator[](int index) arr1[2] = 6;
{ arr1.print();
if (index >= size) arr1[8] = 6;
{ return 0;
cout << "Array index out of bound, exiting"; }
exit(0); Output:
} 1245
return array[index]; 1265
} Array index out of bound, exiting
Operator overloading in C++

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy