Question

In: Computer Science

C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include...

C++ file=.class definitions.h

Define a Fraction class with num and den as its private data. Include a constructor to initialize the fraction to 0/1, a copy constructor, a destructor, and overloading functions to overload the assignment operator =, the comparison operators <, >, ==, !=, arithmetic operators +, +=, -, -=, *, *=, /, /=, as well as friend functions (non-member) to overload << and >> to output and input a fraction (see book example). Also, include a private member function called reduce() that gets called after arithmetic operations to reduce the frraction. +, -, *, / must return the result of the operation; e.g.: the sum or difference of the two fractions. +=, -=, *= and /= must assign the result of the operation to the object that's invoking the operation; i.e.: frac1 += frac2 must modify frac1 to make it equal to the sum of the two fractions, but frac1 + frac2 must simply return the sum.

If n1/d1 and n2/d2 are two fractions, their sum fraction's numertor is: n1 * d2 + n2 * d1 and its denominator is d1 * d2. To compare the two fractions, you can compare n1 * d2 with n2 * d1.

Solutions

Expert Solution

ANSWER:

#include <iostream>
using namespace std;

class Fractions { // The class
private: // Access specifier
int num; // Attribute (int variable)
int den;
Fractions reduce(int r)
{
Fractions t;
t.num=num-r*den;
t.den=den;
cout<<"reduced fraction is: "<<t.num<<"/"<<t.den<<"\n";
cout<<"___________________________________\n";
return t;

}
public:
Fractions()
{
num=0;
den=1;
}
Fractions(int nn,int dd)
{
num=nn;
den=dd;
  
}
~Fractions()
{

}
Fractions operator=(Fractions f)
{
//cout<<"Assignment operator invoked\n";
// cout<<"_____________________________\n";
num=f.num;
den=f.den;
  
return Fractions(num,den);
}
Fractions (const Fractions& f)
{
// cout<<"Copy constructor invoked\n";
// cout<<"_________________________\n";
num=f.num;
den=f.den;
  
}
Fractions decrement(int i)
{
reduce(i);
}
Fractions operator+= (Fractions f)
{
num=num*f.den+f.num*den;
den=den*f.den;

cout<<"new value of the fraction after += operation is :"<<num<<"/"<<den<<"\n";

}
Fractions operator-=(Fractions f)
{
num=num*f.den-f.num*den;
den=den*f.den;
cout<<"new value of the fraction after -= operation is :"<<num<<"/"<<den<<"\n";
}
Fractions operator*=(Fractions f)
{
num=num*f.num;
den=den*f.den;
cout<<"new value of the fraction after *= operation is :"<<num<<"/"<<den<<"\n";
}
Fractions operator/=(Fractions f)
{
num=num*f.den;
den=den*f.num;
cout<<"new value of the fraction after /= operation is :"<<num<<"/"<<den<<"\n";
}
Fractions operator +(Fractions F2)
{
Fractions t;
t.num=num*F2.den+F2.num*den;
t.den=den*F2.den;
return t;
}
Fractions operator-(Fractions F2)
{
Fractions t;
t.num=num*F2.den-F2.num*den;
t.den=den*F2.den;
return t;
}
Fractions operator*(Fractions F2)
{
Fractions t;
t.num=num*F2.num;
t.den=den*F2.den;
return t;
}
Fractions operator/(Fractions F2)
{
Fractions t;
t.num=num*F2.den;
t.den=den*F2.num;
return t;
}
bool operator==(Fractions F2)
{
bool x;
if(num*F2.den==F2.num*den)
x=1;
else
x=0;
return x;
}
bool operator<(Fractions F2)
{

bool x;
if(num*F2.den<F2.num*den)
x=1;
else
x=0;
return x;
}
bool operator>(Fractions F2)
{

bool x;
if(num*F2.den>F2.num*den)
x=1;
else
x=0;
return x;
}
int operator!=(Fractions F2)
{
bool x;
if(num*F2.den!=F2.num*den)
x=1;
else
x=0;
return x;
}

friend ostream& operator<<(ostream& s,Fractions& f);
friend istream& operator>>(istream& s,Fractions& f);

};
ostream& operator<<(ostream& s,Fractions& f)
{
s<<"("<<f.num<<"/"<<f.den<<")";
return s;
}
istream& operator>>(istream& s,Fractions& f)
{
s>>f.num>>f.den;
return s;
}
  
int main() {

  
Fractions f1(1,5); // Create an object of Fractions
  
Fractions f2,f4;//two othe objects without innitialising values
f4=f2=f1;//initialising objects using assignment operator

Fractions f3=f1;//Initialising using copy constructor

f1.decrement(3);

f4+=f2;
f4-=f1;
f2*=f3;
f1/=f3;
Fractions f5;
f5=f1+f2;
f5=f1-f2;
f5=f1*f2;
f5=f1/f2;
Fractions f6;
if(f1!=f4)f6=f1;
else f6=f4;
Fractions f7;
if(f1<f4)f7=f1;
else f7=f4;

cout<<"Friend function checking:f1="<<f1<< "\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f2="<<f2<< "\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f3="<<f3<< "\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f4="<<f4<< "\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f5="<<f5<< "\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f6="<<f6<< "\n";
cout<<"---------------------------\n";
cout<<"Friend function checking:f7="<<f7<< "\n";
cout<<"---------------------------\n";
   return 0;
}


Related Solutions

construct c program flow chart #include <stdio.h> #include <math.h> #include <string.h> #define num 6 #define b...
construct c program flow chart #include <stdio.h> #include <math.h> #include <string.h> #define num 6 #define b 6 #define t 6 double bmical(double w, double h){ double o; double bmi; o = pow(h,2); bmi = w/o; return bmi; } double maxheartrate(int num1, int age){ double mhr; mhr = num1 - age; return mhr; } double heartratereserve(double mhr, double rhr){ double hrr; hrr = mhr - rhr; return hrr; } double minrange(int hrr, int rhr){ double mirt; mirt = (hrr * 0.7)...
Declare and define a class for a fraction number. A fraction in mathematics is defined as...
Declare and define a class for a fraction number. A fraction in mathematics is defined as a/b, where a and b are integers and called numerator and denominator. Requirements Task1    Define a fraction class that has the following member functions: constructor that initializes the fraction by default arguments. set function that sets the numerator of the fraction. set function that sets the denominator of the fraction. get function that returns the numerator of the fraction. get function that returns...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
1. Define a class counterType to implement a counter. Your class must have a private data...
1. Define a class counterType to implement a counter. Your class must have a private data member counter of type int and functions to set counter to the value specified by the user, initialize counter to 0, retrieve the value of counter, and increment and decrement counter by one. The value of counter must be nonnegative. 2. Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design a class bookType that...
(C++ ONLY) You will define your own data type. It will be a struct called Fraction....
(C++ ONLY) You will define your own data type. It will be a struct called Fraction. The struct will have 2 integer fields: numerator and denominator. You will write a function called reduce that takes a Fraction as a parameter and returns that Fraction in its reduced form. For example, if the fraction 2/4 is passed to the function, the fraction 1/2 will be returned. Consider any fraction with a denominator of 1 to be in reduced form. You will...
Write a program in which define a templated class mySort with private data members as a...
Write a program in which define a templated class mySort with private data members as a counter and an array (and anything else if required). Public member functions should include constructor(s), sortAlgorithm() and mySwap() functions (add more functions if you need). Main sorting logic resides in sortAlgorithm() and mySwap() function should be called inside it. Test your program inside main with integer, float and character datatypes.
C++ // Rectangle.cpp is the Rectangle class function implementation file. #include "Rectangle.h" /******************************************************************* * Rectangle::setLength *...
C++ // Rectangle.cpp is the Rectangle class function implementation file. #include "Rectangle.h" /******************************************************************* * Rectangle::setLength * * If the argument passed to the setLength function is zero or * * greater, it is copied into the member variable length, and true * * is returned. If the argument is negative, the value of length * * remains unchanged and false is returned. * *******************************************************************/ bool Rectangle::setLength(double len) { bool validData = true; if (len >= 0) // If the len...
Given the main method of a driver class, write a Fraction class. Include the following instance...
Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT