Need help with Complx

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Need help with Complx

Postby perproject06 » Tue Oct 12, 2010 8:23 pm

I need help with this problem. Any input would be appreciated.

Problem Statement
You need to implement a class, complx, to manipulate complex numbers: a+bi.
In the class, you need to implement the following arithmetic operators (in red) of this data type.

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

The following code implements the following arithmetic operations. You need to implement the rest.
// 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::operator+ (complx c)
{
complx result;
result.real = (this->real + c.real);
result.imag = (this->imag + c.imag);
return result;
}
perproject06
 
Posts: 1
Joined: Tue Oct 12, 2010 8:12 pm

Return to Homeworks

Who is online

Users browsing this forum: No registered users and 2 guests