Returning object from function

This is a discussion on Returning object from function within the C++ Programming forums, part of the General Programming Boards category; I'm trying to return an object from the function toFraction, which takes a decimal number as input and returns an ...

  1. #1
    843
    843 is offline
    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
    681
    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.
    I never put signature, but I decided to make an exception.

  3. #3
    843
    843 is offline
    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
    681
    '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.
    I never put signature, but I decided to make an exception.

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, 01:28 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM

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