Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all the instructions:
And this is the code i have so far:
#include <iostream>
using namespace std;
class Fractions {
private:
int numerator;
int denominator;
public:
void NumeratorDenominator(int n, int d);
int getNumerator() const{
return numerator;
}
int getDenominator()const {
return denominator;
}
void print()const;
};
int main() {
Fractions f1;
Fractions f2;
cout << " Testing 9/0" << endl;
f1.NumeratorDenominator(9, 0);
f1.print();
cout << "Testing 9/2" << endl;
f2.NumeratorDenominator(9, 2);
f2.print();
cout << " Testing -9/4" << endl;
f1.NumeratorDenominator(-9, 4);
f1.print();
cout << " Testing 10/2" << endl;
f1.NumeratorDenominator(10, 2);
f1.print();
cout << " Testing 3/4" << endl;
f1.NumeratorDenominator(3, 4);
f1.print();
system("pause");
}
void Fractions::NumeratorDenominator(int n, int d) {
if (d <= 0 || n < 0) {
numerator = 0;
denominator = 1;
}
else {
numerator = n;
denominator = d;
}
}
void Fractions:: print()const {
if (numerator<denominator)
cout << numerator<< "/"
<< denominator << endl;
else {
cout << numerator /
denominator << " " << numerator % denominator <<
"/" << denominator << endl;
}
}
Please find the answer below::::
main.cpp
#include <iostream>
using namespace std;
class Fractions {
private:
int numerator;
int denominator;
public:
//default constructor
Fractions(){
cout<<"Calling default
constructor"<<endl;
numerator = 0;
denominator = 1;
}
Fractions(int whole,int num,int den){
num = whole*den + num;
cout<<"Calling parametrized
constructor"<<endl;
if(num>=0){
numerator =
num;
}else{
cout<<"Numerator is less than zero!!! Setting it to
zero";
}
if(den==0){
cout<<"Denominator is zero !! Setting it to
1"<<endl;
den = 1;
}else if(den<0){
cout<<"Denominator is less than zero!!! Setting it to
1";
denominator =
1;
}else{
denominator =
den;
}
}
Fractions(int num,int den){
cout<<"Calling parametrized
constructor"<<endl;
if(num>=0){
numerator =
num;
}else{
cout<<"Numerator is less than zero!!! Setting it to
zero";
}
if(den==0){
cout<<"Denominator is zero !! Setting it to
1"<<endl;
den = 1;
}else if(den<0){
cout<<"Denominator is less than zero!!! Setting it to
1";
denominator =
1;
}else{
denominator =
den;
}
}
Fractions(const Fractions & copyMe){
numerator = copyMe.numerator;
denominator =
copyMe.denominator;
}
~Fractions(){
cout<<"Calling
destructor"<<endl;
denominator = 1;
numerator = 0;
}
void NumeratorDenominator(int n, int d);
int getNumerator() const{
return numerator;
}
int getDenominator()const {
return denominator;
}
void print()const;
};
int main() {
Fractions f1;
Fractions f2;
cout << " Testing 9/0" << endl;
f1.NumeratorDenominator(9, 0);
f1.print();
cout << "Testing 9/2" << endl;
f2.NumeratorDenominator(9, 2);
f2.print();
cout << " Testing -9/4" << endl;
f1.NumeratorDenominator(-9, 4);
f1.print();
cout << " Testing 10/2" << endl;
f1.NumeratorDenominator(10, 2);
f1.print();
cout << " Testing 3/4" << endl;
f1.NumeratorDenominator(3, 4);
f1.print();
Fractions f3(4,5,6);
cout << " Fractions f3(4,5,6)" <<
endl;
f3.print();
Fractions f4;
cout << " Fractions f4" << endl;
f4.print();
Fractions f5(12,5);
cout << " Fractions f5(12,5)" <<
endl;
f5.print();
system("pause");
}
void Fractions::NumeratorDenominator(int n, int d) {
if (d <= 0 || n < 0) {
numerator = 0;
denominator = 1;
}
else {
numerator = n;
denominator = d;
}
}
void Fractions:: print()const {
if (numerator<denominator)
cout << numerator<< "/"
<< denominator << endl;
else {
cout << numerator /
denominator << " " << numerator % denominator <<
"/" << denominator << endl;
}
}
output:
Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all...
In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator; public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...
You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...
In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...
C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...
C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Since Fractions cannot have...
C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...
13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...
Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...
Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...
Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1 5) Modify the operator>> function so that after it reads the denominator...