
- 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
Complex numbers in C++
In this section we will see how to create and use complex numbers in C++. We can create complex number class in C++, that can hold the real and imaginary part of the complex number as member elements. There will be some member functions that are used to handle this class.
In this example we are creating one complex type class, a function to display the complex number into correct format. Two additional methods to add and subtract two complex numbers etc.
Example
#include<iostream> using namespace std; class complex { int real, img; public: complex() { //default constructor to initialize complex number to 0+0i real = 0; img = 0; } complex(int r, int i) { //parameterized constructor to initialize complex number. real = r; img = i; } void set(); void get(); void display(); friend complex add(complex, complex); friend complex sub(complex, complex); }; void complex::set() { cout << "Enter Real part: "; cin >> real; cout << "Enter Imaginary Part: "; cin >> img; } void complex::get() { cout << "The complex number is: "<< real << "+" << img << "i" << endl; } void complex::display() { if(img < 0) if(img == -1) cout << "The complex number is: "<< real << "-i" << endl; else cout << "The complex number is: "<< real << img << "i" << endl; else if(img == 1) cout << "The complex number is: "<< real << " + i"<< endl; else cout << "The complex number is: "<< real << " + " << img << "i" << endl; } complex add(complex c1, complex c2) { complex res; res.real = c1.real + c2.real;//addition for real part res.img = c1.img + c2.img;//addition for imaginary part return res;//the result after addition } complex sub(complex c1, complex c2) { complex res; res.real = c1.real - c2.real;//subtraction for real part res.img = c1.img - c2.img;//subtraction for imaginary part return res;//the result after subtraction } main() { complex n1(3, 2), n2(4, -3); complex result; result = add(n1,n2); result.display(); result = sub(n1,n2); result.display(); }
Output
The complex number is: 7-i The complex number is: -1 + 5i
- Related Articles
- Complex Numbers in Python?
- Complex Numbers in C#
- Complex Numbers in Java
- Complex numbers in C++ programming
- Complex numbers operations in Arduino
- Geometry using Complex Numbers in C++
- Python program for Complex Numbers
- How to Plot Complex Numbers in Python?
- Proj() function for Complex Numbers in C++
- How can we use complex numbers in Python?
- Program to Add Two Complex Numbers in C
- How to add two Complex numbers in Golang?
- Java Program to Add Two Complex numbers
- Haskell program to add two complex numbers
- Kotlin Program to Add Two Complex numbers

Advertisements