I have an assignment due for my intro c++ class and while i have done well on the other exercises, this one i feel over my head. I'm not trying to cheat or get the answer but i need very simple, clear instructions on what to do. I have asked many other classmates about this and they all seem even more lost than i am (if possible). Also i have never posted here so sorry if i format/do something seriously wrong. i apologize in advance.
the instructions: In this assignment 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.
What i have so far (not the original source code i was given, ive added/changed stuff to it so it may be all over the place.
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
|
// Complx.h
#include <iostream>
#include <fstream>
using namespace std;
class complx
{
double real;
double imag;
public:
complx( double real = 0, double imag = 0); // constructor
complx operator+(complx); // operator+()
complx operator+(double); // operator+()with double //(red)
complx operator- (complx); // operator-() //(red)
complx operator* (complx); // operator*() //(red)
complx &operator + (double, complx); //(red)
complx &operator - (double, complx); //(red)
complx &operator * (double, complx); //(red)
bool operator== (complx); // operator==() //(red)
//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;
}
};
extern ostream &operator << ( ostream &out_file, complx &number );
extern istream &operator >> ( istream &in_file, complx &number );
extern ifstream &operator >> ( ifstream &in_file, complx &number );
|
for this code ^^ i keep getting an error that lines 18, 19, and 20 need to take either zero or one arguement. I don't know how to fix this--is it because they are in the class and they should be outside? or is the entire thing just all wrong?
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
|
// Complx.cpp
#include <iostream>
#include <fstream>
#include "complx.h"
using namespace std;
// define I/O stream
// define constructor
complx::complx( double r, double i )
{
real = r; imag = i;
}
complx complx::operator+ (double d, complx c)
{
complx result;
result.Real() = (d + c.real );
result.Imaginary() = (d + c.imag);
return result;
}
complx &operator - (double d, complx c)
{
//complx result (d - c.Real(), c.Imaginary());
//return result;
}
complx &operator * (double d, complx c)
{
// complx result (d * c.Real(), c.Imaginary());
// return result;
}
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;
}
ostream &operator<< ( ostream &out_file, complx number )
{
out_file << '(' << number.Real() << ',' << number.Imaginary() << ')';
return out_file;
}
// 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 + (plus) operator (with double)
complx complx::operator+ (double d)
{
complx result;
result.real = (this->real +d);
result.imag = (this->imag);
return result;
}
//define overloaded - (minus) operator
complx complx::operator- (complx c)
{
complx result;
result.real = (this->real - c.real);
result.imag = (this->imag - c.imag);
return result;
}
//define overloaded * (multiplication) operator
complx complx::operator* (complx c)
{
complx result;
result.real = (this->real * c.real) - (this->imag * c.imag);
result.imag = (this->imag * c.imag) + (this->imag * c.real);
return result;
}
//define overloaded = bool operator
bool complx::operator== (complx b)
{
return ((this->real == b.real) && (this->imag == b.imag));
}
|
i quite frankly dont have a specific question to ask about this code ^^ because i'm trying to fix the .h flie but i'm including it so you know what else i have if needed.
last code:
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
|
//call_complx.cpp
#include "complx.h"
#include <iostream>
#include <fstream>
using namespace std;
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::operator+()
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
}
|
ok. so now you have everything. i know people prefer exact questions instead of broad ones which i tried to do, but really any help you can give is welcome. I asked a friend for help and he ended up adding so much to the .h file and not much to the complx.cpp file. It was very confusing and i'm not sure will satisfy what my prof wants. Anyways thanks again.