I am currently working on an assignment using classes with fractions. I am almost finished but the last part of the assignment is to store 3 fractions in an array and print them. Arrays confuse me to no end. Could someone point me in the right direction? Here is my code so far:
#include <iostream>
#include <cmath>
#include <string>
usingnamespace 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.
};
int main(){
fraction fraction1;
int num1;
int denom1;
do{
cout << "Enter your Numerator and Denominator" << endl;
cin >> num1;
cin >> denom1;
fraction1.set_numerator(num1);
fraction1.set_denominator(denom1);
fraction1.print();
}while(denom1 != 0);
return 0;
}
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)
{
denominator = denom;
if(denom == 0){
cout << "You cannot divide by 0. Thou art not Chuck Norris." << endl;
}
}