Reading in imaginary numbers

Hi,

I'm trying to figure out how to do the first part of this problem.

Here is the instructions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Given the following class; write the implementation code of its member functions.

class Complex {
	friend ostream& operator<<(ostream& out, const Complex& theComplex);
	friend istream& operator>>(istream& in, Complex& theComplex);
	friend Complex operator+(const Complex& lhs, const Complex& rhs);
	friend Complex operator-(const Complex& lhs, const Complex& rhs);
	friend Complex operator*(const Complex& lhs, const Complex& rhs);
	friend Complex operator/(const Complex& lhs, const Complex& rhs);
public:
	Complex(double re = 0.0, double im = 0.0);
	double getReal(void) const;            // return real part
	double getImaginary(void) const;        // return imaginary part
	void setReal(double re);            // sets the real part
	void setImaginary(double im);        // sets the imaginary part
	void convertStringToComplex(const string& complexString);
private:
	double real;
	double imag;
};

Complex numbers are added by adding the real and imaginary parts of the summands.That is to say, assuming a + bi is the first 
complex number and c + di is the second complex number : (a + bi) + (c + di) = (a + c) + (b + d)i

Similarly, the subtraction is defined by : (a + bi) - (c + di) = (a - c) + (b - d)i

The multiplication of the two complex numbers is defined by the following formula : (a + bi)(c + di) = (ac - bd) + (b - d)i

The division of the two complex numbers is defined in terms of complex multiplication, which is described above, and real
division.Where at least one of c and d is non - zero : image : division of two complex numbers



Finally, two complex numbers are equal if (a == c) and (b == d).

Create a program to test the class.
For example :
Enter first complex number : 4.2 + 3.0i
Enter second complex number : 2.0 + 2.1i
No spaces between the real and imaginary number in each string. Make sure you read the entry at once as a string.
The output should test all the operators; for example, the addition operator will give the result :
(4.2 + 3.0i) + (2.0 + 2.1i) = 6.2 + 5.1i
*/


Any suggestions how to read in the complex numbers?


Last edited on
for c++ complex type, its simple enough:
complex<double> p;
cin >> p.real() >> p.imag();


which is a clue as to how to do yours.
you want to enable setting of real and imag parts of your class so you can do it.
your function .real() would return a reference to the variable, allowing it to be modified through the ref.
Last edited on
Topic archived. No new replies allowed.