I'm stuck on one problem of a group that I've been working on and my deadline is rapidly approaching. It's supposed to draw from work that we've done in our previous assignments but I just can't figure out how to even get going.
Here is the Q;
Define a class called Fraction that has 2 private members that represent the numerator and denominator respectively. They will be represented as integers. Include a constructor and a public void function called print that prints out the fraction. (For example, if the numerator is 3 and the denominator is 5, print provides as output 3/5.) Include the class in a program that prints out the fraction 3/5.
and embarrassingly this is all I have(and its probably not right)
Thanks in advance!!
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
usingnamespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
void print(int x, int y) : numerator(x), denominator(y) {}
};
You don't have a constructor. You should include a 2-parameter/default constructor that either sets the private data to 0 or to what was passed in. So it will look something like this:
Fraction(int num = 0, int den = 0) : numerator(num), denominator(den){}
The print function is incorrect. An initializer list only works with constructors, not with normal functions. Your print function should simply cout the numerator followed by a '/' character followed by denominator. It would be best if print did not take any parameters.