Hello please solve the below in C++. I used the code provide by a Bartleby expert and I was marked down, the feedback was “it doesn't overload the operators as it should” Question Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks: Make an overloaded operator of the + symbol for the add method - Adds two Rational numbers. The result should be stored in reduced form. Make an overloaded operator of the - symbol for the subtract method - Subtracts two Rational numbers. Store the result in reduced form. Make an overloaded operator of the * symbol for the multiply method - Multiples two Rational numbers. Store the result in reduced form. Make an overloaded operator of the / symbol for the divide method - Divides two Rational numbers. The result should be stored in reduced form. NO CHANGES TO THIS (toRationalString - optional uses iostream from previous chapter - extra credit) - Returns a string representation of a Rational number in the form a/b, where a is the numerator and b is the denominator. NO CHANGES TO THIS | display - Display the Rational number a/b. NO CHANGES TO THIS | toDouble - Returns the Rational number as a double. (make sure the driver tests out all the overloaded operators)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Hello please solve the below in C++. I used the code provide by a Bartleby expert and I was marked down, the feedback was “it doesn't overload the operators as it should

Question

Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For example, the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator. Provide public member functions that perform each of the following tasks:

    1. Make an overloaded operator of the + symbol for the add method - Adds two Rational numbers. The result should be stored in reduced form.
    2. Make an overloaded operator of the - symbol for the subtract method - Subtracts two Rational numbers. Store the result in reduced form.
    3. Make an overloaded operator of the * symbol for the multiply method - Multiples two Rational numbers. Store the result in reduced form.
    4. Make an overloaded operator of the / symbol for the divide method - Divides two Rational numbers. The result should be stored in reduced form.
    5. NO CHANGES TO THIS (toRationalString - optional uses iostream from previous chapter - extra credit) - Returns a string representation of a Rational number in the form a/b, where a is the numerator and b is the denominator.
    6. NO CHANGES TO THIS | display - Display the Rational number a/b.
    7. NO CHANGES TO THIS | toDouble - Returns the Rational number as a double.

(make sure the driver tests out all the overloaded operators)

