
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Subtract Complex Number using Operator Overloading
Operator overloading can be done with most of the built-in operators in C++. The overloaded operators are functions with the keyword operator followed by the operator symbol that is defined. The overloaded operators have a return type and a parameter list like any function.
A program that subtracts complex numbers using operator overloading is as follows −
Example
#include<iostream> using namespace std; class ComplexNum { private: int real, imag; public: ComplexNum(int r = 0, int i =0) { real = r; imag = i; } ComplexNum operator - (ComplexNum const &obj1) { ComplexNum obj2; obj2.real = real - obj1.real; obj2.imag = imag - obj1.imag; return obj2; } void print() { if(imag>=0) cout << real << " + i" << imag <<endl; else cout << real << " + i(" << imag <<")"<<endl; } }; int main() { ComplexNum comp1(15, -2), comp2(5, 10); cout<<"The two comple numbers are:"<<endl; comp1.print(); comp2.print(); cout<<"The result of the subtraction is: "; ComplexNum comp3 = comp1 - comp2; comp3.print(); }
Output
The two comple numbers are: 15 + i(-2) 5 + i10 The result of the subtraction is: 10 + i(-12)
In the above program, the class ComplexNum is defined which has variables real and imag for the real and imaginary part of a complex number respectively. The constructor ComplexNum is used to initialize the values of real and imag. It also contains the default values as 0. This is shown in the following code snippet −
class ComplexNum { private: int real, imag; public: ComplexNum(int r = 0, int i =0) { real = r; imag = i; } }
The function that is the overloaded operator contains the keyword operator followed by - as that is the operator being overloaded. The function subtracts the two complex numbers and the result is stored in the object obj2. Then this value is returned to the ComplexNum object comp3.
The following code snippet demonstrates this −
ComplexNum operator - (ComplexNum const &obj1) { ComplexNum obj2; obj2.real = real - obj1.real; obj2.imag = imag - obj1.imag; return obj2; }
The print() function prints the real and imaginary part of the complex number. This is shown as follows.
void print() { if(imag>=0) cout << real << " + i" << imag <<endl; else cout << real << " + i(" << imag <<")"<<endl; }
- Related Articles
- Program to add and Subtract Complex Numbers using Class in Java
- Overloading unary operator in C++?
- How to use Operator Overloading in C#?
- How to implement operator overloading in C#?
- C++ program to overload addition operator to add two complex numbers
- Rules for operator overloading in C++
- Overloading array index operator [] in C++
- C++ Program to Perform Complex Number Multiplication
- Increment ++ and Decrement -- Operator Overloading in C++
- Program to find the Largest Number using Ternary Operator in C++
- C++ Program to convert a number into a complex number
- What is overloading a unary operator in C++?
- C++ Program to initialize and print a complex number
- Operator overloading in C++ to print contents of vector, map, pair ..
- Write a program to add two complex numbers using C
