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

Operator Overloading in C++ Programming

Operator overloading in C++ allows programmers to redefine the behavior of operators when they are used on user-defined types like objects and classes. This allows operators like + and - to be used to concatenate strings or subtract complex numbers. The document provides examples of overloading unary increment (++), postfix and prefix decrement (--), and binary subtraction (-) operators for classes representing a counter, integer, and complex number respectively. Overloaded operators allow built-in operators to work with custom types in intuitive and readable ways.

Uploaded by

Fateha Hasan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Operator Overloading in C++ Programming

Operator overloading in C++ allows programmers to redefine the behavior of operators when they are used on user-defined types like objects and classes. This allows operators like + and - to be used to concatenate strings or subtract complex numbers. The document provides examples of overloading unary increment (++), postfix and prefix decrement (--), and binary subtraction (-) operators for classes representing a counter, integer, and complex number respectively. Overloaded operators allow built-in operators to work with custom types in intuitive and readable ways.

Uploaded by

Fateha Hasan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Operator overloading in C++ Programming

The meaning of an operator is always same for variable of basic types like: int, float, double etc.
For example: To add two integers, + operator is used.

However, for user-defined types (like: objects), you can redefine the way operator works. For
example:

If there are two objects of a class that contains string as its data members. You can redefine the
meaning of + operator and use it to concatenate those strings.

This feature in C++ programming that allows programmer to redefine the meaning of an operator
(when they operate on class objects) is known as operator overloading.

1. Unary ++ overloading

#include <iostream>
using namespace std;

class Test
{
private:
int count;

public:
Test(): count(5){}

void operator ++()


{
count = count+1;
}
void Display() { cout<<"Count: "<<count; }
};

int main()
{
Test t;
// this calls "function void operator ++()" function
++t;
t.Display();
return 0;
}
2. Operator Overloading of Decrement -- Operator

#include <iostream>
using namespace std;

class Check
{
private:
int i;
public:
Check(): i(3) { }
Check operator -- ()
{
Check temp;
temp.i = --i;
return temp;
}

// Notice int inside barcket which indicates postfix decrement.


Check operator -- (int)
{
Check temp;
temp.i = i--;
return temp;
}

void Display()
{ cout << "i = "<< i <<endl; }
};

int main()
{
Check obj, obj1;
obj.Display();
obj1.Display();

// Operator function is called, only then value of obj is assigned to obj1


obj1 = --obj;
obj.Display();
obj1.Display();

// Assigns value of obj to obj1, only then operator function is called.


obj1 = obj--;
obj.Display();
obj1.Display();
return 0;
}

3. Binary Operator Overloading to Subtract Complex Number

#include <iostream>
using namespace std;

class Complex
{
private:
float real;
float imag;
public:
Complex(): real(0), imag(0){ }
void input()
{
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}

// Operator overloading
Complex operator - (Complex c2)
{
Complex temp;
temp.real = real - c2.real;
temp.imag = imag - c2.imag;

return temp;
}

void output()
{
if(imag < 0)
cout << "Output Complex number: "<< real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag << "i";
}
};

int main()
{
Complex c1, c2, result;

cout<<"Enter first complex number:\n";


c1.input();

cout<<"Enter second complex number:\n";


c2.input();

// In case of operator overloading of binary operators in C++ programming,


// the object on right hand side of operator is always assumed as argument by compiler.
result = c1 - c2;
result.output();

return 0;
}

You might also like