Hi my professor for an entry level C++ class recently assigned us a homework problem, but hasn't quite taught us how to approach this problem.

The problem asks us to create a working C++ program that has a class called `Fractions` that has two int data members called numerator and denominator. The class's default constructor should provide both data members with default values of 1 if no explicit user initialization is provided. The constructor must also prohibit a 0 denominator value. We also have to include member functions for displaying an object's data and mathematical functions capable of adding, subtracting, multiplying and dividing two `Fractions` objects. Then we have to write another member function that reduces the fractions to their lowest common terms (ie. 12 / 15 becomes 3 / 5 )

I'm having several problems with this. How do I provide user inputs into class data members? I'm currently just doing it in the main function. How do I write a member function that takes two class objects? Is this even possible?

Here's what I have so far. I'm not quite sure how to use the sum, diff, prod, and div functions with the Fractions class objects.

``````#include <iostream>
using namespace std;

class Fractions{
private:
int num;
int denom;
public:
Fractions(int = 1, int = 1);
int setNum(int);
int setDenom(int);
void setFraction(int, int);
void displayData();
void sum(int, int, int, int);
void diff(int, int, int, int);
void prod(int, int, int, int);
void div(int, int, int, int);
void gcd(int, int);
};

Fractions::Fractions(int a, int b){
num = a;
denom = b;
}

void Fractions::setFractions(int a, int b){
num = a;
denom = b;
}

int Fractions::setNum(int a){
num = a;
return num;
}

int Fractions::setDenom(int b){
denom = b;
return denom;
}

void Fractions::displayData(){
cout << "Fraction: " << num << "/" << denom << endl;
}

void Fractions::sum(int a, int b, int c, int d){
double sum;
sum = ((a * d) + (c * b)) / (b * d);
cout << "Sum: " << sum << endl;
}

void Fractions::diff(int a, int b, int c, int d){
double diff;
diff = ((a * d) - (c * b)) / (b * d);
cout << "Difference: " << diff << endl;
}

void Fractions::prod(int a, int b, int c, int d){
double prod;
prod = (a * c) / (b * d)
cout << "Product: " << prod << endl;
}

void Fractions::div(int a, int b, int c, int d){
double div;
div = (a * d) / (b * c)
cout << "Division: " << div << endl;
}

int main(){
Fractions a, b;
int num1, num2, denom1, denom2;

//user inputs num and denom values for two fractions
//do-while statement checks if user entered a 0 for denom
cout << "Please enter a numerator: " << endl;
cin >> num1
cout << "Please enter a denominator: " << endl;
cin >> denom1
do{
cout << "You cannot have 0 in the denominator." << endl;
cout << "Please enter a denominator: " << endl;
cin >> denom1
}
while(denom1 = 0);
cout << "Please enter another numerator: " << endl;
cin >> num2
cout << "Please enter another denominator: " << endl;
cin >> denom2
do{
cout << "You cannot have 0 in the denominator." << endl;
cout << "Please enter a denominator: " << endl;
cin >> denom2
}
while(denom2 = 0);

a.setFractions(num1, denom1);
b.setFractions(num2, denom2);
a.displayData();
b.displayData();

}``````

Any input would be great. Thanks a lot!

2
Contributors
1
3
Views
8 Years
Discussion Span
Last Post by caut_baia
``````#include <iostream>
#include <iomanip>

using namespace std;

template <typename C> class fraction
{
protected:
C numerator;
C denominator;

public:
fraction() {}
~fraction() {}

explicit fraction (C num,C denom) : numerator(num),denominator(denom)
{
frac(numerator,denominator);
}
C frac(C,C);
};

template <typename C> C fraction<C> :: frac(C n,C d)
{
return n/d;
}

int main ()
{
fraction<float> a;
fraction<float> b;
float x,y,z,q;

while (cin >> x >> y >> z >> q)
cout << setprecision(3) << a.frac(x,y) + b.frac(z,q);
}
``````

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.