help with c++ complex numbers program

I need help with a program involving complex numbers, here is what i have and i am stuck what I need to add to get the program to compile and run correctly.

// Complx.h

#include <iostream>

#include <fstream>

using namespace std;

class complx

{

double real,

imag;

public:

complx( double real = 0., double imag = 0.); // constructor

~complx(); // destrcutor

friend int operator== (const complx&, const complx&);



//Sets private data members.



void Set(double new_real, double new_imaginary) {

real = new_real;

imag = new_imaginary;

}


// Complx.cpp

#include "complx.h"

complx::~complx() {

real=r; imag=i;

}

ostream &operator<< ( ostream &out_file, complx &number )

{
out_file << '(' << number.real() << ',' << number.imaginary () << ')';


return out_file;

}





extern ifstream &operator >> ( ifstream &in_file, complx &number )

{

double re, is;

char ch;

if (in_file >> ch && ch == '('&& in_file >> re >> ch && ch == ','

&& in_file >> is >> ch && ch == ')')

number.Set(re,is);

else cerr << "Finish the input"<<endl;

return in_file;

}



extern istream &operator >> ( istream &in_file, complx &number )

{

double re, is;

char ch;

if (in_file >> ch && ch == '('&& in_file >> re >> ch && ch == ','

&& in_file >> is >> ch && ch == ')')

number.Set(re,is);

else cerr << "Finish the input"<<endl;

return in_file;

}



// define constructor

complx::complx( double r, double i )

{

real = r; imag = i;

}



int operator== (const complx& a, const complx& b){


what goes here????



}


//call_complx.cpp

#include "complx.h"

int main()

{

int i=0;

complx c1, c2(3,5),in[5];

cout<< "Please input a complex number in the format of (a,b) - " << endl;

while ((cin >> in[i])&& (i<4)){

cout << in[i] << endl;

i++;

}

if (c1 == in[0] )

cout << c1 << " equals to " << in[0] << endl;

else

cout << c1 << " not equal to " << in[0]<< endl;



if (in[1] == in[2] )

cout << in[1] << " equals to " << in[2] << endl;

else

cout << in[1] << " not equal to " << in[2]<< endl;



char c; cin >> c;

return 0; //successful termination

}
closed account (9y8C5Di1)
Why do you pass variables to complx constructor that in the constructor have same identifiers as the variables within the class. The constructor has scope over a object of that class, so i believe this will cause a compilation error. And since you are only declaring the constructor within the class, you should not include identifiers, only the parameters respective datatypes.
Try this:

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Complx.h

#include <iostream>

#include <fstream>

using namespace std;

class complx

{

	double real, imag;

public:

	complx( double real = 0., double imag = 0.); // constructor
	~complx(); // destructor

	friend int operator== (const complx&, const complx&);
	
	friend ostream &operator<< ( ostream &out_file, complx &number );
	
	friend ifstream &operator >> ( ifstream &in_file, complx &number );
	
	friend istream &operator >> ( istream &in_file, complx &number );





	//Sets private data members.



	void Set(double new_real, double new_imaginary) 
	{
		real = new_real;
		imag = new_imaginary;

	}
};


	// Complx.cpp
	#include "complx.h"

	complx::~complx() 
	{

		//real=r; imag=i;

	}

	int operator== (const complx& a, const complx& b)
	{
		return ( a.imag == b.imag && a.real == b.real )	;
	}

	ostream &operator<< ( ostream &out_file, complx &number )

	{
		out_file << '(' << number.real << ',' << number.imag << ')';


		return out_file;

	}





	ifstream &operator >> ( ifstream &in_file, complx &number )

	{

		double re, is;

		char ch;

		if (in_file >> ch && ch == '('&& in_file >> re >> ch && ch == ','

			&& in_file >> is >> ch && ch == ')')

			number.Set(re,is);

		else cerr << "Finish the input"<<endl;

		return in_file;

	}



	istream &operator >> ( istream &in_file, complx &number )

	{

		double re, is;

		char ch;

		if (in_file >> ch && ch == '('&& in_file >> re >> ch && ch == ','

			&& in_file >> is >> ch && ch == ')')

			number.Set(re,is);

		else cerr << "Finish the input"<<endl;

		return in_file;

	}



	// define constructor

	complx::complx( double r, double i )

	{

		real = r; imag = i;

	}



	//call_complx.cpp
	#include "complx.h"

	int main()

	{

		int i=0;

		complx c1, c2(3,5),in[5];

		cout<< "Please input a complex number in the format of (a,b) - " << endl;

		while ((cin >> in[i])&& (i<4)){

			cout << in[i] << endl;

			i++;

		} 

		if (c1 == in[0] ) 

			cout << c1 << " equals to " << in[0] << endl;

		else

			cout << c1 << " not equal to " << in[0]<< endl;



		if (in[1] == in[2] ) 

			cout << in[1] << " equals to " << in[2] << endl;

		else

			cout << in[1] << " not equal to " << in[2]<< endl; 



		char c; cin >> c;

		return 0; //successful termination

	}
Last edited on
Topic archived. No new replies allowed.