Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Looking for help with a C++ object oriented coding project. The paramaters are a

ID: 3665990 • Letter: L

Question

Looking for help with a C++ object oriented coding project.

The paramaters are as follows, and will require the code provided to be completed.

Add two constructors to the class. The first accepts two integer values representing the numerator and denominator. if a single interger is passed to the constructor use it as the numerator. and use a default value of 1 for the denominator. if no values are passed to the constructor. use the default value of 0 for the numerator, and 1 for the denominator. when any FRACTION is constructed with an argument of 0 for the denominator. Force the argument to be 1

whenever a fraction object is constructed, automatically reduce the fraction to the proper format.

write a main() function that declares several fraction objects, and confirm that the class works correctly.

#include "stdafx.h"

#include <iostream>
#include <string>
using namespace std;
// declarations
class Fraction
{
private:
   int wholenum;
   int numerator;
   int denominator;
public:
   const static char seperator;
   int getwholenum();
   int getnumerator();
   int getdenominator();
   void enter_fraction_value();
   void reduce_fraction();
   void display();
};

const char Fraction::seperator = '/';

void Fraction::enter_fraction_value() {
   cout << "enter whole number ";
   cin >> wholenum;
   cout << "Enter numerator: ";
   cin >> numerator;
   while (true) {
       cout << "Enter denominator: ";
       cin >> denominator;
       if (denominator == 0) {
           cout << "Denominator can not be negative. try again!!" << endl;
       }
       else {
           break;
       }
   }
}

void Fraction::display() {
   cout << getwholenum();
   cout << getnumerator() << seperator << getdenominator();
}
void Fraction::reduce_fraction() {
   for (int i = denominator * numerator; i > 1; i--) {
       if ((denominator % i == 0) && (numerator % i == 0)) {
           denominator /= i;
           numerator /= i;
       }
   }
}

// implementation structure
int Fraction::getwholenum()
{
   return wholenum;
}
int Fraction::getnumerator()
{
   return numerator;
}
int Fraction::getdenominator()
{
   return denominator;
}
// main structure
int main()
{
   Fraction aFrac;
   aFrac.enter_fraction_value();
   aFrac.display();
   cout << " = ";
   aFrac.reduce_fraction();
   aFrac.display();
   cout << endl;
   system("pause");

}

Thank you in advance for your assistance.

Explanation / Answer

#include <iostream>

#include <string>
using namespace std;
class Fraction
{
private:
    int wholenum;
    int numerator;
    int denominator;
public:
Fraction(int,int);
Fraction(int);
Fraction();
    const static char seperator;
    int getwholenum();
    int getnumerator();
    int getdenominator();
    void enter_fraction_value();
    void reduce_fraction();
    void display();
};

Fraction::Fraction(int num ,int denom)//constructor with two arguments
{
    numerator=num;
    if(denom==0)
    denominator=1;
    else
denominator=denom;
}
Fraction::Fraction(int num )// constructor with one argument
{
    numerator=num;
denominator=1;
}
Fraction::Fraction()// constructor without arguments
{
    numerator=0;
denominator=1;
}
const char Fraction::seperator = '/';

void Fraction::enter_fraction_value() {
    cout << "enter whole number ";
    cin >> wholenum;
    cout << "Enter numerator: ";
    cin >> numerator;
    while (true) {
        cout << "Enter denominator: ";
        cin >> denominator;
        if (denominator == 0) {
            cout << "Denominator can not be negative. try again!!" << endl;
        }
        else {
            break;
        }
    }
}

void Fraction::display() {
   // cout << getwholenum()<<endl;
    cout << getnumerator() << seperator << getdenominator();
}
void Fraction::reduce_fraction() {
    for (int i = denominator * numerator; i > 1; i--) {
        if ((denominator % i == 0) && (numerator % i == 0)) {
            denominator /= i;
            numerator /= i;
        }
    }
}
int Fraction::getwholenum()
{
    return wholenum;
}
int Fraction::getnumerator()
{
    return numerator;
}
int Fraction::getdenominator()
{
    return denominator;
}
// main structure
int main()
{
    Fraction aFrac;
    aFrac.enter_fraction_value();
    aFrac.display();
    cout << " = ";
    aFrac.reduce_fraction();
    aFrac.display();
    cout << endl;
Fraction bFrac(10,0);//fraction object with two arguments
    bFrac.display();
    cout << " = ";
    bFrac.reduce_fraction();
    bFrac.display();
    cout<<endl;
    Fraction cFrac(12); //fraction object with one argument
    cFrac.display();
    cout << " = ";
    cFrac.reduce_fraction();
    cFrac.display();
    cout<<endl;
   }

output:

sh-4.3$ g++ -std=c++11 -o main *.cpp                                                                                          

sh-4.3$ main                                                                                                                  

enter whole number 11                                                                                                         

Enter numerator: 12                                                                                                           

Enter denominator: 18                                                                                                         

12/18 = 2/3                                                                                                                   

10/1 = 10/1                                                                                                                   

12/1 = 12/1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote