In: Computer Science
Modify the Fraction class created previously as follows. Read the entire description (Code at the bottom also using C++ on visual studios)
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Fraction {
private:
int numer;
int denomin;
public:
int GetNumerator();
int GetDenominator();
Fraction();
Fraction(int, int);
void SetNumerator(int);
void SetDenominator(int);
void SetFraction(int, int);
double GetFraction();
string ToString();
};
Fraction::Fraction() {
numer = 0;
denomin = 1;
}
Fraction::Fraction(int numer, int denomin) {
SetFraction(numer, denomin);
}
void Fraction::SetNumerator(int numer) {
SetFraction(numer, this->denomin);
}
void Fraction::SetDenominator(int denomin) {
SetFraction(this->numer, denomin);
}
int Fraction::GetNumerator() {
return numer;
}
int Fraction::GetDenominator() {
return denomin;
}
void Fraction::SetFraction(int numer, int denomin) {
if (numer < 0 || denomin < 1) {
this->numer = 0;
this->denomin = 1;
}
else {
this->numer = numer;
this->denomin = denomin;
}
}
double Fraction::GetFraction() {
return (double)numer / (double)denomin;
}
string Fraction::ToString() {
return to_string(numer) + "/" + to_string(denomin);
}
int main() {
Fraction f(1, 2);
cout << "Fraction: " << f.ToString() <<
"\n";
cout << "Numerator: " << f.GetNumerator() <<
"\n";
cout << "Denominator: " << f.GetDenominator() <<
"\n";
cout << "Fraction value: " << f.GetFraction() <<
"\n";
cout << "\n";
f.SetNumerator(5);
f.SetDenominator(3);
cout << "Fraction: " << f.ToString() <<
"\n";
cout << "Numerator: " << f.GetNumerator() <<
"\n";
cout << "Denominator: " << f.GetDenominator() <<
"\n";
cout << "Fraction value: " << f.GetFraction() <<
"\n";
cout << "\n";
f.SetFraction(4, 3);
cout << "Fraction: " << f.ToString() << "\n";
cout << "\n";
f.SetNumerator(4);
f.SetDenominator(3);
cout << "Numerator: " << f.GetNumerator() <<
"\n";
cout << "Denominator: " << f.GetDenominator() <<
"\n";
cout << "Fraction value: " << f.GetFraction() <<
"\n";
return 0;
}
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
class Fraction {
private:
int numer;
int denomin;
public:
int GetNumerator();
int GetDenominator();
~Fraction();
Fraction();
Fraction(int);
Fraction(int, int);
Fraction(int, int, int);
// Copy constructor
Fraction(const Fraction &f) {
cout<<" copy constructor is calling\n";
numer = f.numer; denomin = f.denomin;
}
void SetNumerator(int);
void SetDenominator(int);
void SetFraction(int);
void SetFraction(int, int);
void SetFraction(int, int, int);
double GetFraction();
void PrintFraction();
string ToString();
};
Fraction::Fraction() {
numer = 0;
denomin = 1;
}
Fraction::Fraction(int number) {
cout<<"constructor is calling\n";
SetFraction(number);
}
Fraction::Fraction(int numer, int denomin) {
cout<<"constructor is calling\n";
SetFraction(numer, denomin);
}
Fraction::Fraction(int whole, int numer, int denomin) {
cout<<"constructor is calling\n";
SetFraction(whole, numer, denomin);
}
Fraction::~Fraction() {
cout<<"disctructor is calling\n";
numer = 0;
denomin = 1;
cout << "Pause! Press the Enter key to continue.\n";
system("pause");
//cin.get();
cout << "Resuming...\n";
}
void Fraction::SetNumerator(int numer) {
SetFraction(numer, this->denomin);
}
void Fraction::SetDenominator(int denomin) {
SetFraction(this->numer, denomin);
}
int Fraction::GetNumerator() {
return numer;
}
int Fraction::GetDenominator() {
return denomin;
}
void Fraction::SetFraction(int number) {
if(number < 0) {
cout<<"Only positive values allowed.\n";
this->numer = 0;
this->denomin = 1;
}
else {
this->numer = number;
this->denomin = number;
}
}
void Fraction::SetFraction(int numer, int denomin) {
if(denomin==0) {
cout<<"Denominator can be Zero in any fraction because
division by zero is undefined.\n";
denomin = 1;
}
if(numer < 0 || denomin < 1) {
cout<<"Only positive values allowed.\n";
this->numer = 0;
this->denomin = 1;
}
else {
this->numer = numer;
this->denomin = denomin;
}
}
void Fraction::SetFraction(int whole, int numer, int denomin) {
if(denomin==0) {
cout<<"Denominator can be Zero in any fraction because
division by zero is undefined.\n";
denomin = 1;
}
if(whole < 0 || numer < 0 || denomin < 1) {
cout<<"Only positive values allowed.\n";
this->numer = 0;
this->denomin = 1;
}else {
this->numer = whole*denomin + numer;
this->denomin = denomin;
}
}
double Fraction::GetFraction() {
return (double)numer / (double)denomin;
}
string Fraction::ToString() {
return to_string(numer) + "/" + to_string(denomin);
}
void Fraction::PrintFraction(){
cout<< "Fraction: " << ToString() <<"\n";
}
int main() {
Fraction f(1);
cout << "Fraction: " << f.ToString() <<
"\n";
cout << "Numerator: " << f.GetNumerator() <<
"\n";
cout << "Denominator: " << f.GetDenominator() <<
"\n";
cout << "Fraction value: " << f.GetFraction() <<
"\n";
cout << "\n";
Fraction f2(2,3,4);
cout << "Fraction: " << f2.ToString() <<
"\n";
cout << "Numerator: " << f2.GetNumerator() <<
"\n";
cout << "Denominator: " << f2.GetDenominator() <<
"\n";
cout << "Fraction value: " << f2.GetFraction() <<
"\n";
cout << "\n";
Fraction f3(f2);
f3.PrintFraction();
cout << "Numerator: " << f3.GetNumerator() <<
"\n";
cout << "Denominator: " << f3.GetDenominator() <<
"\n";
cout << "Fraction value: " << f3.GetFraction() <<
"\n";
cout << "\n";
f.SetNumerator(5);
f.SetDenominator(3);
cout << "Fraction: " << f.ToString() <<
"\n";
cout << "Numerator: " << f.GetNumerator() <<
"\n";
cout << "Denominator: " << f.GetDenominator() <<
"\n";
cout << "Fraction value: " << f.GetFraction() <<
"\n";
cout << "\n";
f.SetFraction(4, 3);
cout << "Fraction: " << f.ToString() << "\n";
cout << "\n";
f.SetNumerator(4);
f.SetDenominator(3);
cout << "Numerator: " << f.GetNumerator() <<
"\n";
cout << "Denominator: " << f.GetDenominator() <<
"\n";
cout << "Fraction value: " << f.GetFraction() <<
"\n";
return 0;
return 0;
}
Note: In linux we need use 'cin.get()' instead of system("pause") because it's works only in windows.