I
// Fig. 10.11: Array.cpp
2
// Array class member- and friend-function definitions.
3 #include <iostream>
4 #include <iomanip>
5 #include <stdexcept>
6
7
8
9
10
// default constructor for class Array (default size 10)
|| Array::Array(int arraySize)
12
: size{(arraySize > 0 ? static_cast<size_t>(arraySize) :
13
throw invalid_argument{"Array size must be greater than 0"})},
ptr{new int[size]{}} { /* empty body */ }
14
15
16
17
18
19
20
21
22
23 }
24
25
26
27
28
31
32
#include "Array.h" // Array class definition
using namespace std;
33
34
// copy constructor for class Array;
// must receive a reference to an Array
Array::Array (const Array& arrayToCopy)
41
42
43
44
45
46
47
48
49
50
51
52
53
29
30 // return number of elements of Array
size_t Array::getSize() const {
return size; // number of elements in Array
}
// destructor for class Array
Array::~Array () {
delete[] ptr; // release pointer-based array space
}
35 // overloaded assignment operator;
36 // const return avoids: (al = a2) = a3
37
38
39
40
: size{arrayToCopy.size}, ptr{new int[size]} {
for (size_t i{0}; i < size; ++i) {
ptr[i] = arrayToCopy.ptr[i]; // copy into object
}
}
const Array& Array::operator=(const Array& right) {
if (&right != this) { // avoid self-assignment
// for Arrays of different sizes, deallocate original
// left-side Array, then allocate new left-side Array
}
75
76
77
78
79
80 }
if (size != right.size) {
}
for (size_t i{0}; i < size; ++i) {
ptr[i]=right.ptr[i]; // copy array into object
}
}
return *this; // enables x = y = z, for example
54
55
// determine if two Arrays are equal and
56 // return true, otherwise return false
57 bool Array::operator==(const Array& right) const {
if (size !
58
}
for (size_t i{0}; i < size; ++i) {
if (ptr[i] != right.ptr[i]) {
return false; // Array contents are not equal
delete[] ptr; // release space
size= right.size; // resize this object
ptr = new int[size]; // create space for Array copy
59
60
61
62
63
64
65
66
67
68
69 }
70
71 // overloaded subscript operator for non-const Arrays;
72 // reference return creates a modifiable 1value
right.size) {
return false; // arrays of different number of elements
}
return true; // Arrays are equal
73 int& Array::operator [] (int subscript) {
74
// check for subscript out-of-range error
if (subscript < 0 || subscript>= size) {
throw out_of_range{"Subscript out of range"};
}
return ptr [subscript]; // reference return
}
81
82 // overloaded subscript operator for const Arrays
83 // const reference return creates an rvalue
84 int Array::operator [] (int subscript) const {
85
// check for subscript out-of-range error
if (subscript < 0 || subscript >= size) {
86
87
throw out_of_range{"Subscript out of range"};
88
}
89
90
91
}
92
93 // overloaded input operator for class Array;
94 // inputs values for entire Array
95 istream& operator>>(istream& input, Array& a) {
96
for (size_t i{0}; i < a.size; ++i) {
97
input >> a.ptr[i];
98
99
100
101 }
102
103 // overloaded output operator for class Array
104 ostream& operator<<(ostream& output, const Array& a) {
105
106
// output private ptr-based array
for (size_t i{0}; i < a.size; ++i) {
output <<
107
a.ptr[i]<<
108
}
109
110
III
112 }
return ptr [subscript]; // returns copy of this element
2
return input; // enables cin >> x >> y;
output << endl;
return output; // enables cout << x << y;
3
Transcribed Image Text:I // Fig. 10.11: Array.cpp 2 // Array class member- and friend-function definitions. 3 #include <iostream> 4 #include <iomanip> 5 #include <stdexcept> 6 7 8 9 10 // default constructor for class Array (default size 10) || Array::Array(int arraySize) 12 : size{(arraySize > 0 ? static_cast<size_t>(arraySize) : 13 throw invalid_argument{"Array size must be greater than 0"})}, ptr{new int[size]{}} { /* empty body */ } 14 15 16 17 18 19 20 21 22 23 } 24 25 26 27 28 31 32 #include "Array.h" // Array class definition using namespace std; 33 34 // copy constructor for class Array; // must receive a reference to an Array Array::Array (const Array& arrayToCopy) 41 42 43 44 45 46 47 48 49 50 51 52 53 29 30 // return number of elements of Array size_t Array::getSize() const { return size; // number of elements in Array } // destructor for class Array Array::~Array () { delete[] ptr; // release pointer-based array space } 35 // overloaded assignment operator; 36 // const return avoids: (al = a2) = a3 37 38 39 40 : size{arrayToCopy.size}, ptr{new int[size]} { for (size_t i{0}; i < size; ++i) { ptr[i] = arrayToCopy.ptr[i]; // copy into object } } const Array& Array::operator=(const Array& right) { if (&right != this) { // avoid self-assignment // for Arrays of different sizes, deallocate original // left-side Array, then allocate new left-side Array } 75 76 77 78 79 80 } if (size != right.size) { } for (size_t i{0}; i < size; ++i) { ptr[i]=right.ptr[i]; // copy array into object } } return *this; // enables x = y = z, for example 54 55 // determine if two Arrays are equal and 56 // return true, otherwise return false 57 bool Array::operator==(const Array& right) const { if (size ! 58 } for (size_t i{0}; i < size; ++i) { if (ptr[i] != right.ptr[i]) { return false; // Array contents are not equal delete[] ptr; // release space size= right.size; // resize this object ptr = new int[size]; // create space for Array copy 59 60 61 62 63 64 65 66 67 68 69 } 70 71 // overloaded subscript operator for non-const Arrays; 72 // reference return creates a modifiable 1value right.size) { return false; // arrays of different number of elements } return true; // Arrays are equal 73 int& Array::operator [] (int subscript) { 74 // check for subscript out-of-range error if (subscript < 0 || subscript>= size) { throw out_of_range{"Subscript out of range"}; } return ptr [subscript]; // reference return } 81 82 // overloaded subscript operator for const Arrays 83 // const reference return creates an rvalue 84 int Array::operator [] (int subscript) const { 85 // check for subscript out-of-range error if (subscript < 0 || subscript >= size) { 86 87 throw out_of_range{"Subscript out of range"}; 88 } 89 90 91 } 92 93 // overloaded input operator for class Array; 94 // inputs values for entire Array 95 istream& operator>>(istream& input, Array& a) { 96 for (size_t i{0}; i < a.size; ++i) { 97 input >> a.ptr[i]; 98 99 100 101 } 102 103 // overloaded output operator for class Array 104 ostream& operator<<(ostream& output, const Array& a) { 105 106 // output private ptr-based array for (size_t i{0}; i < a.size; ++i) { output << 107 a.ptr[i]<< 108 } 109 110 III 112 } return ptr [subscript]; // returns copy of this element 2 return input; // enables cin >> x >> y; output << endl; return output; // enables cout << x << y; 3
Expert Solution
steps

Step by step

Solved in 7 steps with 6 images

Blurred answer
Knowledge Booster
ADT and Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education