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)...
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...
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.
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...
IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write...
IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write a recursive function to produce a pattern of n lines of asterisks. The first line contains one asterisk, the next line contains two, and so on, up to the nth line, which contains n asterisks. For example, if the non-negative integer is 5, the pattern generated is: * ** *** **** ***** Prototype: void pattern(unsigned n); Write a recursive function, sum_range that finds the...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10];...
#include #include #include int main(void) { int feof(FILE *stdin); int i, num; int binary[10]; char input[10]; printf("Starting the CPSC 1011 Decimal to Binary Converter!\n"); while(1) {    i=0;    printf("\nPlease enter a positive whole number (or EOF to quit): ");    scanf("%s", input); // user inputs value as a string for separate values    if(strcmp(input,"")==0) {        printf("\n\tThank you for using the CPSC 1011 Decimal to Binary Generator.\nGoodbye!\n\n");    return(0); } num=atoi(input); if (num<=0) {    printf("\n\tSorry, that was...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
Define a class named University with data members such as campus_name, courses_offered and no_of_faculty. Include appropriate...
Define a class named University with data members such as campus_name, courses_offered and no_of_faculty. Include appropriate methods and illustrate the following object oriented concepts. i. Polymorphism. ii. Static methods
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT