Thread: Composition: Complex number in fraction

  1. #1
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    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
    28,413
    Why not:
    Code:
    Fraction(const complexClass& a = complexClass(), const complexClass& b = complexClass());
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    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
    28,413
    You're not going to modify the argument, just copy it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    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
    28,413
    You shouldn't assign an address. You should initialise the private member variables to be copies of the parameters.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    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
    28,413
    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;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    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, 07:52 PM
  2. Replies: 8
    Last Post: 12-23-2009, 01:55 PM
  3. Reading negative numbers from command line
    By Ace2187 in forum C Programming
    Replies: 3
    Last Post: 12-06-2009, 01: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