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

Operator Overloading More Operators

The document discusses operator overloading in C++. It covers overloading binary operators like +, -, *, / etc. for a complex number class. It also discusses overloading unary operators like ++, -- and unary negation. An example is provided to overload operators for a complex number class to allow arithmetic operations on complex numbers. The class should implement getter/setter methods and overloaded operators for addition, subtraction, multiplication and division.

Uploaded by

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

Operator Overloading More Operators

The document discusses operator overloading in C++. It covers overloading binary operators like +, -, *, / etc. for a complex number class. It also discusses overloading unary operators like ++, -- and unary negation. An example is provided to overload operators for a complex number class to allow arithmetic operations on complex numbers. The class should implement getter/setter methods and overloaded operators for addition, subtraction, multiplication and division.

Uploaded by

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

Today’s Lecture

• Binary Operator overloading (+, -, *, /, %)


• Unary Operator overloading (++, --, -)
• Pre increment (++a,--a)
• Post increment (a++, a++)
• Unary negation operator (-a)
• Relational Operator(>, >=, <, <=, ==)
• Subscript Operator or Array index operator []
• Stream insertion << stream extraction >>
Operator Overloading (Binary Operator)
• Operator overloading is an important concept in C++. It is a type of
polymorphism in which an operator is overloaded to give user-defined meaning to
it
• An operator with two operands to perform a mathematical operation is called the
Binary Operator Overloading.
• There are multiple binary operators, like +, -, *, /,%, etc., can directly
manipulate or overload the object of a class.
Operator Overloading Syntax
Operator Overloading (Class Activity)
Write a C++ program to create a complex number class. Write getter setter, constructor, destructor, and all overloaded operators needed to run
the given driver code.
class complex {
private:
int real;
int imag;
public:
//All functions that are needed. };
int main() {
complex c1(10,7); // for complex numbers, such as 10+7i
complex c2(5); // for numbers without imaginary part as 5 + 2i
complex c3();
complex c4();
c3 = c1+c2;
c4 = c1-c2;
c5 = c3*c4;
c6 = c5/c4; }
Operator Overloading(main.cpp)
#include <iostream> //complex c3; cout << endl;
#include "Header.h" cout << " Division: " << endl;
cout << endl;
cout << " Addition: " << endl; complex c6 = c5 / c4;
using namespace std; complex c3 = c1 + c2; c6.display4();
c3.display();

int main()
cout << endl;
{ cout << " Subtraction: " << endl; cout << endl;
{ complexc4 = c1 - c2;
complex c1(10, 7); c4.display2();
}
c1.display();
cout << endl; system("pause");
cout << " Multiplication: " << endl;
complex c2(5); complex c5 = c3 * c4;
c2.display(); c5.display3(); }
Operator Overloading (Header.h)
#pragma once void display();
class complex { void display2();
private:
void display3();
int real;
void display4();
int img;

public: complex operator+(complex &other);


complex operator-(complex& other);
complex(); // Default Constructor complex operator*(complex& other);
complex(int r, int i);
complex operator/(complex& other);
complex(int r);

~complex();
int set_real(int r);
int set_img(int i);
};
int get_real();
int get_img();
Operator Overloading (Source1.cpp)
#include "Header.h" int complex::set_real(int r) void complex::display()
{ {
#include <iostream>
using namespace std; real = r; cout << " Calculation is " << real << " + " << img << "i " << endl;
return real; }
}
complex::complex()
{ void complex::display2()
int complex::set_img(int i)
int real=0; {
{
int img=0;
img = i; cout << " Calculation is " << real << " - " << img << "i " << endl;
}
return img; }
}
complex::complex(int r)
{ void complex::display3()
int complex::get_real()
real = r; {
{
img = 0;
return real; cout << " Calculation is " << real << " * " << img << "i " << endl;
} } }
int complex::get_img()
complex::complex(int r, int i){
void complex::display4()
{ return img;
{
real = r; }
img = i;
cout << " Calculation is " << real << " / " << img << "i " << endl;
} }
Operator Overloading (Source1.cpp)
complex complex::operator+(complex & complex complex::operator*(complex & other)
other)
{
{
complex save_info; complex::~complex()
complex save_info;
{
save_info.real = real * other.real;
save_info.real = real + other.real;
save_info.img = img * other.img; cout << " Destruction Called " << endl;
save_info.img = img + other.img;
}
return save_info;
return save_info;
}
}

complex complex::operator-(complex&
complex complex::operator/(complex& other)
other)
{
{
complex save_info;
complex save_info;

save_info.real = real / other.real;


save_info.real = real - other.real;
save_info.img = img / other.img;
save_info.img = img - other.img;

return save_info;
return save_info;
}
}
Class Activity
•Write a C++ program that initially takes the length of the two matrixes from
the user and puts the values in both matrixes, then performs addition using the
operator overloading concept
Operator Overloading (+) (Header.h)
#pragma once void set_data()
#include<iostream> {
using namespace std; twodarr = new int* [rows];
   
class matrix_addition { for (int i = 0; i < rows; i++)
{
private:
twodarr[i] = new int[cols];
int rows, cols;
}
public:  
int** twodarr; for (int i = 0; i < rows; i++)
int** save_data; {
  for (int j = 0; j < cols; j++)
public: {
matrix_addition() {  
  cin >> twodarr[i][j];
} }
  }
int set_rows() }
{
void show_data()
cout << " Enter the number of rows:
{
"; for (int i = 0; i < rows; i++)
cin >> rows; {
return rows; for (int j = 0; j < cols; j++)
} {
   
int set_cols() cout << twodarr[i][j];
{ cout << "\t";
cout << " Enter the number of column:  
"; }
cin >> cols; cout << endl;
return cols; }
}
}
Operator Overloading (+) (Header.h)
void operator +(matrix_addition& other)
{
save_data = new int* [rows];
 
for (int i = 0; i < rows; i++)
{
save_data[i] = new int[cols];
}
 
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
save_data[i][j] = twodarr[i][j] + other.twodarr[i][j] ;
cout<< save_data[i][j];
cout << "\t";

}
cout << endl;

}
}
 
 
 
};
Operator Overloading (+) (Source.cpp)
#include<iostream>
#include "Header.h"
using namespace std;
 
