Write and execute a C++ program to implement the complex number class
Share Now...


Problem Statement

Write and execute a C++ program to implement the complex number class.


Here’s an algorithm for a C++ class to implement complex numbers:

  1. Start by defining the class structure with its data members to store the real and imaginary parts of the complex number.
  2. Define a constructor to initialize the real and imaginary parts of the complex number. You can use default arguments to initialize the real and imaginary parts to zero, in case the user does not provide any values.
  3. Overload the ‘+’ and ‘-‘ operators to perform addition and subtraction of two complex numbers.
  4. Overload the ‘*’ operator to perform multiplication of two complex numbers.
  5. Overload the ‘/’ operator to perform division of two complex numbers.
  6. Implement a method to calculate the magnitude of the complex number.
  7. Implement a method to calculate the phase of the complex number, if necessary.
  8. Implement a method to display the complex number in the format “a + bi”, where “a” is the real part and “bi” is the imaginary part.
  9. Test the implementation by creating objects of the class and performing operations on them, such as addition, subtraction, multiplication, and division.

Code Implementation Method-1


using namespace std;


class Complex {



                        int real, imaginary;


            Complex(int tempReal = 0, int tempImaginary = 0)


                        real = tempReal;

                        imaginary = tempImaginary;



            Complex addComp(Complex C1, Complex C2)



                        Complex temp;


                        temp.real = C1.real + C2.real;


                        temp.imaginary = C1.imaginary + C2.imaginary;


                        return temp;




// Main Class

int main()



            Complex C1(3, 2);


            cout<<"Complex number 1 : "<< C1.real

                                                << " + i"<< C1.imaginary<<endl;


            Complex C2(9, 5);


            cout<<"Complex number 2 : "<< C2.real

                                                << " + i"<< C2.imaginary<<endl;


            Complex C3;


            C3 = C3.addComp(C1, C2);


            cout<<"Sum of complex number : "

                                                            << C3.real << " + i"

                                                            << C3.imaginary;


Code Implementation Method-2

#include <iostream>
#include <cmath>

class Complex {
        double real, imag;

        Complex(double real = 0, double imag = 0) : real(real), imag(imag) {}

        Complex operator+(const Complex &other) const {
            return Complex(real + other.real, imag + other.imag);

        Complex operator-(const Complex &other) const {
            return Complex(real - other.real, imag - other.imag);

        Complex operator*(const Complex &other) const {
            return Complex(real * other.real - imag * other.imag, real * other.imag + imag * other.real);

        Complex operator/(const Complex &other) const {
            double denominator = other.real * other.real + other.imag * other.imag;
            return Complex((real * other.real + imag * other.imag) / denominator, (imag * other.real - real * other.imag) / denominator);

        double magnitude() const {
            return sqrt(real * real + imag * imag);

        double phase() const {
            return atan2(imag, real);

        void display() const {
            std::cout << real << " + " << imag << "i" << std::endl;

int main() {
    Complex a(1, 2), b(3, 4), c;

    c = a + b;
    std::cout << "Addition: ";

    c = a - b;
    std::cout << "Subtraction: ";

    c = a * b;
    std::cout << "Multiplication: ";

    c = a / b;
    std::cout << "Division: ";

    return 0;

Explore More Projects on Matlab

Join us for Regular Updates

TelegramJoin Now
WhatsAppJoin Now
FacebookJoin Now
InstagramJoin Now
LinkedInJoin Now
Join our Telegramconnectkreations

About Connect Kreations

We the team Connect Kreations have started a Blog page which is eminently beneficial to all the students those who are seeking jobs and are eager to develop themselves in a related area. As the world is quick on uptake, our website also focuses on latest trends in recent technologies and project learning and solutions. We are continuously putting our efforts to provide you with accurate, best quality, and genuine information. Here we also have complete set of details on how to prepare aptitude, interview and more of such placement/ off campus placement preparation.

Connect Kreations is excited to announce the expansion of our services into the realm of content creation! We are now offering a wide range of creative writing services, including poetry, articles, and stories.

Whether you need a heartfelt poem for a special occasion, a thought-provoking article for your blog or website, or an engaging story to captivate your audience, our team of talented writers is here to help. We have a passion for language and a commitment to creating high-quality content that is both original and engaging.

Our services are perfect for individuals, businesses, and organizations looking to add a touch of creativity and personality to their content. We are confident that our unique perspectives and diverse backgrounds will bring a fresh and exciting voice to your project.

Thank you for choosing Connect Kreations for your content creation needs. We look forward to working with you and helping you to bring your vision to life!

The website is open to all and we want all of you to make the best use of this opportunity and get benefit from it..🤓

Share Now...
Connect Kreations
Connect Kreations
Articles: 53