Refer to this header file:
// Fraction class
// This class represents a fraction a / b
class Fraction
{
public:
// Constructors
Fraction(); // sets numerator to 1 and denominator to 1
Fraction(int num, int denom);
// Setters
void setNumerator(int num);
void setDenominator(int denom);
// getters
int getNumerator()const {return num;}
int getDenominator()const {return denom;}
double getDecimal(){return static_cast<double> num / denom;}
private:
int num, denom;
};
1.Write the code for the non-member overloaded << operator that will display all of the member variables for Fraction in the following format: (num / denom). Assume this is the code that goes in the implementation file (Fraction.cpp) and that the operator is defined as a friend function in the header file. Also assume all necessary include directives/libraries have been added.
2.
Write the code that would go in the implementation file (.cpp)
that overloads the + operator. Assume it is defined as a friend
function in the header file and that all necessary include
directives/ libraries have been added. When you add the two
fractions, you should come up with a common denominator. Example:
4/ 2 + 3/4 = 16/8 + 6/8 = 22 / 8 . This operation should return a
new fraction with the correct total.
Note: You do not have to come up
with the least common denominator
3. Write the code that would go in the implementation file (.cpp) that overloads the >> input operator, so that it reads two inputs from the input stream into the Fraction object’s num and denom. The first value should be stored in num and the second value should be stored in denom. Assume the operator was declared as a friend function in the header file and that all necessary include directives/libraries have been added.
4.
Write code for the following constructor. Assume this is the code that goes in the implementation file:
Fraction(int num, int denom); |
Make sure to add code that ensures the denominator is not zero and assume all necessary libraries have been added.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// Fraction.h
#ifndef FRACTION_H
#define FRACTION_H
class Fraction
{
public:
// Constructors
Fraction(); // sets numerator to 1 and denominator to 1
Fraction(int num, int denom);
// Setters
void setNumerator(int num);
void setDenominator(int denom);
// getters
int getNumerator()const ;
int getDenominator()const ;
double getDecimal();
friend std::istream & operator>>(std::istream &,
Fraction &f);
// Input Format: two integers with a space in it
// "a b" is correct. Not "a/b"
friend std::ostream & operator<<(std::ostream &,
const Fraction &f);
friend Fraction operator+(const Fraction &f1,const Fraction
&f2) ;
private:
int num, denom;
};
#endif
=====================================
// Fraction.cpp
#include <iostream>
using namespace std;
#include "Fraction.h"
// Constructors
Fraction::Fraction() // sets numerator to 1 and denominator to
1
{
this->num=0;
this->denom=1;
}
Fraction::Fraction(int num, int denom)
{
this->num=num;
if(denom>0)
this->denom=denom;
else
this->denom=1;
}
// Setters
void Fraction::setNumerator(int num)
{
this->num=num;
}
void Fraction::setDenominator(int denom)
{
if(denom>0)
this->denom=denom;
else
this->denom=1;
}
// getters
int Fraction::getNumerator()const {return num;}
int Fraction::getDenominator()const {return denom;}
double Fraction::getDecimal()
{
return ((double) num) / denom;
}
std::istream & operator>>(std::istream &din, Fraction
&f)
{
char ch;
cout<<"Enter Fraction (in a/b format):";
din >> f.num;
din >> ch;
din >> f.denom;
return din;
}
// Input Format: two integers with a space in it
// "a b" is correct. Not "a/b"
std::ostream & operator<<(std::ostream &dout, const
Fraction &f)
{
dout << f.num << "/" << f.denom;
return dout;
}
Fraction operator+(const Fraction &f1,const Fraction
&f2)
{
int numer=f1.num * f2.denom + f1.denom * f2.num;
int den= f1.denom *f2.denom;
Fraction res(numer,den);
return res;
}
====================================
// main,cpp
#include <iostream>
using namespace std;
#include "Fraction.h"
int main()
{
Fraction f1,f2;
cout<<"Fraction#1:"<<endl;
cin>>f1;
cout<<"Fraction#2:"<<endl;
cin>>f2;
cout<<"Fraction#1:";
cout<<f1;
cout<<endl;
cout<<"Fraction#2:";
cout<<f2;
cout<<endl;
Fraction f3=f1+f2;
cout<<"Fraction#3:";
cout<<f3;
cout<<endl;
return 0;
}
=======================================
Output:

=====================Could you plz rate me well.Thank You
Refer to this header file: // Fraction class // This class represents a fraction a /...
For the add method in Fraction class, fill the following blank. public Fraction add(Fraction f) { int num = numerator * f.getDenominator() + f.getNumerator() * denominator; int denom = denominator * f.getDenominator(); return ww new Fraction(num, denom) num/denom (double)num/(double)denom (int)num/denom
Consider this declaration of the class Fraction: class Fraction { private: int numerator, denominator; public: Fraction operator+(const Fraction & frac2); Fraction & setNumerator(int numer); Fraction & setDenominator(int denom); }; a. Why is there only one parameter, frac2, used with operator+, even though addition has two operands? b. Write the operator: Fraction Fraction::operator+ (Fraction frac2){ } c. Write setNumerator() and setDenominator() as cascading functions: Fraction & Fraction::setNumerator(int numer) { } Fraction & Fraction::setDenominator(int denom) { } d. Assign the fractions frac1...
Question 34 (2 points) For the add method in Fraction class, fill the following blank. public Fraction add(Fraction f) { int num = numerator * f.getDenominator() + f.getNumerator() * denominator; int denom = denominator * f.getDenominator: return } new Fraction(num, denom) O num/denom (double)num/(double)denom (int)num/denom Previous Page Next Page
C++ Questions. Question 1 What would the function prototype in the class declaration for the following member function look like? void Fractions::showData() { if((num < 0 && denom < 0) || denom < 0) { num = -num; denom = -denom; } cout << num << "/" << denom << endl; } Question 2 What would the function header of the following member function prototype in class Fractions be? Fractions operator+(const Fractions &f2); Question 3 What would the function prototype...
code the inverse function
5. Here is the outline of a Rational number class, i.e., a class that contains fractional numbers represented by an integer numerator and denominator). class Rational ( << operator operator public: Rational (int num, int denom) operator inverse function moiionu o loino private: int num, denom For partial credit for parts a-c, you can code the *, Integer class and << operators for the a. Code the operator; the result of multiplying two rationals is a...
C++ project we need to create a class for Book and Warehouse
using Book.h and Warehouse.h header files given. Then make a main
program using Book and Warehouse to read data from book.dat and
have functions to list and find book by isbn
Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...
C++ I need help adding to this program. Here's my assignment: (1) Add the following functions to your working mixed number class: Overload the ==, <, and > comparison operators Overload the + and * arithmetic operators Overload the insertion (<<) operator: It should display whole part, space, numerator, forward slash, denominator: 5 4/13 Overload the extraction (>>) operator: It should read integer, integer, character, integer. That is, it can assume that the numerator, forward slash, and denominator are input...
Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...
Header file for the Rational class:
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational( int = 0, int = 1 ); // default constructor
Rational addition( const Rational & ) const; // function
addition
Rational subtraction( const Rational & ) const; // function
subtraction
Rational multiplication( const Rational & ) const; // function
multi.
Rational division( const Rational & ) const; // function
division
void printRational () const; // print rational format
void printRationalAsDouble() const; // print rational as...
If you have already answered this question, please do
not repost your old solutions, you will be thumbs downed. I'm
looking for NEW solutions only!
Frac.h:
// a Fraction object holds one Fraction number, one
fraction
#ifndef FRAC_H
#define FRAC_H
#include <iostream>
using namespace std;
//Creaing a Fraction class
class Fraction {
public:
Fraction(int = 0, int = 1);
// Function Declarations which performs operations on Fraction
class
Fraction add(const Fraction &);
Fraction subtract(const Fraction& a);
Fraction multiply(const Fraction& a);...