int main()
{
matrix_addition obj1, obj2;
 
obj1.set_rows();
obj1.set_cols();
 
obj2.set_rows();
obj2.set_cols();
 
obj1.set_data();
obj2.set_data();
 
obj1.show_data();
cout << endl;
obj2.show_data();
cout << endl;
 
obj1 + obj2;
 
cout << "\n";
system("pause");
}
Operator Overloading (Unary Operator)
• Unary operators are used to calculating the result on only one operand.
• Unary operators are used on a single operand to calculate the new value of that
variable
• Unary Operator overloading (++, --, -)
• Pre increment (++a,--a)
• Post increment (a++, a++)
• Unary negation operator (-a)
Operator Overloading(source.cpp)
#include <iostream> complex c5 = --c4; // pre - increment operator
c5.display3();
#include "Header.h"

complex c6 = c5++; // post - increment operator


using namespace std;
c6.display4();

int main() complex c7 = c6--; // post - increment operator


{ c7.display5();
{
complex c8 = -c7;
complex c1(10, 7);
c8.display6();
c1.display();

}
system("pause");
complexc4= ++c1; // pre - increment operator
c4.display2(); }
Operator Overloading (Header.h)
#pragma once
class complex {
complex operator++();
private:
int real; complex operator--();
int img; complex operator++(int);
complex operator--(int);
public:
complex operator-();
complex(); // Default Constructor
complex(int r, int i);

~complex();
void display();
void display2();
void display3(); };
void display4();
void display5();
void display6();
Operator Overloading (Source1.cpp)
complex complex::operator++()
#include "Header.h" complex complex::operator--(int)
{ complex temp;
#include <iostream> {
temp.real = ++real;
complex temp;
using namespace std; temp.img = ++img;
real=real--;
return temp; }
temp.real = real;
complex complex::operator--()
complex::complex() { img = img--;
complex temp; temp.img = img;
{
temp.real = --real; return temp;
real=0; temp.img = --img; }
img=0; return temp;
} complex complex::operator-()
} complex complex::operator++(int) {
{
complex temp;
complex temp;
complex::complex(int r, int i) real = -real;
real = real++;
temp.real = real;
{ temp.real = real;
img = -img;
real = r; img = img++;
temp.img = img;
temp.img = img;
img = i; return temp; return temp;

} } }
Operator Overloading (Source1.cpp)
void complex::display()

