a question about rational class

hi,
I wrote this code about sum,subtraction,divided,multiple of fraction numbers.but when I enter 4 numbers it shows only number 2011 .plz tell me where is problem?
Tanx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
 
using namespace std;
class rational
{
public:
        int a,b,c,d;
    int plus (int ,int ,int ,int );
        int subtraction (int ,int ,int ,int );
            int multiple (int ,int ,int ,int );
                int divided (int ,int ,int ,int );
void printnumbers ();

};
                int rational :: plus(int a,int b,int c,int d)
                {
                    return a/b +c/d;
                }
                int rational :: subtraction(int a,int b,int c,int d)
                {
                    return a/b - c/d;
                }
                int rational :: multiple(int a,int b,int c,int d)
                    {
                        return (a/b) * (c/d);
                }
                int rational :: divided(int a,int b,int c,int d)
                {
                    return (a/b) / (c/d);
                }
                void rational :: printnumbers () 
                {
                    cout<< (a/b) +(c/d);
                        cout<< (a/b) -( c/d);
                            cout<< (a/b) * (c/d);
                                cout<< (a/b) / (c/d);
                                }
                
                int main()
                {
                    int a,b,c,d;
                cin>>a>>b>>c>>d;
                rational p;
                p.plus(a,b,c,d);
                p.subtraction(a,b,c,d);
                p.multiple(a,b,c,d);
                    p.divided(a,b,c,d);
p.printnumbers () ;
                }
Last edited on
nobody doesn't see my post?plz help me.
If you want to have space between the numbers you have to output space.
cout << ' ';
Last edited on
ok,you're right but when I enter any numbers as input It shows only number 2012 in output.It should give 4 numbers as output but gives only 2012
Your rational class is confused in it's functions: "Should I use the a,b,c,d that were passed to the function, or the a,b,c,d that I own?". You never assign anything to rational's a,b,c,d so that is probably why you get some junk.

Quick fix, make a constructor and remove the function arguments:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class rational
{
public:
  int a,b,c,d;
  
  //constructor
  rational(int a, int b, int, c, int d);

  int plus (void);
  // other functions
};
rational::rational(int a, int b, int c, int d)
{
  this->a = a;
  this->b = b;
  this->c = c;
  this->d = d;
}

int rational :: plus(void)
{
  return a/b +c/d;
}

int main(void)
{
  rational myrational(1,2,3,4);
  myrational.print();
}

The use of ints won't work very well for this - if the user entered 1,2, 3, 4 then then answer for the plus function would 0.

If you want a floating point answer, make the types double.

However you can do it with ints by using cross multiplying to get the answer. This is the mathematical way of doing things. Might also need GCD to reduce the fraction.
why should rational class has constructor function?(we don't want assign value to numbers). we get 4 numbers(with cin in int main) and they are passed to function arguments . Is it right? Is this program written without constructor function?
Last edited on
Is this program written without constructor function?
I provided a quick fix to a problem. You don't need a constructor, but then if you want printnumbers(void) you need to assign a,b,c,d to rational's a,b,c,d for every operation.
Topic archived. No new replies allowed.