Implementation of complx class?

Basically I need to implement a class, complx, to manipulate complex numbers: a+bi.


Here's what I have for the header file:

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
// Complx.h
#include <iostream>
#include <fstream>
using namespace std;

class complx
{
double real,
imag;
public:
complx( double real = 0., double imag = 0.); // constructor
complx operator+(complx); // operator+()

complx operator+(double); // operator+()with double
complx operator- (complx); // operator-()
complx operator* (complx); // operator*()

bool operator== (complx); // operator==()


//Sets private data members.
void Set(double new_real, double new_imaginary) {
real = new_real;
imag = new_imaginary;
}

//Returns the real part of the complex number.
double Real() {
return real;
}

//Returns the imaginary part of the complex number.
double Imaginary() {
return imag;
}
};

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

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

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

complx &operator + (double, complx);
complx &operator - (double, complx);
complx &operator * (double, complx);






And then here's what I have for the Complx.cpp file:


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
// Complx.cpp
#include "complx.h"
// define I/O stream

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

ostream &operator<< ( ostream &out_file, complx number )
{
out_file << '(' << number.Real() << ',' << number.Imaginary() << ')';
return out_file;
}

// define constructor
complx::complx( double r, double i )
{
real = r; imag = i;
}

// define overloaded + (plus) operator
complx complx:perator+ (complx c)
{
complx result;
result.real = (this->real + c.real);
result.imag = (this->imag + c.imag);
return result;
}






Lastly this is what I have to call the class:

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
//call_complx.cpp
#include "complx.h"
ifstream infile ("in.dat");
int main()
{
int i=0;
complx in[7];
double d = 4.5; 
cout<< "The input numbers are: " << endl;

while (infile >> in[i]){
cout << in[i] << endl;
i++;
} 
complx s1 = in[0] + in[1]; // calls complx:perator+()
complx s2 = d + in[2]; // overload operator+()
complx s3 = in[3] + d; // overload operator+()
complx a = in[4] - in[5];
complx mm=in[3]*in[4];
complx dm=d*in[4] ;
complx b=d-in[0] ;

cout << "The sum is a complex number " << s1 <<endl;
cout << "The sum is a complex number " << s2 <<endl;
cout << "The sum is a complex number " << s3 <<endl;
cout << "The subtract is a complex number " << a <<endl;
cout << "The product is a complex number " << mm <<endl; 
cout << "The subtract is a complex number " << b <<endl;
cout << "The product is a complex number " << dm <<endl;
if (in[4] == in[5]) cout << "in[4] and in[5] are the same " << endl;
system("pause");
return 0; //successful termination
}




Can anyone please help me complete this?
Last edited on
I need help on this assignment as well!
[code] "Please use code tags" [/code]
¿what do you need help with?
1
2
3
4
5
6
7
8
9
complx operator+(double);       // operator+()with double

      complx operator- (complx);       // operator-()

      complx operator* (complx);       // operator*()

 

      bool operator== (complx);   // operator==() 


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

1
2
3
4
5
 complx &operator + (double, complx);

complx &operator - (double, complx);

complx &operator * (double, complx); 
inb4 syntax errors
Don't forget to make your class's visible stuff public. Also, some of the operators you are asking about should be const.

You should know what the operators look like. If you cannot remember, or cannot decipher your notes, take a look at Wikipedia.
http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Arithmetic_operators

So, to write a + operator, you should have:

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
class complx
{
  // private stuff goes here
  ...

public:
  // public stuff goes here, including:

  complx operator+(double) const;

  ...

};

// Here is where I implement the addition with a scalar
complx complx::operator+(double d) const
{
  // this will be what I return: a new complx object
  complx result( real, imag );

  // here is where I add 'd' to 'result' (you should know how to do that)
  ...

  // return the new complx object
  return result;
}

All the other operators will be similar.

You should be implementing:

    +    -    *    +=    -=    *=    and    ==

Keep in mind that the first three do not modify the object; they create and return a new object. The penultimate three do modify the object; they return a reference to *this.


You should only have one extraction operator:

istream& operator>>( istream& ins, complx& c )

You should not overload that with ifstream unless it has some special need to do that (and it shouldn't). [Even so, follow your professor's instructions.]


Hope this helps.
Last edited on
Can you elaborate on how to fix this part of the code, I'm not sure if I follow your first explanation.

1
2
3
4
5
6
7
8
9
  complx operator+(double);       // operator+()with double

      complx operator- (complx);       // operator-()

      complx operator* (complx);       // operator*()

 

      bool operator== (complx);   // operator==()   
See in my example above, on line 9, how I wrote what you have on the first line just above?

Next, somewhere after your class's block, you need to write the function that was prototyped on line 9. See my example lines 15 through 26.

That is what you are supposed to be doing. Make sure to fill in the missing parts, and don't forget to click on some links above, and google around "C++ operator overloading example" for more.

Good luck!
God I'm so lost. I have no idea what I'm doing or what Duoas is talking about.
This is the part where you've got to wrap your brain around something. Don't give up. (It gets easier the more you do it.)

I'll be back later to help more.
1
2
3
4
5
complx &operator + (double, complx);

complx &operator - (double, complx);

complx &operator * (double, complx);




How would you implement these into the complx.cpp file?
Each time you post you list something different.

There are three general sets of arithmetic operators you need to implement:

1) member functions that take as argument the same type as the class
2) member functions that take as argument something different than the class
3) non-member functions that take as arguments something different and the class

Here are examples. Assume a, b, and c are complxex, and d is a double.
1) c = a + b; c += b;
2) c = a + d; c += d;
3) c = d + b;

The member functions should have the following signatures:

1)
complx complx::operator + ( const complx& a ) const  
complx& complx::operator += ( const complx& a )

2)
complx complx::operator + ( double d ) const  
complx& complx::operator += ( double d )

3)
complx operator + ( double d, const complx& b )

The ones you just listed are from group 3. They should be friends of the class. (But not members!)

Remember, each function should be prototyped inside the class definition, and then the function should be again listed, complete with it's body, outside the class definition.

For example:

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
class complx
{
  ...
public:
  ...

  complx  operator +  ( const complx& b ) const;
  complx& operator += ( const complx& b );

  complx  operator +  ( double d ) const;
  complx& operator += ( double d );

  friend complx operator + ( double d, const complx& b );
};

...

complx complx::operator + ( const complx& b ) const
{
  return complx( real + b.real, imag + b.imag );
}

complx& complx::operator += ( const complx& b )
{
  real += b.real;
  imag += b.imag;
  return *this;
}

...

Hope this helps.
Last edited on
Topic archived. No new replies allowed.