# Composition: Complex number in fraction

• 03-16-2011
843
Composition: Complex number in fraction
I have the following classes for complex numbers and fractions:

Code:

```class Fraction {     int top, bottom; public:     Fraction(int a = 0, int b = 0);     ~Fraction(){}     void displayFraction();     void operator= (Fraction x);         Fraction operator+ (Fraction x);         Fraction operator- (Fraction x);         Fraction operator* (Fraction x);         Fraction operator/ (Fraction x); }; Fraction::Fraction (int a, int b) {     top = a; bottom = b; }```
Code:

```class complexClass {         double x, y; public:         complexClass(double r=0, double i=0): x(r), y(i) {}         ~complexClass(){}         void displayComplex(void);         void operator= (complexClass a) {x=a.x; y=a.y;};         complexClass operator+ (complexClass a);         complexClass operator- (complexClass a);         complexClass operator* (complexClass a);         complexClass operator/ (complexClass a);         complexClass operator^ (double n); };```
I am supposed to combine their functionality together so that I can input fractions of complex numbers and perform arithmetic operations on them. How do I go about doing it?

I tried creating the constructor Fraction(complexClass a) but I'm stuck at the private variable assignment.
• 03-16-2011
laserlight
Looks like a matter of appropriately substituting complexClass for int in Fraction.

Note that for both classes you don't need to define the copy constructor, copy assignment operator and destructor as the compiler generated versions will suffice.
• 03-16-2011
843
I was thinking of something along this line...
Code:

`Fraction(int a = 0, int b = 0, int c = 1, int d = 0);`
...which would easily solve the parameter issue, but it requires significant changes to the codes. I was wondering if there is an easier way.
• 03-16-2011
laserlight
Why not:
Code:

`Fraction(const complexClass& a = complexClass(), const complexClass& b = complexClass());`
• 03-16-2011
843
Copying just the address! Why didn't I think of that?

Thanks!

By the way, what's the const for?
• 03-16-2011
laserlight
You're not going to modify the argument, just copy it.
• 03-16-2011
843
Well, this may be a stupid question but how do I assign the address to the private variable?
• 03-16-2011
laserlight
You shouldn't assign an address. You should initialise the private member variables to be copies of the parameters.
• 03-16-2011
843
I'm guessing it's like this...

Code:

```Fraction(const complexClass& a = complexClass(), const complexClass& b = complexClass()): top(a.x), bottom(b.y) {};```
...but they are inaccessible since these variables are private to complexClass.
• 03-16-2011
laserlight
If all you are going to do is to store a.x and b.y, then you might as well not involve complexClass. Rather, you want to change Fraction itself to work with complexClass objects. That is, change this:
Code:

```class Fraction {     int top, bottom;```
to:
Code:

```class Fraction {     complexClass top, bottom;```
• 03-16-2011
843
Thanks again! Without you, I'd be scratching my head for a long time.