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)
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)
![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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F0fe20004-301e-41c6-8c04-c95d974b1869%2F3f48494f-85ad-4524-b21e-0a784040746b%2F76u4z9o_processed.png&w=3840&q=75)

Step by step
Solved in 7 steps with 6 images









