简体   繁体   中英

How to use Complex numbers in Class/Object C++

Im strugling to implement complex numbers into my code. When I compile it gives me random numbers. It should use imaginary number i = sqrt(-1). It should output realPart + imaginaryPart * i.

#include <iostream>
#include <complex>
using namespace std;

class Complex
{
private:
    double i;
    complex<double> imaginarypart = i*sqrt(1);
    double realpart;
public:
    void seti(double a1) {
        i = a1;
    }
    void setrealpart(double a2) {
        realpart = a2;
    }
    void printwhole() {
        cout << realpart + imaginarypart;
    }
};

int main()
{
    double a, b;
    cout << "Enter your realpart" << endl;
    cin >> a;
    cout << "Enter your imaginarypart " << endl;
    cin >> b;

    Complex num1;
    num1.seti(b);
    num1.setrealpart(a);
    cout << endl;
    num1.printwhole();
}

The point is that an the type double cannot store imaginary values.
Yet you try to do so, eg with sqrt(1); (though you probably meant -1).
The complex<double> is indeed able to store imaginary values, but on the one hand it won't work by assigning the product of the non-initialised i with 1 and on the other hand you seem to try to implement complex numbers yourself. Using the provided complex standard type somewhat defeats that. If you use it, then you just need to learn outputting it in the format you want, instead of doing all the implementation work.

Lets assume that you are more interested in getting it done yourself first.
What you need to do is to always represent complex numbers as two numbers.
I hence propose to change your code likes this.

#include <iostream>
using namespace std;

class Complex
{
private:
    /* double i; Won't work, it cannot store imaginary values. */
    double imaginarypart;
    double realpart;
public:
    void setimaginarypart(double a1) {
        imaginarypart = a1;
    }
    void setrealpart(double a2) {
        realpart = a2;
    }
    void printwhole() {
        cout << realpart << '+' <<  imaginarypart << 'i'; // To get the output you want.
    }
};

int main()
{
    double a, b;
    cout << "Enter your realpart" << endl;
    cin >> a;
    cout << "Enter your imaginarypart " << endl;
    cin >> b;

    Complex num1;
    num1.setimaginarypart(b);
    num1.setrealpart(a);
    cout << endl;
    num1.printwhole();
}

Alternatively use existing solutions (eg std::complex), instead of trying to program one yourself. Outputting them in the shape you want (r+i*I) does not require to implement the whole thing yourself.
Have a look at https://en.cppreference.com/w/cpp/numeric/complex to learn about what it offers.

As you figured out, complex numbers have a real and an imaginary part. And in std::complex<double> , those two parts are combined.

But in your class Complex , you have Complex::i , Complex::realpart , Complex::imaginarypart::real() and Complex::imaginarypart::imag() . That's 4 parts!

Furthermore, complex<double> imaginarypart = i*sqrt(1); means that Complex::imaginarypart is set to i*sqrt(1) when a Complex object is created. But at that time, i is still uninitialized! So that won't work either. This is why you get random numbers (or a crash, depending on your luck).

The simple solution is to drop your whole class Complex . std::complex<double> already has functions to set the real and imaginary parts, namely std::complex<double>::real(double) and std::complex<double>::imag(double)

(Note: real and imag are setters if you pass a value, getters if you don't).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM