Highschool C++ Homework Assignment

This chapter uses the class rectangleType to illustrate how to overload the operators +, *, ==, !=, >>, and <<. In this exercise, first redefine the class rectangleType by declaring the instance variables as protected and then overload additional operators as defined in parts a to c.

Part a: Overload the pre- and post-increment and decrement operators to increment and decrement, respectively, the length and width of a rectangle by one unit. (Note that after decrementing the length and width, they must be positive.)
Part b: Overload the binary operator - to subtract the dimensions of one rectangle from the corresponding dimensions of another rectangle. If the resulting dimensions are not positive, output an appropriate message and do not perform the operation.
Part c: The operators == and != are overloaded by considering the lengths and widths of rectangles. Redefine the functions to overload the relational operator by considering the areas of rectangles as follows: Two rectangles are the same, if they have the same area; otherwise, the rectangles are not the same. Similarly, rectangle yard1 is greater than rectangle yard2 if the area of yard1 is greater than the area of yard2. Overload the remaining relational operators using similar definitions.
Part d: Write the definitions of the functions to overload the operators defined in parts a to c.
Part e: Use the test program main.cpp that tests various operations on the class rectangleType.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  //In my main.cpp file
#include <iostream>
#include "rectangleType.h"

using namespace std;


// part e
int main()
{
    rectangleType rectangle1(10, 5);
    rectangleType rectangle2(8, 7);
    rectangleType rectangle3;
    rectangleType rectangle4;

    cout << "rectangle1: " << rectangle1 << endl;

    cout << "rectangle2: " << rectangle2  << endl;

    rectangle3 = rectangle1 + rectangle2;
    
    cout << "rectangle3: " << rectangle3 << endl;

    rectangle4 = rectangle1 * rectangle2;
    
    cout << "rectangle4: " << rectangle4 << endl;

    if (rectangle1 > rectangle2)
        cout << "Area of rectangle1 is greater than the area "
             << "of rectangle2 ." << endl;
    else
        cout << "Area of rectangle1 is less than or equal to the area "
             << "of rectangle2 ." << endl;     

	rectangle1++;

	cout << "After increment the length and width of "
		 << "rectangle1 by one unit, \nrectangle1: " 
         << rectangle1 << endl;

	rectangle4 = ++rectangle3;

	cout << "New dimension of rectangle3: " << rectangle3 << endl;
	cout << "New dimension of rectangle4: " << rectangle4 << endl;

    return 0;
}

//In my rectangleType.cpp file
#include <iostream>
#include <cassert>
 
#include "rectangleType.h"
 
using namespace std;

void rectangleType::setDimension(double l, double w)
{
    if (l >= 0)
        length = l;
    else
        length = 0;

    if (w >= 0)
        width = w;
    else
        width = 0;
}

double rectangleType::getLength() const
{
    return length;
}

double rectangleType::getWidth()const
{
    return width;
}

double rectangleType::area() const
{
    return length * width;
}

double rectangleType::perimeter() const
{
    return 2 * (length + width);
}

rectangleType::rectangleType(double l, double w)
{
    setDimension(l, w);
}

rectangleType::rectangleType()
{
    length = 0;
    width = 0;
}

rectangleType rectangleType::operator++()
{
    // part d
}

rectangleType rectangleType::operator++(int u)
{
    // part d
}

rectangleType rectangleType::operator--()
{
    // part d
}

rectangleType rectangleType::operator--(int u)
{
    // part d
}

rectangleType rectangleType::operator+ 
                          (const rectangleType& rectangle) const
{
    // part d
}

rectangleType rectangleType::operator- 
                          (const rectangleType& rectangle) const
{
    // part d
}

rectangleType rectangleType::operator*(const rectangleType& rectangle) const
{
    // part d
}

bool rectangleType::operator== 
                      (const rectangleType& rectangle) const
{
    // part d
}

bool rectangleType::operator!= 
                       (const rectangleType& rectangle) const
{
    // part d
}

bool rectangleType::operator<= 
                       (const rectangleType& rectangle) const
{
    // part d
}

bool rectangleType::operator< 
                       (const rectangleType& rectangle) const
{
    // part d
}

bool rectangleType::operator>= 
                       (const rectangleType& rectangle) const
{
    // part d
}

bool rectangleType::operator>
                       (const rectangleType& rectangle) const
{
    // part d
}

ostream& operator<<(ostream& osObject, 
                      const rectangleType& rectangle)
{
    osObject << "Length = "  << rectangle.length 
             << "; Width = " << rectangle.width;

    return osObject;
}

istream& operator>>(istream& isObject, rectangleType& rectangle)
{
    isObject >> rectangle.length >> rectangle.width;

    return isObject;
}

//In my rectangleType.h file 
#ifndef H_rectangleType
#define H_rectangleType
  
#include <iostream>
using namespace std;

class rectangleType
{
        //Overload the stream insertion and extraction operators
    friend ostream& operator<<(ostream&, const rectangleType &);
    friend istream& operator>>(istream&, rectangleType &);

public:
    void setDimension(double l, double w);
    double getLength() const;
    double getWidth() const;
    double area() const;
    double perimeter() const;

    //Overload the arithmetic operators (part b)
    rectangleType operator + (const rectangleType &) const;
    rectangleType operator - (const rectangleType &) const;
    rectangleType operator * (const rectangleType&) const;

    //Overload the increment and decrement operators (part a)
    rectangleType operator ++ ();          //pre-increment
    rectangleType operator ++ (int);       //post-increment
    rectangleType operator -- ();          //pre-decrement
    rectangleType operator -- (int);       //post-decrement

    //Overload the relational operators (part c)
    bool operator == (const rectangleType&) const;
    bool operator != (const rectangleType&) const;
    bool operator <= (const rectangleType&) const;
    bool operator < (const rectangleType&) const;
    bool operator >= (const rectangleType&) const;
    bool operator > (const rectangleType&) const;

        //constructors
    rectangleType();
    rectangleType(double l, double w);

protected:
    double length;
    double width;
};

#endif 
And?

Do you have a question?

Is there something you want help with?
@MikeyBoy

Yes, those are my instructions, and the code I have started with, and my question is this: How would I go about writing this? And of course, I am NOT asking you to write this entire program for me, but more so guide me in the right direction please and thank you.
Topic archived. No new replies allowed.