Search code examples
c++complex-numbers

Add complex numbers code


class complex1   
{   
    public:
        int real,img;
        complex1(){}
        complex1(int a)
        {
            real=a;
            img=a;
        }
        complex1(int a,int b)
        {
            real=a;
            img=b;
        }

        complex1 sum(int x,complex1 y);
        complex1 sum(complex1,complex1);
        complex1 display(complex1); 
};

complex1 complex1::sum(int x,complex1 y)
{
    complex1 num;
    num.real=x+y.real;
    num.img=y.img;
    return num;
}

complex1 complex1::sum(complex1 a,complex1 b)
{
    complex1 num;
    num.real=a.real+b.real;
    num.img=a.img+b.img;
    return num;

}

complex1 complex1::display(complex1 c)
{
    cout<<"The complex number is:\n";
    cout<<c.real<<"+i"<<c.img;
}

main()
{
    complex1 p,q,r,s;
    p=complex1(2,4);
    q=complex1 (3,5);


    cout<<"\n";
    cout<<"p="<<display(p);
    cout<<"q="<<display(q);
}

Using constructors we need to add two complex numbers. I get the error

Error: display was not declared in this scope.

Any suggestions? where am I wrong?


Solution

  • You are getting this error because you are calling display() outside of an instance method implementation, without invoking it on an instance of complex1, so the compiler doesn't know where to find it.

    You would need to call it something like p.display(p).

    However, you have several more serious errors here:

    1. The sum() and display() methods do not do anything with this, and so they are acting like static methods but still require an instance. This is confusing. Make them static. (The sum() method should probably be implemented as a + operator overload, anyway.)

    2. The display() method should probably be implemented as a << operator overload, taking an std::ofstream& as the left operand, and it should return std::ofstream& instead of a complex1 object. (And in your implementation it doesn't even return a complex1 object at all, even though it is declared to. This is an error.)

    For example, to fix the first point I would remove the sum() methods altogether and go with a free operator, something like this:

    complex1 operator+(complex1 const & a, complex1 const & b)
    {
        return complex1(a.real + b.real, a.img + b.img);
    }
    

    Since your complex1(int) constructor is implicitly implicit (hah!) this operator can work with complex1 + complex1, complex1 + int, and int + complex1 automatically. (But note that your complex1(int) constructor assigns the argument to both real and img, which is likely a bug; I would expect you would want to initialize img to zero instead.)

    To fix the second issue, I would remove your display() method entirely and go with a free operator, something like this:

    std::ofstream & operator<<(std::ofstream & s, complex1 const & c)
    {
        return s << c.real << "+i" << c.img;
    }
    

    Then, to output a number you could do something like:

    std::cout << "p=" << p;