Home > Net >  do I call the functions in class correct o r did I define them right?
do I call the functions in class correct o r did I define them right?

Time:12-18

I'm writing this code for one of my assignments and don't worry, I'm not here to ask the solution :) I wrote this code (class complex part) and the main part was already on the code and I had to define the class and its functions, constructor etc. my question is about my functions like add, sub, constructComple etc. and compiler says they're not defined. I'm new to this part of cpp and been learning it for 3 months. my question is: I have defined these functions; so why isn't it working and compiler says it's not defined?

#include<iostream>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
class Complex
{

    public:
         double x1,y1,x2,y2, real, img, y , x;

        Complex(double x, double y)
        {
          real = x;
          img  = y;
        }

        void constructComplex(double x, double y)
        {
            real = x;
            img = y;
        }

        void conjugate(Complex c)
        {
            c.img = -c.img;
        }

        void add(Complex c,Complex c2)
        {
             real = c.real   c2.real;
             img = c.img   c2.img;
        }
        void sub(Complex c ,Complex c2)
        {
            img = c.img - c2.img;
            real = c.real - c2.real;
        }
        void mul(Complex c , Complex c2)
        {
            real = c.real*c2.real - c.img*c2.img;
            img = c.real*c2.img   c2.real*c.img;
        }
        void printPolarForm(Complex c)
        {
            double radian = atan2(y,x);
            double angle = (radian*180)/3.14;
            cout<<sqrt(c.real*c.real   c.img*c.img)<<"e"<<"^"<<"("<<"i"<<angle<<")";
        }


};
        void constructComplex(double x, double y)
        {
            double real = x;
            double img = y;
        }

//this part of code was already in the assignment and I just defined the class.

int main() {
    cout << fixed << setprecision(2);
    double x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    Complex c = constructComplex(x1, y1);
    Complex c2 = constructComplex(x2, y2);
    Complex c3 = conjugate(c);
    cout << c3.real << ' ' << c3.img << '\n';
    Complex c4 = add(c, c2);
    cout << c4.real << ' ' << c4.img << '\n';
    Complex c5 = sub(c, c2);
    cout << c5.real << ' ' << c5.img << '\n';
    Complex c6 = mul(c, c2);
    cout << c6.real << ' ' << c6.img << '\n';
    printPolarForm(c);
    return 0;
}
this code has four double entries and four results that include: 
1. conjugate form of the complex number 
2. sum of the given complex numbers
3. subtraction of the given complex numbers
4. polar form of the first complex number.

CodePudding user response:

If you put the function inside the class, then it's an instance function. You call it with a different syntax, and it takes an implicit this parameter. So if you want a standalone function, don't put it in the class. Just do

// At the top-level of your file:
Complex add(Complex c, Complex c2) {
  double real = c.real   c2.real;
  double img = c.img   c2.img;
  return Complex(real, img);
}

and call it like you were: Complex c4 = add(c, c2);

If you want it inside the class, then the left-hand argument is implicit.

// Inside class Complex:
Complex add(Complex c2) {
  double real = this->real   c2.real;
  double img = this->img   c2.img;
  return Complex(real, img);
}

and you call it as: Complex c4 = c.add(c2);

  • Related