You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/solutions/oop/inheritance.md
+79-1Lines changed: 79 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -181,7 +181,6 @@ tags:
181
181
!include`snippetStart="// ToString method",snippetEnd="// End of ToString method"` code/projects/RoomBedBath/RoomBedBath/BathRoom.cs
182
182
```
183
183
</details>
184
-
185
184
186
185
#. Consider the diagram representing the "Article", "Book" classes and their relations.
187
186
@@ -221,3 +220,82 @@ tags:
221
220
```
222
221
</details>
223
222
223
+
#. Consider the diagram representing the "Shape", "Circle" and "Rectangle" classes, as well as their relations.
224
+
225
+
!include diag/cla/Shapes.md
226
+
227
+
#. Write the complete implementation of the `Shape` abstract class. The `ToString` method should simply return the string `"This shape is "`.
228
+
229
+
<details><summary>Solution</summary>
230
+
```{download="code/projects/Shapes.zip"}
231
+
!include code/projects/Shapes/Shapes/Shape.cs
232
+
```
233
+
</details>
234
+
235
+
#. Write an implementation for the `Radius` property of the `Circle` class such that setting the radius to a negative value would result in an `ArgumentOutOfRangeException` (that you can shorten to `AOORE`) exception being thrown. Add an attribute if needed.
#. Write the `Diameter` property for the `Circle` class, which should return $2$ times the radius. Only the `get` should be provided: briefly explain why the `set` is missing.
244
+
245
+
<details><summary>Solution</summary>
246
+
```{download="code/projects/Shapes.zip"}
247
+
!include`snippetStart="// Diameter property",snippetEnd="// Rest of the class"` code/projects/Shapes/Shapes/Circle.cs
248
+
```
249
+
</details>
250
+
251
+
#. Write an implementation for the `ToString` method of the `Rectangle` class that returns a `string` containing what was returned by the `Shape`'s `ToString` method, the width, length and area of the calling object. For example, for a `Rectangle` with width 10 and length 5, it should be of the form "This shape is a rectangle (W: 10, L: 5, Area: 50)".
#. Write the `Equals` method for the `Rectangle` class. It should return `true` if the calling object and the parameter have the same lengths and same widths, or if one can be obtained by rotating the other.
260
+
261
+
<details><summary>Solution</summary>
262
+
```{download="code/projects/Shapes.zip"}
263
+
!include`snippetStart="// Equals method",snippetEnd="// End of Equals method"` code/projects/Shapes/Shapes/Rectangle.cs
264
+
```
265
+
</details>
266
+
267
+
#. Briefly explain how `Shape` could be converted into an interface and what would be the benefit(s) and disadvantage(s) of carrying out such a modification.
268
+
269
+
<details><summary>Solution</summary>
270
+
A `Shape` interface would be
271
+
272
+
- less code,
273
+
- without the need to explicitly list the methods as `public` and `abstract`,
274
+
- not capable of specifying what the beginning of the string returned by the `ToString` method should be, or to give any non-abstract method for that matter.
275
+
276
+
Another benefit is that the `Rectangle` and `Circle` classes could realize multiple interfaces instead of only inheriting from the `Shape` class.
277
+
278
+
Its implementation would be:
279
+
280
+
```
281
+
interface Shape
282
+
{
283
+
double GetArea();
284
+
}
285
+
```
286
+
287
+
Note that our interface *cannot* have a `ToString` method, for a delicate reason that we explain now, but that was not supposed to be part of the answer. You cannot force the implementation of a `ToString` with an interface, as discussed for example [in this stack exchange post](https://stackoverflow.com/q/510341), where an alternative solution is provided (essentially: use an abstract class).
288
+
289
+
You can convince yourself that this is the case by downloading and compiling the following two files:
0 commit comments