{ void complex::display5()
cout << " Calculation is " << real << " + " << img << "i " << endl; {
}
cout << " Post Decrement of real and Img is: " << real << " + " <<
img << "i " << endl;

void complex::display2() }

cout << " Pre Increment of real and Img is: " << real << " + " << img << "i " << endl; void complex::display6()

} {

cout << " Negation Operator of real and Img is: " << real << " + "
<< img << "i " << endl;
void complex::display3()
}
{

cout << " Pre Decrement of real and Img is: " << real << " + " << img << "i " << endl;

}
complex::~complex()

void complex::display4() cout << " Destruction Called " << endl;

{ }

cout << " Post Increment of real and Img is: " << real << " + " << img << "i " << endl;

}
Output of the following program
Operator Overloading (Relational Operator)
• There are various relational operators supported by C++ language like ( <, >, <=,
>=, ==, etc.) which can be used to compare C++ built-in data types.
• You can overload any of these operators, which can be used to compare the
objects of a class.
Operator Overloading(source.cpp)
#include <iostream> cout << " Relational Operator c1>c2 " << endl;
#include "Header.h" if (c1 > c2)
{
using namespace std; cout << " c1 values are greater: " << endl;
}
else
int main()
cout << " c2 values are greater: " << endl;
{
cout << endl;
{
complex c1(10, 20);
complex c2(20, 30);
cout << " Relational Operator c1>=c2 " << endl;
complex c3(5, 50);
if (c1 >= c2)
complex c4(10, 20); {
c1.display(); cout << " c1 values are greater and equal to c2: " << endl;
c2.display2(); }
c3.display3(); else
c4.display4(); cout << " c1 values are not less than and equal to c2: " << endl;
cout << endl; cout << endl;
Operator Overloading(source.cpp)
cout << " Relational Operator c1<c2 " << endl;
if (c1 < c2)
cout << " Relational Operator c1==c4 " << endl;
{
if (c1 == c4)
cout << " c1 values are smaller: " << endl;
{
}
cout << " c1 values are == to c2: " << endl;
else
}
cout << " c2 values are greater: " << endl;
else
cout << endl;
cout << " c1 values != to c2: " << endl;
cout << endl;
cout << " Relational Operator c1<=c2 " << endl;
if (c1 <= c2)
{ }
cout << " c1 values are less and equal to c2: " << endl; cout << endl;
} system("pause");
else
cout << " c1 values are not less than and equal to c2: " << endl;
}
cout << endl;
Operator Overloading (Header.h)
#pragma once void display();
class complex {
void display2();
private:
void display3();
int real;
int img; void display4();

public:

~complex();
complex(); // Default Constructor
complex(int r, int i);
};
bool operator >(const complex &other);
bool operator >=(const complex& other);
bool operator <(const complex& other);
bool operator <=(const complex& other);
bool operator ==(const complex& other);
Operator Overloading (Source1.cpp)
bool complex::operator >=(const complex& other)
#include "Header.h"
{
#include <iostream>
using namespace std; if (real > other.real)
return true; bool complex::operator == (const complex& other)
complex::complex() else {
{ return false;
if (real == other.real && img==other.img)
real=0; }
img=0; return true;
} bool complex::operator <(const complex& other)
else
{
complex::complex(int r, int i) return false;
if (real < other.real)
{ return true; }
real = r; else
img = i; return false;
}
}

bool complex::operator >(const complex&


other) bool complex::operator <= (const complex & other)
{ {
if (real > other.real) if (real < other.real)
return true; return true;
else else
return false; return false;
} }
Operator Overloading (Source1.cpp)
void complex::display()
{
cout << " Values of real and img of c1: " << real << " and Imag is: " << img << "i " << endl;
}
void complex::display2()
{
cout << " Values of real and img of c2: " << real << " and Imag is: " << img << "i " << endl;
}
void complex::display3()
{
cout << " Values of real and img of c3: " << real << " and Imag is: " << img << "i " << endl;
}
void complex::display4()
{
cout << " Values of real and img of c4: " << real << " and Imag is: " << img << "i " << endl;
}
complex::~complex()
{
cout << " Destruction Called " << endl;
}
Output program
Subscript Operator or Array index operator
• Subscript operator [ ], like the function-call operator, is considered a binary
operator. The declaration is identical to any binary operator, with the following
exception:
• It can not be declared as a non-member function. It must be a non-static member
function.
• It should take a single argument. The argument can be of any type and designates
the desired array subscript.
Operator Overloading(source.cpp)
#include <iostream>
#include "Header.h"
using namespace std;
int main()
{
{
complex c1;
c1.set_marks();
c1[3];
c1.display();
}

cout << endl;


system("pause");
}
Operator Overloading (Header.h)
#pragma once
class complex {
private:
int real;
int img;
int* marks;
int size;
int temp;

public:
complex();
complex(int R, int I);

void set_marks();
int operator[](int index);

void display();

};
Operator Overloading (Source1.cpp)
#include "Header.h" void complex::set_marks()
#include <iostream> {
using namespace std; cout << "Enter the index array: ";
cin >> size;
complex::complex()
marks = new int[size];
{
real = 0;
for (int i = 0; i < size; i++)
img = 0;
{
}
cout << " Enter the [" << i << "] index value: ";
complex::complex(int R, int I) cin >> marks[i];
{ }
real = R; }
img = I;
}
Operator Overloading (Source1.cpp)
int complex::operator[](int index)
{
if (index >= size)
cout << " Index out of bound: ";
temp = marks[index];
return temp;
}

void complex::display()
{
cout << " The value at index is " << temp << endl;
}
Stream insertion << stream extraction >>
• In C++, stream insertion operator “<<” is used for output and extraction operator
“>>” is used for input. 
Stream insertion << stream extraction >>
#include<iostream> istream &operator>>(istream &input, complex& other)
using namespace std; {
class complex {
input >> other.real;
private:
input >> other.img;
int real;
return input;
int img;
public: }
complex()
{
real = 0; ostream& operator<<(ostream& output, complex& other)
img = 0;
{
}
output << other.real;
complex(int R, int I)
output << "+";
{
real = R; output << other.img;
img = I; output << "i";
} return output;
friend istream &operator >>(istream &input, complex& other);
}
friend ostream& operator <<(ostream& output, complex& other);

};
Stream insertion << stream extraction >>
int main()
{
{
cout << "Enter the real and img value: ";
complex c1;
cin >> c1;

cout << " Values of real and img are: ";


cout << c1;
}
cout << endl;
system("pause");
}
Operators that cannot be overloaded
The list of operators which cannot be overloaded is as follows:
• Conditional or Ternary Operator (?:) cannot be overloaded.
• Size of Operator (sizeof) cannot be overloaded.
• Scope Resolution Operator (::) cannot be overloaded.
• Class member selector Operator (.) cannot be overloaded.
• Member pointer selector Operator (.*) cannot be overloaded.
• Object type Operator (typeid) cannot be overloaded.

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