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);
}