Not able to figure out this parameterized constructor.

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
#include <iostream>
#include <stdlib.h>

using namespace std;

class rational
{
    int num, den, num1, den1, num2, den2;
public:
    rational()
    {
        num=0; den=1;
    }
    rational(int a, int b, int c, int d)
    {
        num1=a; den1=b; num2=c; den2=d;
    }
};

int main()
{
    rational rat;
    int p, q, r, s, ch;
    cout<<"\nEnter 2 rational numbers.\n\n";
    cout<<"Rational A= \n"; cin>>p; cout<<"/"; cin>>q;
    cout<<"\nRational B= \n"; cin>>r; cout<<"/"; cin>>s;
    rational(p,q,r,s);
}


Did I initialise the constructors right ?
I tried to pass these no.s in the main() but it is passing some random 7-8 digit values.
I checked it by making an output from the class.
Please suggest where I am wrong ?
Last edited on
figured it out.
Hi,

figured it out.


I wonder about that .... :+)

It's just that you have a conceptual problem: Your rational class should be for one fraction not two. Then provide your own operators to do math to them, print them out etc.

Also consider using a member initialisation list rather than assignment in the ctor.

Having using namespace std; is bad, Gogle to see why :+)

Hope all goes well :+)
Topic archived. No new replies allowed.