Doh...
Oct 20, 2011 at 6:12pm UTC
Create a class called Complex for performing arithmetic with complex numbers. Use double variables to represent the private data of the class. Implement the following public functions:
1 2 3 4 5 6 7 8 9
void set(double , double );
void get(double &, double &) const ;
void print() const ;
Complex add(const Complex&) const ;
Complex subtract(const Complex&) const ;
Complex multiply(const Complex&) const ;
Complex divide(const Complex&) const ;
Complex();
Complex(double , double );
Now, before you start banging angrily at your keyboard telling me that we don't solve assignments here, read on.
Why do I need this:
void get(double &, double &) const ;
and what would go in there?
For some reason I keep going blank.
Last edited on Oct 20, 2011 at 6:16pm UTC
Oct 20, 2011 at 6:47pm UTC
One of those is the "real" portion of the number and the other is the "imaginary" portion. The same as the set function.
They're passed by reference because functions cannot return two values. So the way to output two values is to pass by reference.
Oct 20, 2011 at 8:30pm UTC
Ok I think I get what you mean, here is what I wrote for set and get:
1 2 3 4 5 6 7 8 9 10 11
void Complex :: set(double REAL, IMAGINARY)
{
real=REAL;
imaginary=IMAGINARY;
}
void Complex :: get(double & REAL, double & IMAGINARY) const
{
REAL=real;
IMAGINARY=imaginary;
}
I've also finished writing the code for all the other functions, except for
Complex();
and
Complex(double , double );
because I do not understand the purpose of these...
Last edited on Oct 20, 2011 at 8:31pm UTC
Oct 20, 2011 at 8:42pm UTC
except for Complex(); and Complex(double, double); because I do not understand the purpose of these...
Those are constructors. They initialize the object to a known state. They're similar to 'set' in this case, but are only called once when an object is created.
Oct 20, 2011 at 8:48pm UTC
Still don't get their purpose... Here is my finished code, it compiles and the math is correct (I checked it against professor's example) and completely ignored the constructors. So what should I put in these constructors and why?
Also, I never used the get() function... I don't understand why that is there either...
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
#include<iostream>
using namespace std;
class Complex
{
private :
double real;
double imaginary;
public :
void set(double , double );
void get(double &, double &) const ;
void print() const ;
Complex add(const Complex&) const ;
Complex subtract(const Complex&) const ;
Complex multiply(const Complex&) const ;
Complex divide(const Complex&) const ;
};
void Complex :: set(double REAL, double IMAGINARY)
{
real=REAL;
imaginary=IMAGINARY;
}
void Complex :: get(double & REAL, double & IMAGINARY) const
{
REAL=real;
IMAGINARY=imaginary;
}
void Complex :: print() const
{
cout<<"(" <<real<<", " <<imaginary<<")" <<endl<<endl;
}
Complex Complex :: add(const Complex& complex) const
{
Complex result;
result.real=complex.real+real;
result.imaginary=complex.imaginary+imaginary;
cout<<"(" <<result.real<<", " <<result.imaginary<<")" ;
}
Complex Complex :: subtract(const Complex& complex) const
{
Complex result;
result.real=complex.real-real;
result.imaginary=complex.imaginary-imaginary;
cout<<"(" <<result.real<<", " <<result.imaginary<<")" ;
}
Complex Complex :: multiply(const Complex& complex) const
{
Complex result;
result.real=(complex.real*real)-(complex.imaginary*imaginary);
result.imaginary=(complex.real*imaginary)+(complex.imaginary*real);
cout<<"(" <<result.real<<", " <<result.imaginary<<")" ;
}
Complex Complex :: divide(const Complex& complex) const
{
Complex result;
result.real=((complex.real*real)+(complex.imaginary*imaginary))/(real*real+imaginary*imaginary);
result.imaginary=((complex.imaginary*real)-(complex.real*imaginary))/(real*real+imaginary*imaginary);
cout<<"(" <<result.real<<", " <<result.imaginary<<")" ;
}
int main()
{
Complex complex1;
Complex complex2;
float real, imaginary;
cout<<"Please enter the real and imaginary parts of the first number: " ;
cin>>real>>imaginary;
complex1.set(real,imaginary);
complex1.print();
cout<<"\n\nPlease enter the real and imaginary parts of the second number: " ;
cin>>real>>imaginary;
complex2.set(real,imaginary);
complex2.print();
cout<<"\n\nThe sum of the two numbers is: " ;
complex2.add(complex1);
cout<<"\n\nThe subtraction of the two numbers is: " ;
complex2.subtract(complex1);
cout<<"\n\nThe multiplication of the two number is: " ;
complex2.multiply(complex1);
cout<<"\n\nThe division of the two number is: " ;
complex2.divide(complex1);
cout<<endl<<endl;
return 0;
}
Last edited on Oct 20, 2011 at 8:51pm UTC
Oct 20, 2011 at 9:21pm UTC
Still don't get their purpose...
Try this:
1 2 3 4 5
Complex foo;
foo.print(); // ??
Complex bar(10,5);
bar.print();
Topic archived. No new replies allowed.