Table of Contents
In this article, you’ll learn about how to make C++ program to increment and decrement complex numbers using unary
operator overloading and binary operator overloading.
What is Operator Overloading?
It is a compile-time polymorphism using which a special meaning can be provided to an operator for a given user-defined data type.
C++ program to increment and decrement complex numbers using unary operator overloading
#include <iostream>
class Complex {
private:
double real;
double imaginary;
public:
Complex(double r = 0, double i = 0) {
real = r;
imaginary = i;
}
// Overloading unary ++ operator
Complex operator++() {
++real;
++imaginary;
return *this;
}
// Overloading unary -- operator
Complex operator--() {
--real;
--imaginary;
return *this;
}
void display() {
std::cout << "Real part: " << real << " Imaginary part: " << imaginary << std::endl;
}
};
int main() {
Complex c1(2.5, 3.5);
Complex c2(1.2, 1.8);
++c1; // Increment c1
--c2; // Decrement c2
c1.display();
c2.display();
return 0;
}
The output of this program is
Real part: 3.5 Imaginary part: 4.5
Real part: 0.2 Imaginary part: 0.8
In this program, we define a class called “Complex” that represents complex numbers. The class has a constructor that takes two arguments, the real and imaginary parts of the complex number.
We then overload the unary ++
and --
operators using the following syntax:
Complex operator++() {
++real;
++imaginary;
return *this;
}
Complex operator--() {
--real;
--imaginary;
return *this;
}
In these functions, we simply increment or decrement the real and imaginary parts of the complex number, and then return a reference to the modified object.
In the main
function, we create two complex numbers c1
and c2
, and then increment c1
and decrement c2
using the overloaded ++
and --
operators respectively. Finally, we display the real and imaginary parts of both complex numbers using the display()
function.
That’s an example of how to increment and decrement complex numbers using unary operator overloading in C++. The unary ++
and --
operators are used to increment and decrement a single operand, and can be overloaded to work with user-defined classes like Complex
.
C++ program to increment and decrement complex numbers using binary operator overloading
#include <iostream>
class Complex {
private:
double real;
double imaginary;
public:
Complex(double r = 0, double i = 0) {
real = r;
imaginary = i;
}
// Overloading binary + operator
Complex operator+(Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imaginary = imaginary + obj.imaginary;
return res;
}
// Overloading binary - operator
Complex operator-(Complex const &obj) {
Complex res;
res.real = real - obj.real;
res.imaginary = imaginary - obj.imaginary;
return res;
}
void display() {
std::cout << "Real part: " << real << " Imaginary part: " << imaginary << std::endl;
}
};
int main() {
Complex c1(2.5, 3.5);
Complex c2(1.2, 1.8);
Complex c3 = c1 + c2; // Add c1 and c2
Complex c4 = c1 - c2; // Subtract c2 from c1
c3.display();
c4.display();
return 0;
}
The output of this program is
Real part: 3.7 Imaginary part: 5.3
Real part: 1.3 Imaginary part: 1.7
In this program, we define a class called “Complex” that represents complex numbers. The class has a constructor that takes two arguments, the real and imaginary parts of the complex number.
We then overload the binary +
and -
operators using the following syntax:
Complex operator+(Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imaginary = imaginary + obj.imaginary;
return res;
}
Complex operator-(Complex const &obj) {
Complex res;
res.real = real - obj.real;
res.imaginary = imaginary - obj.imaginary;
return res;
}
In these functions, we create a new Complex
object res
and set its real and imaginary parts to the sum or difference of the corresponding parts of the two complex numbers. We then return the res
object.
In the main
function, we create two complex numbers c1
and c2
, and then add them and subtract them using the overloaded +
and -
operators respectively. Finally, we display the real and imaginary parts of both resulting complex numbers using the display()
function.
That’s an example of how to increment and decrement complex numbers using binary operator overloading in C++. The binary +
and -
operators are used to add and subtract two operands, and can be overloaded to work with user-defined classes like Complex
.