c-plus-plus
  1. c-plus-plus-add-two-complex-numbers-using-class

Add Two Complex Numbers using Class in C++

In C++, we can add two complex numbers using class. Complex numbers have a real part and an imaginary part, and we can create a class to represent them.

Syntax

class Complex {
    private:
        float real;
        float imag;
    public:
        Complex(float r = 0.0, float i = 0.0) {
            real = r;
            imag = i;
        }
        Complex operator+(Complex const &obj) {
            Complex res;
            res.real = real + obj.real;
            res.imag = imag + obj.imag;
            return res;
        }
        void display() {
            cout << real << " + i" << imag << endl;
        }
};

Example

#include <iostream>
using namespace std;

class Complex {
    private:
        float real;
        float imag;
    public:
        Complex(float r = 0.0, float i = 0.0) {
            real = r;
            imag = i;
        }
        Complex operator+(Complex const &obj) {
            Complex res;
            res.real = real + obj.real;
            res.imag = imag + obj.imag;
            return res;
        }
        void display() {
            cout << real << " + i" << imag << endl;
        }
};

int main() {
    Complex c1(3.2, 5.0);
    Complex c2(5.0, 6.7);
    Complex res = c1 + c2;
    res.display();
    
    return 0;
}

Output

8.2 + i11.7

Explanation

In the above example, we have created a class called "Complex" to represent complex numbers. The class has two private data members called "real" and "imag" to represent the real and imaginary parts of the complex number. The class constructor takes two arguments, which are used to initialize the real and imaginary parts.

We have also overloaded the "+" operator to add two complex numbers. The overloaded operator takes a reference to a constant object of the "Complex" class and returns a "Complex" object. It adds the real and imaginary parts of the two complex numbers and returns the result as a new "Complex" object.

In the main function, we create two "Complex" objects called "c1" and "c2" with their respective real and imaginary parts. We then add them together using the "+" operator and store the result in a new "Complex" object called "res". Finally, we display the result using the "display" function defined in the "Complex" class.

Use

We can use the "Complex" class to perform arithmetic operations on complex numbers. We can define functions to overload various arithmetic operators such as "+", "-", "*", "/", etc.

Important Points

  • A complex number has a real part and an imaginary part.
  • We can create a class to represent complex numbers.
  • We can overload operators to perform arithmetic operations on complex numbers.
  • Operators can be overloaded using member functions or friend functions.
  • Complex numbers can be added, subtracted, multiplied and divided using the appropriate operators.

Summary

In summary, we can add two complex numbers using class in C++. We create a class to represent complex numbers with private data members for the real and imaginary parts. We can overload operators to perform arithmetic operations on complex numbers. Finally, we can use the class to add, subtract, multiply and divide complex numbers.

Published on: