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

Week-12

The document provides an overview of operator overloading in object-oriented programming, detailing how to overload both unary and binary operators with examples. It discusses the use of friend functions in operator overloading and includes rules and important points to consider. Additionally, it references various resources for further reading on the topic.

Uploaded by

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

Week-12

The document provides an overview of operator overloading in object-oriented programming, detailing how to overload both unary and binary operators with examples. It discusses the use of friend functions in operator overloading and includes rules and important points to consider. Additionally, it references various resources for further reading on the topic.

Uploaded by

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

Object Oriented Programming

Concepts

Object Oriented Programming


Outline
2

 Operator overloading – overview


 Operator overloading - overloading basic operators with detailed
examples.
 Operator overloading and Friend functions.
Operator Overloading
3
Operator Overloading
4
Operator Overloading (Unary Operators)
5

#include <iostream>
using namespace std;
class Count {
private:
int value;

Example 1 public:
// Constructor to initialize count to 5
int main() { Count() : value(5) {}
Count count1; // Overload ++ when used as prefix
void operator ++ () {
// Call the "void operator ++ ()" function ++value;
++count1; }

count1.display(); void display() {


return 0; cout << "Count: " << value << endl;
} }
};
Operator Overloading (Unary Operators)
6
Operator Overloading (Unary Operators)
7
Operator Overloading (Unary Operators)
8

Example 2 int main() {


#include <iostream> Count count1;
using namespace std;
class Count { // Call the "void operator ++ (int)" function
private: count1++;
int value; count1.display();
public:
// Constructor to initialize count to 5 // Call the "void operator ++ ()" function
Count() : value(5) {} ++count1;
// Overload ++ when used as prefix
void operator ++ () { ++value;} count1.display();
// Overload ++ when used as postfix return 0;
void operator ++ (int) { }
value++;
}
void display() {
cout << "Count: " << value << endl;
}
};
Operator Overloading (Unary Operators)
9
Operator Overloading (Unary Operators)
10

// Overload ++ when used as postfix


Example 3: Return Value from Operator
Count operator ++ (int) {
Function (++ Operator)
Count temp;
#include <iostream> // Here, value is the value attribute of the calling object
using namespace std; temp.value = value++;
class Count { return temp;
private: }
int value; void display() {cout << "Count: " << value << endl;}
public: };
// Constructor to initialize count to 5 int main() {
Count() : value(5) {} Count count1, result;
// Overload ++ when used as prefix // Call the "Count operator ++ ()" function
Count operator ++ () { result = ++count1;
Count temp; result.display();
// Here, value is the value attribute of the calling object // Call the "Count operator ++ (int)" function
temp.value = ++value; result = count1++;
return temp; result.display();
} return 0;
}
Operator Overloading (Unary Operators)
11
Operator Overloading (Binary Operators)
12
Operator Overloading (Binary Operators)
13

class Complex { void output() {


private: if (imag < 0)
float real; cout << "Output Complex number: " << real << imag << "i";
float imag; else
public: cout << "Output Complex number: " << real << "+" << imag
// Constructor to initialize real and imag to 0 << "i";}
Complex() : real(0), imag(0) {} };
void input() { int main() {
cout << "Enter real and imaginary parts respectively: "; Complex complex1, complex2, result;
cin >> real; cout << "Enter first complex number:\n";
cin >> imag; complex1.input();
} cout << "Enter second complex number:\n";
// Overload the + operator complex2.input();
Complex operator + (const Complex& obj) { // complex1 calls the operator function
Complex temp; // complex2 is passed as an argument to the function
temp.real = real + obj.real; result = complex1 + complex2;
temp.imag = imag + obj.imag; result.output();
return temp; return 0;
} }
Operator Overloading (Binary Operators)
14
Operator Overloading (Binary Operators)
15
Operator Overloading (Binary Operators)
16
Operator Overloading (Binary Operators)
17
Conversion Operator
18
Conversion Constructor
19
Assignment Operator vs Copy Constructor
20
When should we write our own assignment operator in C++
21
When should we write our own assignment operator in C++
22
When should we write our own assignment operator in C++
23
Assignment Operator vs Copy Constructor
24
Operator Overloading and Friend Function
25
Operator Overloading and Friend Function(Example1)
26
Operator Overloading and Friend Function(Example2)
27
Operator Overloading and Friend Function(Example2)
28
Operator Overloading and Friend Function(Example3)
29
Important Points Operator Overloading
30
Important Points Operator Overloading
31
Rules for Operator Overloading
32
Rules for Operator Overloading
33
Rules for Operator Overloading
34
References
35

• https://
harshityadav95.medium.com/object-oriented-programming-c-d2
42601be045
• https://
www.programiz.com/cpp-programming/operator-overloading
• https://www.geeksforgeeks.org/copy-constructor-vs-assignment-
operator-in-c
/
• https://
www.tutorialride.com/cpp/friend-function-using-operator-overlo
ading-in-c.htm
36

THANK YOU

You might also like