Printing fractions with classes help?

So I am way lost as usual... I have an assignment due tonight, here's whats needed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Create a fraction class with the following members:

numerator - a data member of type int
denominator - a data member of type int
print() - a function member that prints fractions as: numerator/denominator
set_numerator() - a function member the takes one argument of type int and sets the numerator to be that value.
set_denominator() - a function member the takes one argument of type int and sets the denominator to be that value. 
However, if the value is 0 the function should print a warning message and set the denominator to 1.
constructor - the constructor should create all new fractions with a numerator of 0 and a denominator of 1.
Write a main function to test the fraction class. The main function should do the following.

Create a fraction and show that the constructor works properly.
Create three fraction objects with the values 1/2, 3/4, and 5/0. 
Show that the fractions are printed properly - and in the case of 5/0 give the right warning message.
Create an array of 3 fraction objects: 1/5, 2/5, 3/5, and print them.


I'm way off on my code I'm sure, but I really just don't know where to go with it anymore... My instructor does a horrible job at explaining things, and doesn't give any examples. I learn best with examples.

Here's what I got so far, can anyone help me out? Give me some pointers, examples, or point me towards some examples similar to what I need to get done? I have searched, and I have read my book over and again.

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
50
51
52
53
#include <iostream>
using namespace std;


class fraction{
private:
        int numerator, denominator;
public:
        int n, d;
        fraction();
        fraction(int n, int d=1);
        void print();
        int set_numerator(int n);
        int set_denominator(int d=1);


};



fraction::fraction(){

        if (d==0){

                cout << "CAN NOT DIVIDE BY ZERO" << endl;
                denominator == 1;
                }
        else
                denominator = d;
        }

int fraction::set_numerator(int n){

                numerator = n;
}

int fraction::set_denominator(int d=1){

                denominator = d;
}


void fraction::print(){
        cout << numerator << "/" << denominator << endl;
}

int main(){

        cout << "Put in the numerator and denominator" << endl;
        cin >> n << endl;
        cin >> d << endl;

}
Last edited on
Your instructor wrote:
Create a fraction class with the following members:

numerator - a data member of type int
denominator - a data member of type int
print() - a function member that prints fractions as: numerator/denominator
set_numerator() - a function member the takes one argument of type int and sets the numerator to be that value.
set_denominator() - a function member the takes one argument of type int and sets the denominator to be that value.
However, if the value is 0 the function should print a warning message and set the denominator to 1.
constructor - the constructor should create all new fractions with a numerator of 0 and a denominator of 1.
Write a main function to test the fraction class. The main function should do the following.

Create a fraction and show that the constructor works properly.
Create three fraction objects with the values 1/2, 3/4, and 5/0.
Show that the fractions are printed properly - and in the case of 5/0 give the right warning message.
Create an array of 3 fraction objects: 1/5, 2/5, 3/5, and print them.


I will start you off with how your class and function definitions will look.

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
class fraction
{
private:
	int numerator, denominator; //Data members that store the numerator and denominator respectively.
public:
	fraction(); //A default constructor that takes no parameters.
	fraction(int n, int d); //A constructor that takes 2 int parameters

	void print(); //A public method that will print out the fraction
	void set_numerator(int num); //A public method that sets the numerator to the desired value.
	void set_denominator(int denom); //A public method that sets the denominator to the desired value.
};

fraction::fraction()
{
	//Set numerator to 0 and denominator to 1.
	//Alternatively, call set_numerator(0) and set_denominator(1)
}

fraction::fraction(int n, int d)
{
	//call set_numerator(n), call set_denominator(d)
}

void fraction::print()
{
	//print out (numerator)/(denominator)
}

void fraction::set_numerator(int num)
{
	//set numerator to num.
}

void fraction::set_denominator(int denom)
{
	//check if denom is 0.  If it is, print warning, set it to 1.  Otherwise set denominator to denom.
}


When you make all those do the correct things, you can use this class in main to accomplish the rest. You had some of the right ideas but misunderstood function arguments and data members and..yeah. This is how your skeleton should look.
Last edited on
A few problems here:

1. main does not create an object of the fraction type, so nothing much happens. You need to create an object, then call it's member functions.

2. In the default constructor, d is not defined. The spec says that the denominator and numerator should be set, so do that. The testing of the denominator should be in the set_denominator function.

3. The normal convention is for member variables to be named with a leading m_ . And capitalise the name it self as in MyVariable. I also name my classes with a leading C, as in CFraction.

Hope all goes well.

Edit: I would have main return a zero at the end, to keep the compiler happy.
Last edited on
Hi Noylekul,

Try using a default constructor like this: fraction::fraction(){numerator = 1; denominator = 1;}
Then your second constructor might look like this:
1
2
3
4
5
6
7
8
9
10
11
fraction::fraction(int n, int d){

	numerator = n; //setting initial value for numerator at n;
        if (d==0) //denominator cannot equal 0
		{		
			cout << "CAN NOT DIVIDE BY ZERO" << endl;
			exit(0);
		}
        else
            denominator = d;
        }

Good Luck! =D
This is what I got so far. On the right track?
I know its not right yet...

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
using namespace std;


class fraction
{
private:
        int numerator, denominator; //Data members that store the numerator and denominator respectively.
public:
        fraction(); //A default constructor that takes no parameters.
        fraction(int n, int d); //A constructor that takes 2 int parameters

        void print(); //A public method that will print out the fraction
        void set_numerator(int num); //A public method that sets the numerator to the desired value.
        void set_denominator(int denom); //A public method that sets the denominator to the desired value.
};

fraction::fraction()
{
        numerator = 0;
        denominator = 1; //Set numerator to 0 and denominator to 1.
        int set_numerator(0);
        int set_denominator(1);//Alternatively, call set_numerator(0) and set_denominator(1)
}

fraction::fraction(int n, int d)
{
        int set_numerator(n);
        int set_denominator(d);  //call set_numerator(n), call set_denominator(d)
}

void fraction::print()
{
        cout << numerator << "/" << denominator << endl; //print out (numerator)/(denominator)
}

void fraction::set_numerator(int num)
{
        numerator = num; //set numerator to num.
}

void fraction::set_denominator(int denom)
{
        if (denom=0){
                        cout << "YOU CAN NOT DIVIDE BY ZERO" << endl;
                        denom == 1;
                }
        else
                denominator = denom;           //check if denom is 0.  If it is, print warning, set it to 1.  Otherwise set denominator to denom.
}

int main(){

        fraction fraction;

                do{
                      
                        int choice = 2;
                        cout << "Enter numerator and denominator ? (1) \n Exit? (2) \n";
                        cin >> choice;
                        switch(choice){

                        case 1:
                                fraction.set_numerator(int);
                                fraction.set_denominator(int);
                }while (choice != 0)

                           
                        fraction.print();

                        return 0;

}
Last edited on
1
2
3
int main(){

        fraction fraction;


Does that cause any problems? It would make more sense to me, to name the object MyFraction, say. I am guessing - is it an error to name an object the same as it's class?

The call to the print function will only print the last instantiation.

HTH
When you declare a function, you need to specify what it returns. E.g. int FunctionThatReturnsInt(). However, when you call that function, you do not specify that it returns an int, because you aren't declaring the function anymore, you're calling it. So to call the function that I defined before that returns an int, I'd just do FunctionThatReturnsInt(). If I wanted to store the int value that the function returned I would do something like this:

1
2
int returnval;
returnval = FunctionThatReturnsInt();


Now returnval is equal to what the function returned. Functions of type void do not return any values.
Topic archived. No new replies allowed.