CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 40
  1. #1
    Join Date
    Feb 2015
    Posts
    20

    adding rational number using only class functions

    Hi, i'm having trouble finishing this program. What I’m trying to do is create a class for Rational numbers. Get input from user for denominator and numerator. Store the numbers without a sign, and for the sign store it separately in char. I’m not supposed to use accessor functions. The part that I can’t seem to know how to do is use the info that was stored in the addUp function. This function need to add two rational numbers. i need to make this function general enough that it can handle two fractions, or a fraction and a whole number, or two whole numbers. What I already have here is readin function which reads in the numerator and denominator. setnumerator and setdenominator to assign positive values. setsign should get the sign of the fraction. Finally addUp should addUp which I explained before. I have some ideas about writing the tests, but can’t seem to know how to implement it to the program. The main program is still empty but all I’m supposed to do there is call the class functions, which my because of my lake of experience I can’t seem to figure out where to start.


    Code:
    #include <iostream>
    
    using namespace std;
    
    class Rational
    {
    private:
    	
    	int numerator, denominator;
    	char sign;
    	
    public:
    
    	Rational(const int denominator = 1, const char sign = '+');  // constructor.  Will set beginning balance to zero
    	int setnumerator(const int numerator);
    	int setdenominator(const int denominator);
    	char setsign(const int numerator, const int denominator, const char sign);
    	void PrintFraction();
    	void readint();
    	Rational addUp(Rational first, Rational second);
    
    };
    
    
    
    
    int main()
    {
    
    	
    
    
    
    	system ("PAUSE");
    	return 0;
    }
    
    
    
    
    
    Rational::Rational(const int denominator, const char sign)   // the constructor
    {
    
    }
    
    int Rational::setnumerator(const int numerator)
    {
    
    	if (numerator<0)
    		{
    			int numer = numerator * -1;
    			return numer;
    		}
    	else 
    		{
    			int numer = numerator;
    			return numer;
    		}
    
    }
    
    int Rational::setdenominator(const int denominator)
    {
    	
    		if (denominator<0)
    			{
    				int denom = denominator * -1;
    				return denom;
    			}
    		else 
    			{
    				int denom = denominator;
    				return denom;
    			}
    
    }
    
    char Rational::setsign(const int numerator, const int denominator, const char sign)
    {
    
    	if (denominator>0 && numerator>0 || denominator<0 && numerator<0)
    		{
    
    			char fianlsign = '+';
    			return fianlsign;
    		}
    	char fianlsign = '-';
    	return fianlsign;
    
    }
    
    void Rational::PrintFraction()
    {
    	if (denominator!=1)
    		{
    			cout<<sign<<numerator<<"/"<<denominator;
    		}
    	cout<<sign<<numerator;
    }
    
    void Rational::readint()
    {
    	
    	cout<<"enter numerator: ";
    	cin>>numerator;
    	cout<<"enter denominator: ";
    	cin>>denominator;
    
    	return;
    
    }
    
    Rational addUp(Rational first, Rational second)
    {
    
    }

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: adding rational number using only class functions

    Quote Originally Posted by stratovarios View Post
    Store the numbers without a sign, and for the sign store it separately in char.
    Better to use a bool for the sign (and name it something like isPositive or isNegative), since there can only be two possible states ever.
    Quote Originally Posted by stratovarios View Post
    I’m not supposed to use accessor functions.
    Your setnumerator and setdenominator functions violate this requirement.

    I think this assignment would really benefit from a TDD approach. I.e. first write the tests to determine what the interface of your class should look like. The goal should be to be able to use your class just like you would use an int or double. You'll then find out that accessor functions are indeed pointless for such a class.

    Note that currently, your addUp function takes three Rational numbers: first, second and *this. That's probably not what you want.
    Idiomatically, you would overload operator + for this, instead of using a named function. Then you can use the class like this:
    Code:
    Rational x(3, 5);
    Rational y(12, 13);
    Rational z = x + y;
    For more details, see the section 'Binary arithmetic operators' here.
    Last edited by D_Drmmr; February 23rd, 2015 at 04:51 AM.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: adding rational number using only class functions

    Before implementing this "addUp function" please, explain what your
    Code:
    	int setnumerator(const int numerator);
    	int setdenominator(const int denominator);
    are supposed to do? And why did you write so much lines of code for them rather than just use abs() function?
    So these both could be implemented (if you really need such "implementations") as
    Code:
    	int setnumerator(const int numerator) {return abs(numerator);}
    	int setdenominator(const int denominator) {return abs(denominator);}
    Victor Nijegorodov

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: adding rational number using only class functions

    Why isn't your constructor taking the values of numerator and denominator? Why is it taking sign when the value of sign is obtained from the numerator and denominator?

    If you are supposed to store these values without sign then the type would be unsigned int.

    setnumerator, setdenominator and setsign are not needed as public class members. If they are required at all (?) then they should be private.

    As D_drmmr indicated in post #2, you wouldn't have a class function addUp. You would have operator + (and other operators such as *, - etc) overloaded for the class. You would also overload the operator =.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: adding rational number using only class functions

    The AddUp() should be done in exactly the same way as you add rational numbers using pen and paper. That should be Obvious enough to do, since this is obviously homework/school assignment, we aren't supposed to help you with doing the work for you.

    If you have it coded then you can ask us about the implementation, but it is really up to you to get it into a functional state.

    Tips:
    1) the 2 parameters to the AddUp should be const references.

    2) If you're doing a rational number class you will probably want to create a helper function to figure out the GCD (greatest common divisor) so you can 'normalize' your rational numbers after each operation. so you can normalize for example 3/6 (GCD between 3 and 6 is 3) into 1/2
    There is a popular and efficient algorithm to determine the GCD of 2 numbers, I would assume that's somewhere in your course material.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: adding rational number using only class functions

    also:
    Code:
    Rational::Rational(const int denominator, const char sign)   // the constructor
    While not technically invalid, it is entirely pointless to make POD value types const.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: adding rational number using only class functions

    Quote Originally Posted by OReubens View Post
    it is entirely pointless to make POD value types const.
    Oh?? Doesn't it ensure that the values of the parameters are not 'accidentally ' changed in the function?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Feb 2015
    Posts
    20

    Re: adding rational number using only class functions

    Quote Originally Posted by VictorN View Post
    Before implementing this "addUp function" please, explain what your
    Code:
    	int setnumerator(const int numerator);
    	int setdenominator(const int denominator);
    are supposed to do? And why did you write so much lines of code for them rather than just use abs() function?
    So these both could be implemented (if you really need such "implementations") as
    Code:
    	int setnumerator(const int numerator) {return abs(numerator);}
    	int setdenominator(const int denominator) {return abs(denominator);}


    abs()? i dont know what it is. This is my first semester taking C++ so my knowledge is limited to the basics.

  9. #9
    Join Date
    Feb 2015
    Posts
    20

    Re: adding rational number using only class functions

    so unsigned int basically would store an int ignoring its sign? this would help vs just using int and making it positive.

  10. #10
    Join Date
    Feb 2015
    Posts
    20

    Re: adding rational number using only class functions

    we are now required to do common divisor since it is intermediate programming class. we can just leave the denominator and numerator as they are. the only this i have to do is leave the fraction as just and int if the denominator is 1 or -1.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: adding rational number using only class functions

    Quote Originally Posted by stratovarios View Post
    abs()? i dont know what it is. This is my first semester taking C++ so my knowledge is limited to the basics.
    From MSDN:
    abs
    Calculates the absolute value.

    int abs( int n );

    Return Value
    The abs function returns the absolute value of its parameter. There is no error return.

    Parameter
    n
    Integer value
    Victor Nijegorodov

  12. #12
    Join Date
    Feb 2015
    Posts
    20

    Re: adding rational number using only class functions

    i thought about using a bool for the sign because it makes so much sense, but im required to use char in this particular case.
    determine what the interface of your class should look like
    could you please explain a little more about what you meant here?

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: adding rational number using only class functions

    Quote Originally Posted by stratovarios View Post
    so unsigned int basically would store an int ignoring its sign? this would help vs just using int and making it positive.
    Not quite. If you assign a negative number to an unsigned int it makes the number very large and positive because of the way the bits are interpreted as a number. What an unsigned int means is that it will always hold a positive number. eg
    Code:
    unsigned int a;
    
    if (a >=0) //this will always be true
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: adding rational number using only class functions

    This is my first semester taking C++ so my knowledge is limited to the basics.
    ...
    since it is intermediate programming class
    ??

    to determine what the interface of your class should look like
    ...
    could you please explain a little more about what you meant here?
    Basically, how the class is to be used. In this case to add two numbers you would use
    Code:
    Rational a;
    Rational b;
    Rational c;
    
       c.addUp(a, b);
    whereas the normal way to add two numbers is to use the operator + eg
    Code:
    Rational a;
    Rational b;
    Rational c;
    
       c = a + b;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Feb 2015
    Posts
    20

    Re: adding rational number using only class functions

    In this case if i implement unsigned int wouldnt change the value of the input was a negative int?

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured