I'm taking CMIS140, and I have a project using complex numbers. I can't figure out how to write the calculations into the program. I have attached the project, and the code I have written at this point. If anyone has any suggestions on how to do this, I would appreciate it.
scrappy 0 Newbie Poster
This attachment is potentially unsafe to open. It may be an executable that is capable of making changes to your file system, or it may require specific software to open. Use caution and only open this attachment if you are comfortable working with msword files.
#include <iostream>
#include <iomanip>
#include <cstdio>
class ComplexNumbers
{
private:
double realpart;
double imagpart;
public:
//Default constructor - set realpart = 0.0, imagpart = 0.0
ComplexNumbers ();
//Initializing constructor - realpart = a, imagpart = b
ComplexNumbers (double a, double b);
//Transformer - Gets complex numbers
void GetComp();
//Transformer - Adds two complex numbers together and returns their sum
ComplexNumbers AddComp (const ComplexNumbers&, const ComplexNumbers&);
//Transformer - Subract two complex numbers from each other and returns their difference
ComplexNumbers SubComp (const ComplexNumbers&, const ComplexNumbers&);
//Transformer - Multiplies two complex numbers together and returns their product
ComplexNumbers MultComp (const ComplexNumbers&, const ComplexNumbers&);
//Transformer - Divides two complex numbers and returns their XXXXX
ComplexNumbers DivComp (const ComplexNumbers&, const ComplexNumbers&);
//Transformer - Calculates the absolute value of two complex numbers and returns their XXXXX
ComplexNumbers AbsComp (const ComplexNumbers&, const ComplexNumbers&);
//Informer - displays a number as n' m"
void ShowComp();
};
using namespace std;
int main()
{
ComplexNumbers numb1, numb2(1, 1);
cout << "\n\nNumb1 default constructor values: \n";
numb1.ShowComp();
cout << "\n\nChange numb1 values: \n";
numb1.GetComp();
cout << "\n\nNumb2 initialized constructor values: \n";
numb2.ShowComp();
cout << "\n\nThe sum of Numb1 and Numb2: \n";
ComplexNumbers sumComp;
sumComp = sumComp.AddComp (numb1, numb2);
sumComp.ShowComp();
cout << "\n\nThe difference of Numb1 and Numb2: \n";
ComplexNumbers diffComp;
diffComp = diffComp.SubComp (numb1, numb2);
diffComp.ShowComp();
cout << "\n\nThe product of Numb1 and Numb2: \n";
ComplexNumbers prodComp;
prodComp = prodComp.MultComp (numb1, numb2);
prodComp.ShowComp();
cout << "\n\nThe XXXXXXXXXXX of Numb1 and Numb2: \n";
ComplexNumbers sepComp;
sepComp = sepComp.DivComp (numb1, numb2);
sepComp.ShowComp();
cout << "\n\nThe absolute value of Numb1 and Numb2: \n";
ComplexNumbers avalComp;
avalComp = avalComp.AbsComp (numb1, numb2);
avalComp.ShowComp();
return 0;
}
ComplexNumbers::ComplexNumbers()
{
realpart = 0.0; imagpart = 0.0;
}
ComplexNumbers::ComplexNumbers( double a, double b)
{
realpart = a; imagpart = b;
}
void ComplexNumbers::GetComp ()
{
cout << "\nEnter realpart: "; cin >> realpart;
cout << "\nEnter imagpart: "; cin >> imagpart;
}
void ComplexNumbers::ShowComp()
{
cout << setw(3) << realpart << "\'"
<< setw(3) << imagpart << '\"';
}
ComplexNumbers::AddComp (const ComplexNumbers& N1, const ComplexNumbers& N2)
{
ComplexNumbers T;
T.realpart = N1.realpart + N2.realpart;
T.imagpart = N1.imagpart + N2.imagpart;
return T;
}
ComplexNumbers::SubComp (const ComplexNumbers& N1, const ComplexNumbers& N2)
{
ComplexNumbers T;
T.realpart = N1.realpart - N2.realpart;
T.imagpart = N1.imagpart - N2.imagpart;
return T;
}
jwenting 1,889 duckman Team Colleague
paste your code inside code tags instead of attaching the sourcefiles. M
Makes it a lot easier for everyone to see what you're talking about.
And posting your assignment verbatim doesn't help either (but at least you're honest stating it's a school assignment ;)).
Now for a bit of help to get you started.
I've yesterday for fun and training (I've been more or less out of C++ for years, starting to get pretty rusty) created a class that does addition and subtraction of complex numbers.
The rest you can easily think up to do yourself, they work similarly.
I'll not post the full implementation (which is pretty trivial) nor the test application (which is even more trivial), but the headerfile giving all function prototypes.
That should give you enough hints to get started implementing the system yourself.
#include <iostream>
using namespace std;
class complexNumber
{
private:
float real; // real part
float imag; // imaginary part
public:
complexNumber();
complexNumber(float rPart, float iPart);
~complexNumber(); // for form's sake, not really needed
complexNumber operator+(const complexNumber& c); // addition, so you can call 'c = a + b' where a,b, and c are complexNum objects
complexNumber operator-(const complexNumber& c); // same for subtraction
friend ostream& operator<<(ostream& s, const complexNumber& c); // so you can do for example 'cout << a' where a is a complexNumber object
};
you can of course include far more functionality than this, but this should be a good start.
scrappy 0 Newbie Poster
Thank you for your help, and it did help. Now I keep getting the following error, and I don't know what it means.
error C2556: 'int __thiscall ComplexNumbers::AddComp(const class ComplexNumbers &,const class ComplexNumbers &)' : overloaded function differs only by return type from
jwenting 1,889 duckman Team Colleague
You probably made a typo somewhere in the return type of a function.
If you define 2 functions of the same name (overloaded functions) in the same namespace (or class) they are required to have different argument lists else the compiler and runtime don't know which function to call.
Narue 5,707 Bad Cop Team Colleague
>they are required to have different argument lists
Or constness for member functions. The following introduces no ambiguity yet the return types and argument lists for the overloaded function are identical:
#include <iostream>
using std::cout; using std::endl;
class test {
public:
void f() { cout<<"f()"<<endl; }
void f() const { cout<<"f() const"<<endl; }
};
int main()
{
test t;
const test s;
t.f(); // Will print f()
s.f(); // Will print f() const
}
lohith.s1985 0 Newbie Poster
i am not able to run the program on complex numbers can any one help me out
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
what code are you talking about?
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.