Search code examples
c++classobjectconstructor

conflicting declaration 'printPolarForm c'


I wanted to declare a constructor but I face this error. As it's obvious , I wanted to create a program to show the polar form of a complex number. I know it's kind of ridiculous and it's easier to do this , with functions . BUT this is a project and it is mentioned not to change the body of main function.

How can this be fixed?

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

class Complex
{
public:
    double real , img;


    Complex()
    {
        real = 0;
        img = 0;
    }
};


class constructComplex : public Complex
{
public:
    constructComplex(double a , double b)
    {
        real = a;
        img = b;
    }
};

class printPolarForm : public Complex
{
public:
    printPolarForm(Complex z)
    {
        cout << fixed << setprecision(2);
        double r = sqrt( z.real * z.real + z.img * z.img );
        double argument = atan2(z.img , z.real)*180/3.14;
        cout << r << "e^(i" << argument << ')';
    }
};




int main()
{
    cout << fixed << setprecision(2);
    double x1, y1;
    cin >> x1 >> y1;
    Complex c = constructComplex(x1, y1);
    printPolarForm(c);
    return 0;
}

Solution

  • You have a serious misunderstanding of what classes are for. Generally speaking classes encapsulate some state, i.e. member variables, and some operations on that state. None of your last two classes above have any state beyond the Complex class that they inherit from, so they should not exist as classes.

    Let's see how this code should be written

    constructComplex is an attempt to construct a complex number from parameters, so it should be an alternate constructor within the Complex class. i.e.

    class Complex
    {
    public:
        double real , img;
    
        Complex()
        {
            real = 0;
            img = 0;
        }
    
        Complex(double a, double b)
        {
            real = a;
            img = b;
        }
    };
    

    With this new constructor your main code changes from

    Complex c = constructComplex(x1, y1);
    

    to

    Complex c(x1, y1);
    

    printPolarForm is an operation on an existing complex number. It could be defined as a member function or a free function. I'll show that as a free function first

    void printPolarForm(Complex z)
    {
        cout << fixed << setprecision(2);
        double r = sqrt( z.real * z.real + z.img * z.img );
        double argument = atan2(z.img , z.real)*180/3.14;
        cout << r << "e^(i" << argument << ')';
    }
    

    the alternative is to make is a member function of the Complex class

    class Complex
    {
        ...
    
        void printPolarForm() const
        {
            cout << fixed << setprecision(2);
            double r = sqrt( real * real + img * img );
            double argument = atan2(img , real)*180/3.14;
            cout << r << "e^(i" << argument << ')';
        }
    };
    

    The rest of the Complex class is as before. With the member function method you have to change how the function is called in main. Instead of

    printPolarForm(c);
    

    you would write

    c.printPolarForm();
    

    Either of these two options (free function or member function) are perfectly acceptable in this case. In both cases the class printPolarForm has been removed.