Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help!! Please write a class called Arithmetic to perform arithmetic calculations

ID: 3872797 • Letter: H

Question

Help!! Please write a class called Arithmetic to perform arithmetic calculations with fractions. Use integer variables to represent the private data of the class – numerator and denominator. Write a constructor function to initialize when it is declared. Provide public member functions for addition, multiplication, subtraction, and division. Please, print rational numbers in double – point format.

2. Please write a program to calculate the area of a rectangle (see previous homework, homework 1). However, you need to dynamically allocate memory for the object rectangle by creating a pointer to it. The program should check for the user input values.
3. If a program will throw an exception and the appropriate exception handler begins executing. We assume that the exception handler itself throws the same exception. Would this create an infinite recursion? Write a program to check your observation.
You should have *.h files and *.cpp files where you define classes and main code.

c++

Explanation / Answer

1)
The Answer for your first question is given below clearly as per your requirement.

code:

Arithmetic.h

#ifndef Rational_H
#define Rational_H
#include <iostream>
using namespace std;
class Arithmetic
{
public:
Arithmetic(int = 0,int =0);//default constructors are declared
void addition(Arithmetic r); //set function
void subtraction(Arithmetic r); //set functions
void multiplication(Arithmetic r); //set functions
void division(Arithmetic r); //set functions
void printRational();
void printRationalAsDouble();
void Reduction();
private:
int numerator;
int denominator;
};//end class Arithmetic

main.cpp

#include <iostream>
#include <iomanip>
#include "Arithmetic.h"//include definition of class Arithmetic from Arithmetic.h
using namespace std;
//Arithmetic constructor initializes each data member to zero
Arithmetic::Arithmetic(int num, int den)
{
numerator = num;
denominator = den;
}// end Arithmetic constructor
void Arithmetic::addition(Arithmetic r)
{
numerator = numerator + r.numerator;
denominator = denominator + r.denominator;
Reduction();
}
void Arithmetic::subtraction(Arithmetic r)
{
numerator = numerator - r.numerator;
denominator = denominator - r.denominator;
Reduction();
}
void Arithmetic::multiplication(Arithmetic r)
{
numerator = numerator * r.numerator;
denominator = denominator * r.denominator;
Reduction();
}
void Arithmetic::division(Arithmetic r)
{
numerator = numerator * r.denominator;
denominator = denominator * r.numerator;
Reduction();
}
void Arithmetic::printRational()
{
cout << numerator << "/" << denominator;
}
void Arithmetic::printRationalAsDouble()
{
cout << numerator / static_cast<float>( denominator );
}
void Arithmetic::Reduction()
{
int largest;
largest = numerator > denominator ? numerator : denominator;
int gcd = 0; // greatest common divisor
for ( int loop = 2; loop <= largest; loop++ )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
} // end if
}

Hope This Helps, if you have any doubts Please comment i will get back to you, thank you and please thumbs up

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote