hello all,
i have almost finished a C++ program but am not sure where to place these lines and was hoping for some direction. i know i need to implement:
complx &operator + (double, complx);
complx &operator - (double, complx);
complx &operator * (double, complx);
but i dont exactly know where to place them. Im pretty sure they will go respectively in the complx.cpp page. Any help would be appreciated. and sorry about the length of this post. i've underlined a line of code towards the very bottom where i am getting my compiling error and im pretty sure it has to do with not including complx &operator (double, complx); in the complx.cpp file. I no longer have my notes on alot of this after formating my hard drive and forgetting to save them. so again, thanks for any help given
Code:
// 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 );
// 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;
}
// define overloaded - (minus) operator
complx complx::operator- (complx c)
{
complx result;
result.real = (this->real - c.real);
result.real = (this->imag - c.imag);
return result;
}
// define overloaded * (multiplication) operator
complx complx::operator* (complx c)
{
complx result;
result.real = (this->real * c.real);
result.real = (this->imag * c.imag);
return result;
}
header file
Code:
// 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-(double); //operator-()with double
complx operator*(complx); // operator*()
complx operator*(double); //operator*() with double
//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 ifstream &operator >> ( ifstream& in_file, complx &number );
complx &operator + (double, complx);
complx &operator - (double, complx);
complx &operator * (double, complx);
call_complx
Code:
//call_complx.cpp
#include "complx.h"
ifstream infile ("in.dat");
int main()
{
int i=0;
complx in[5];
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::operator+()
complx s2 = d + in[2]; // overload operator+()
complx s3 = in[3] + d; // overload operator+()
complx a1 = in[4] – in[5]; // calls complx::operator-()
complx a2 = d - in[6]; // overload operator-()
complx a3 = in[7] - d; // overload operator-()
complx mm1 = in[3] * in[4]; //calls complx::operator*()
complx mm2 = d * in[5]; // overload operator*()
complx mm3 = in[6] * d; //overload operator*()
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 " << a1 <<endl;
cout << "The subtract is a complex number " << a2 <<endl;
cout << "The subtract is a complex number " << a3 <<endl;
cout << "The product is a complex number " << mm1 <<endl;
cout << "The product is a complex number " << mm2 <<endl;
cout << "The product is a complex number " << mm3 <<endl;
cout << "The subtract is a complex number " << b <<endl;
cout << "The product is a complex number " << dm <<endl;
return 0; //successful termination
}