Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,262 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,508 people online right now. Registration is fast and FREE... Join Now!




rational number class problem

2 Pages V  1 2 >  
Reply to this topicStart new topic

rational number class problem, classes

rhouzer
11 Dec, 2006 - 07:08 PM
Post #1

New D.I.C Head
*

Joined: 6 Dec, 2006
Posts: 14


My Contributions
I am trying to create a rational number class. But i am new to classes and am unable to get the syntax rite for my class and therefore i can't even see if I've correctly implemented my rational number class. Any help would be greatly appreciated.


CODE
// Roger Leopold
// Create a rational number class

#include <iostream>
#include <cmath>

using namespace std;

class RationalT
{
    public:
        int constructor(void)            // 1/1
        {
            Set(1,1);
        }

        int constructor(int a)            // a/1
        {
            Set(a,1);
        }

        int constructor(int a,int b)        // a/b
        {
            Set(a,b);
        }

        void Print(void)            // print the number in the form a/b
        {
            cout << num1 << "/" << num2 << endl;

            return;
        }

        int Numerator(void)            // returns the numerator
        {
            
            return num1;
        }

        int Denominator(void)            // returns the denominator
        {

            return num2;
        }

        double RealValue(void)            // returns a/b
        {
            return value;
        }

        int Ceil(void)                //returns ceiling of a/b
        {
            int ans;

            ans = (int) ceil(value);

            return ans;
        }

        int Floor(void)                //returns floor of a/b
        {
            int ans;

            ans = (int) floor(value);

            return ans;
        }

        int Round(void)                // returns a/b rounded
        {
            int ans;

            if ((num1/num2) > 5)
            {
                ans = (int) ceil(value);
            }
            else
            {
                ans = (int) floor(value);
            }

            return ans;
        }

        bool IsInteger(void)            //returns true if b = 1 false otherwise
        {
            bool isInt;

            if(num2 == 1)
            {
                isInt = true;
            }

            return isInt;
        }

        RationalT Add(RationalT x)        //returns this + x
        {
            int gcd;
            int a,b;
            double ans;

            if (num2 == x.num2)
            {
                a = num1+x.num1;
            }
            else
            {
                a = (num1)*(x.num2);
                b = (num2)*(x.num2);
                a = num1 + ((x.num1)*(num2));                
            }

            gcd = GCD(a,b);
            a = num1/gcd;
            b = num2/gcd;

            ans = a/b;

            return ans;
        }

        RationalT Sub(RationalT x)        //returns this - x
        {
            int gcd;
            int a,b;
            double ans;

            if (num2 == x.num2)
            {
                a = num1-x.num1;
            }
            else
            {
                a = (num1)*(x.num2);
                b = (num2)*(x.num2);
                a = num1 - ((x.num1)*(num2));                
            }

            gcd = GCD(a,b);
            a = num1/gcd;
            b = num2/gcd;

            ans = a/b;

            return ans;
        }

        RationalT Multiply(RationalT x)        // returns this * x
        {
            int gcd;
            int a,b;
            double ans;

            a = (num1*x.num1);
            b = (num2*x.num2);

            gcd = GCD(a,b);
            a = a/gcd;
            b = b/gcd;

            ans = a/b;

            return ans;
        }

        RationalT Divide(RationalT x)        // returns this/x
        {
            int gcd;
            int a,b;
            double ans;

            a = num1*num2;
            b = num2*num1;

            gcd = GCD(a,b);
            a = a/gcd;
            b = b/gcd;

            ans = a/b;

            return ans;
        }

        void SetNumerator(int )            //set the numerator part of the number
        {
            cin >> num1;

            return;
        }

        void SetDenominator(int )        //set the denominator part of the number
        {
            cin >> num2;

            return;
        }

        void Set(int n, int d)            //set the numerator to be n, the denominator to be d
        {
            int gcd;

            if ((n < 0) && (d < 0))
            {
                n = (-1*n);
                d = (-1*d);
            }

            gcd = GCD(n,d);
            n = n/gcd;
            d = d/gcd;

            num1=n;
            num2=d;
            value=(num1/num2);

            return;
        }


    private:
        int num1, num2;
        double value;

        int GCD(int a,int b)
        {  
            int tmp;

            a = abs(a);
            b = abs(b);

            if (a < b)
            {
                tmp = a;
                a = b;
                b = tmp;
            }

            while(b > 0)
            {
                tmp = b;
                b = a %b;
                a = tmp;
            }

            return(a);
        }

        int abs(int x)
        {
            int y;

            if (x<0)
            {
                y=(-1)*(x);
            }

            return y;
        }

};

int main()
{


    return;
}




This post has been edited by rhouzer: 12 Dec, 2006 - 12:26 AM
User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Rational Number Class Problem
11 Dec, 2006 - 07:15 PM
Post #2

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
so your asking on the body of your main function
just it is simple like this...
CODE
<class name> <assign name>;


CODE
<assign name>.<function on your class>();
is you calling a function inside your main.
User is offlineProfile CardPM
+Quote Post

rhouzer
RE: Rational Number Class Problem
11 Dec, 2006 - 07:23 PM
Post #3

New D.I.C Head
*

Joined: 6 Dec, 2006
Posts: 14


My Contributions
1)yeah i have that part down. the part i cant get now is the part with cout/cin and the endl. Im not sure what to do to get them to work. I know i have to o something dont know what.

2)and also another question im trying to get my class to add and sub and mult and that without doing the class.func() and that just the + (or equivalent) but i dont know if i implemented that correct.
User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Rational Number Class Problem
11 Dec, 2006 - 07:31 PM
Post #4

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
CODE
cout >>"HERE your statement"<<[assign classname].[function of the class].();


CODE
cin << [assign name] . [function] ();


Adding result came from the function of the class?!?!that whats your question??biggrin.gif


User is offlineProfile CardPM
+Quote Post

rhouzer
RE: Rational Number Class Problem
11 Dec, 2006 - 07:35 PM
Post #5

New D.I.C Head
*

Joined: 6 Dec, 2006
Posts: 14


My Contributions
Well i cant do cin and cout in my class. I get an undeclared error when compiling and i know i have to declare it for it to work but i don't know how. (i cant get past this and can't compile)

Then my other question is im trying to overload it so i can do like two of that class added together without the name.whatever. I cant remember how or find how and my question is what do i need to do, to do that.
User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Rational Number Class Problem
11 Dec, 2006 - 07:39 PM
Post #6

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
What cout/cin inside are you sure??...did you want your function of your class interacts???...maybe some declaration fixes you should want to double check.biggrin.gif
User is offlineProfile CardPM
+Quote Post

rhouzer
RE: Rational Number Class Problem
11 Dec, 2006 - 07:49 PM
Post #7

New D.I.C Head
*

Joined: 6 Dec, 2006
Posts: 14


My Contributions
Yeah i need to print the rational number. And its suppose to be a function of the classes. I need to overload the operators so that it will display the rational number when i name.print it displays the rational number. But i cant use cin/cout without declaring them. I remember the discussion my teacher made it a point to say that the didn't need to be called cin or cout but it was a while ago now and i don't know how to do that and cant find out how.
User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Rational Number Class Problem
11 Dec, 2006 - 07:55 PM
Post #8

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
just called the funtion...on the display function of your class..you dont need to call the whole class name with function..just the function it self.biggrin.gif
User is offlineProfile CardPM
+Quote Post

rhouzer
RE: Rational Number Class Problem
11 Dec, 2006 - 07:58 PM
Post #9

New D.I.C Head
*

Joined: 6 Dec, 2006
Posts: 14


My Contributions
Can you show me what you mean? I don't what you mean. (This is the first time i have actually worked with classes, ive only worked with structs before)

and these are what my class is required to have in it:

o constructor(void) 1/1
o constructor(int a) a/1
o constructor(int a,int cool.gif a/b
o void Print(void) print the number in the form a/b
o int Numerator(void) returns the numerator
o int Denominator(void) returns the denominator
o double RealValue(void) returns a/b
o int Ceil(void) returns ceiling of a/b
o int Floor(void) returns floor of a/b
o int Round(void) returns a/b rounded
o bool IsInteger(void) returns true if b = 1 false otherwise
o RationalT Add(RationalT x) returns this + x
o RationalT Sub(RationalT x) returns this - x
o RationalT Multiply(RationalT x) returns this * x
o RationalT Divide(RationalT x) returns this/x
o void SetNumerator(int ) set the numerator part of the number
o void SetDenominator(int) set the denominator part of the number
o void Set(int n, int d) set the numerator to be n, the denominator to be d

This post has been edited by rhouzer: 11 Dec, 2006 - 07:59 PM
User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Rational Number Class Problem
11 Dec, 2006 - 08:14 PM
Post #10

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
for example:
CODE

RationalT Add(RationaIT x)
{
x=cout >>"Add answer is "<<Numerator()<<"/"<<Denuminator()<<".";
}



just give you idea..biggrin.gif
User is offlineProfile CardPM
+Quote Post

rhouzer
RE: Rational Number Class Problem
11 Dec, 2006 - 08:28 PM
Post #11

New D.I.C Head
*

Joined: 6 Dec, 2006
Posts: 14


My Contributions
when i compile it still i get a cout undeclared. Im suppose to have a print function there but idk how i go about using output stream in it.
User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Rational Number Class Problem
11 Dec, 2006 - 08:29 PM
Post #12

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
CODE
std::cout
or use this
CODE
using namespace std;
...
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/4/08 07:06PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month