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

EXP-10 Operator overloading

Operator overloading in C++ allows the definition of custom behaviors for operators when used with user-defined types like classes and structures. Certain operators, such as scope resolution and member selection, cannot be overloaded. The document provides examples demonstrating how to overload the + operator and the NOT (!) operator for a Complex class and a NotOp class, respectively.

Uploaded by

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

EXP-10 Operator overloading

Operator overloading in C++ allows the definition of custom behaviors for operators when used with user-defined types like classes and structures. Certain operators, such as scope resolution and member selection, cannot be overloaded. The document provides examples demonstrating how to overload the + operator and the NOT (!) operator for a Complex class and a NotOp class, respectively.

Uploaded by

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

10.

Operator overloading in c++


 In C++, we can define how operators behave for user-defined types like class and structures. For
example,
 The + operator, when used with values of type int, returns their sum. However, when used with
objects of a user-defined type, it is an error.
 In this case, we can define the behavior of the + operator to work with objects as well.
 This concept of defining operators to work with objects and structure variables is known
as operator overloading.

 We cannot overload following operators in C++:


:: (scope resolution)
. (member selection)
.* (member selection through pointer to function)
?: (ternary operator)
sizeof operator
typeid Operator

Example-1// C++ Program to Demonstrate Operator Overloading


#include <iostream>
using namespace std;

class Complex {
private:
int real, imag;

public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}

// This is automatically called when '+' is used with


// between two Complex objects
Complex operator+(Complex const& obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << '\n'; }
};

int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
}

Output
12 + i9

Example 2:
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;

public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
void print() { cout << real << " + i" << imag << endl; }
// The global operator function is made friend of this
// class so that it can access private members
friend Complex operator+(Complex const& c1,
Complex const& c2);
};
Complex operator+(Complex const& c1, Complex const& c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3
= c1
+ c2; // An example call to &quot;operator+&quot;
c3.print();
return 0;
}

Output
12 + i9

Example 3: Operator overloading


#include <iostream>
using namespace std;
class NotOp {
private:
int a;
bool b;

public:
NotOp() : a(0), b(true) {}

void in() {
cout << "Enter the first number : ";
cin >> a;
cout<< "Enter true or false : ";
cin >> b;
}

// Overloading the NOT (!) operator


void operator ! () {
a= !a;
b= !b;
}

void out() {
cout<<"Output: "<<endl<< a<<endl<<b;
}
};

int main() {
NotOp obj;
!obj;
obj.out();

return 0;
}

Output
1
0

You might also like