Composition: Complex number in fraction

This is a discussion on Composition: Complex number in fraction within the C++ Programming forums, part of the General Programming Boards category; I have the following classes for complex numbers and fractions: Code: class Fraction { int top, bottom; public: Fraction(int a ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Why not:
    Code:
    Fraction(const complexClass& a = complexClass(), const complexClass& b = complexClass());
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Copying just the address! Why didn't I think of that?

    Thanks!

    By the way, what's the const for?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    You're not going to modify the argument, just copy it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Well, this may be a stupid question but how do I assign the address to the private variable?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    You shouldn't assign an address. You should initialise the private member variables to be copies of the parameters.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    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;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thanks again! Without you, I'd be scratching my head for a long time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL DC Buffer Renders slow
    By Lane the Great in forum Game Programming
    Replies: 10
    Last Post: 01-07-2011, 06:52 PM
  2. Replies: 8
    Last Post: 12-23-2009, 12:55 PM
  3. Reading negative numbers from command line
    By Ace2187 in forum C Programming
    Replies: 3
    Last Post: 12-06-2009, 12:21 PM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21