-1

My problem is pretty minor but my understanding of classes is lmiting me. What I want to do is add two complex numbers together, for example adding the imaginary parts of two complex numbers and store that value, then add their real numbers together. The only way I think this is possible with declaring two variables and keeping it inside the add method, is by instantiating another object Imaginary. I don't understand how to do that though.

#include <iostream>
using namespace std;

class Imaginary
{
public:
    Imaginary(float = 0.0, float = 0.0);
    void add(float, float);
    void subtract(float, float);
    void multiply();
    void print();
    void cube();

private:
    float real, imag;
};
Imaginary::Imaginary(float r, float i)
{

    real = r;
    imag = i;
}

void Imaginary::add(float sumreal, float sumimag)

This sum is just a placeholder. I'm unsure How to instantiate another set of real and imag variables to add them.

{
    sum = real + imag;

I want something like sumimag = a.imag + imag;
as well as sumreal = a.real + real; Unsure how to do this within a method.

}

void Imaginary::subtract(float differencereal, float differenceimag)
{
    difference = real - imag;
}

int main()
{
    float sumreal, sumimag, differencereal, differenceimag;
    Imaginary i(1,2);
    i.add(sumreal, sumimag);
    i.subtract(differencereal, differenceimag);
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
Luke D
  • 59
  • 6
  • Do you want your `add()` to add to the `Imaginary` (which i would call `Complex`) you are calling it on (like `int a = 5; a += 2;`) ? Or do you want to get a new `Imaginary` returned (like `int a; int b = 3; a = b + 2;`)? – Swordfish Oct 21 '18 at 02:03
  • Sorry, ill rename that. All I want to do is simply load in two complex numbers, add the real parts together and save that as a variable, then add the imaginary parts together and save those to a variable. The problem is, I want to do this with two variables so I think i need another object for the second set of complex numbers. – Luke D Oct 21 '18 at 02:06
  • @Luke D . from what I remember complex numbers are the combination of real numbers and an imaginary number ex. ` 7 + 1i`. so the 7 is the real part and 1i is the imaginary part. if so, Swordfish seem to have your answer. if not, you got to elaborate abit more on what you mean exactly by needing another object. – Yucel_K Oct 21 '18 at 02:19
  • adding a complex number with two floats is bad design. Instead add two complex numbers – bolov Oct 21 '18 at 03:24

4 Answers4

1

I'm not completely sure if that is what you are looking for, but ...

#include <iostream>

class Complex
{
public:
    Complex(float real = 0.f, float imaginary = 0.f) : r{ real }, i{ imaginary } {}
    Complex add(float real, float imaginary) {
        return Complex(r + real, i + imaginary);
    }
    Complex add(Complex const &other) {
        return Complex(r + other.r, i + other.i);
    }
    Complex subtract(float real, float imaginary) {
        return Complex(r - real, i - imaginary);
    }
    Complex subtract(Complex const &other) {
        return Complex(r - other.r, i - other.i);
    }

private:
    float r, i;
};

int main()
{
    Complex a{ 1, 3 };
    Complex b = a.add(2, 7);

    Complex c{ 4, 2 };
    Complex d = b.subtract(c);
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
0

You want to override the + and - operators for the class so that you can add and subtract two Imaginary instances from one another.

The example here does exactly what you want: https://www.cprogramming.com/tutorial/operator_overloading.html

iam1me
  • 193
  • 8
0

This is one way to do it with overloading.

#include <iostream>
#include <math.h>

using namespace std;

class Complex
{
public:
    Complex(double= 0.0, double= 0.0);

    Complex operator+(const Complex &c);
    //    Complex operator-(Complex c);
    //    Complex operator/(Complex c);
    //    Complex operator*(Complex c);
    //
    void print();

    std::string to_string();
    // void cube();

private:
    double real, imaginary;
};

std::string Complex::to_string()
{
    return (std::to_string(real) + (imaginary < 0 ? " - " : " + ") + std::to_string(fabs(imaginary)) + "i");
}
Complex::Complex(double realArg, double imaginaryArg)
{
    real = realArg;
    imaginary = imaginaryArg;
}

Complex Complex::operator+(const Complex &c)
{
    Complex result;
    result.real = real + c.real;
    result.imaginary = imaginary + c.imaginary;
    return result;
}

void Complex::print()
{
    cout << "Value of Complex number: " << real << (imaginary < 0 ? " - " : " + ") << fabs(imaginary) << "i" << endl;
}

int main()
{
    Complex complex1(1, 1);
    Complex complex2(2, 2);
    Complex complexSum;

    complexSum = complex1 + complex2;

    cout << complex1.to_string() << " + " << complex2.to_string() << " = " << complexSum.to_string() << endl;

    complexSum.print();
    return 0;
}

Output:

1.000000 + 1.000000i + 2.000000 + 2.000000i = 3.000000 + 3.000000i
Value of Complex number: 3 + 3i
Gardener
  • 2,591
  • 1
  • 13
  • 22
  • 1
    *you will need to overload...* he doesn't need to specifically overload + and - operators. it's nice but not necessary. – Yucel_K Oct 21 '18 at 02:30
  • 1
    Suggestion: pass `c` into `operator+` as a `const` reference. Handy reading for more suggestions and tips: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Oct 21 '18 at 02:37
  • @user4581301 I have updated the answer with the const reference. Thank you. – Gardener Oct 21 '18 at 02:44
  • @Yucel_K: Yes, I think you are right. This is a case of my failing to truly understand the customer requirements and coding before asking more questions. It still does not make sense to talk about "sumimag = a.imag + imag;" That suggests an overload, but then it is referencing the imaginary portion as a member and then as a standalone. Again, my failure to get a clear set of requirements. – Gardener Oct 21 '18 at 02:46
  • I'm sorry for not adding specifics. I dont want to overload this set of methods. Thanks for the solution Ill definitely use it for inspiration. – Luke D Oct 22 '18 at 04:24
0

My recommendation is to not, and instead use the existing class for this in the standard library, std::complex<> found in the header <complex> and available in modern compilers and libraries.

Williham Totland
  • 28,471
  • 6
  • 52
  • 68