Thread: Returning object from function

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Returning object from function

    I'm trying to return an object from the function toFraction, which takes a decimal number as input and returns an object with the converted fraction class. The problem I'm having is storing the object, and then displaying it. Any help is appreciated.

    Code:
    class Fraction {
        int top, bottom;
    
    public:
        Fraction(int a = 0, int b = 1);
        ~Fraction(){}
    
    Fraction::Fraction (int a, int b) {
        if (b < 0){
            a = -a; b = -b;}
    
        top = a; bottom = b;
    }
    
    Fraction Fraction::toFraction(double x) {
        int n;
        double u,v;
    
        for (n = 1; u < 1; n++){
        u = x*10*n; cout << u;
        v = 10*n;}
    
        Fraction temp(u,v);
        return temp;
    }
    
    int main(void)
    {
        Fraction a(5,-8);
        Fraction b(1,0);
        Fraction c;
        double dec = 0.00345;
    
        cout << "Decimal value = " << dec <<endl;
        c.toFraction(dec);
        cout << "Converted to fraction = ";
        c.display();
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    class Fraction {
        int top, bottom;
    
    public:
        Fraction(int a = 0, int b = 1);
        ~Fraction(){}
    
    Fraction::Fraction (int a, int b) {
        if (b < 0){
            a = -a; b = -b;}
    
        top = a; bottom = b;
    }
    
    Fraction Fraction::toFraction(double x) const {
        int n;
        double u,v;
    
        for (n = 1; u < 1; n++){
        u = x*10*n; cout << u;
        v = 10*n;}
    
        return Fraction(u,v);
    }
    
    int main(void)
    {
        Fraction a(5,-8);
        Fraction b(1,0);
        Fraction c;
        double dec = 0.00345;
    
        cout << "Decimal value = " << dec <<endl;
        cout << "Converted to fraction = ";
        c.toFraction(dec).display();
    
        return 0;
    }
    Maybe this way.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    Thank you! The multiple dots must have confused me. What does the const do in this case? To prevent the object from changing?

    By the way, why is the resulting fraction automatically simplified? I have yet to implement this. EDIT: Ah, it's rounded off.
    Last edited by 843; 03-14-2011 at 10:54 AM.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    'const' in this case informs the compiler that the method will not alter object state, thus it can be used on an instance marked as const.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM