Remov
Remov
#define BONUS
2 Exercise 2 — Building
The purpose of this exercise is to write a program able to construct "buildings"
based on "building blocks" using C++ operators.
This exercise is simpler than it may first seem. Don’t go too far but follow the
proposed (section 2.2).
2.1 Description
Download the source code available at the course webpage and complete it accor-
ding to the instructions below.
3
WARNING : you should modify neither the beginning nor the end of the provided
file. It’s thus mandatory to proceed as follows :
1. save the downloaded file as construction.cc or construction.cpp ;
2. write your code between these two provided comments :
/*******************************************
* Complétez le programme à partir d’ici.
*******************************************/
/*******************************************
* Ne rien modifier après cette ligne.
*******************************************/
3. save and test your program to be sure that it works properly ; try for ins-
tance the values used in the example given below ;
4. upload the modified file (still named construction.cc or construction.cpp)
in “My submission” then “Create submission”.
4
An example of output for the code is provided further below.
The end of the definition of class Brique and the definition of class Construction,
described below, are missing and you are asked to provide them.
The class Brique represents the “building blocks” of our buildings, as shown in
the following example :
class Construction
5
{
friend class Grader;
The order to internally represent the 3 dimensions (from what direction you look
at the building) is shown here :
briques «vides»
[i]
1 [j]
2
0 1
0
0 1 2 3 [k]
This means that the first index (i above) represents the heigth, the second (j) the
depth and the third (k) the width.
Afterwards, add to the class Construction :
— a constructor taking as parameter a Brique and creating the contenu
as an 1x1x1 array containing only this brick ;
— a public method with the prototype
ostream& afficher(ostream& sortie) const ;
displaying the building’s content layer by layer as shown further below in
the output example (a message “Couche numéro :” (English : Layer
number) must be displayed at the beginning of each layer of the construc-
tion). If the construction is empty, nothing is displayed.
6
2.1.3 Operators for the class Construction
7
^=
^=
^=
^=
^=
^=
— a ^ b; creates a new Construction which is the result of Construction
b placed on top of Construction a ;
3. the internal operator operator-= and the external operator operator-
which adds a Construction behind :
8
- =
=
To make it simple, this operator does the following tests (this can be im-
plemented with only 1 test) : if the height of b (element added behind) is
smaller than the height of a, no action is taken (a is not modified) ; if, by
contrast, it is larger, then we only add the part of the same height as a and
the rest of b is ignored.
Here are a few examples :
- =
- =
- =
- =
4. the internal operator operator+= and the external operator operator+
which adds a Construction to the right :
9
+ =
+ =
+ =
+ =
This operator does the following tests (this can be implemented with 2 tests
only) :
— if the height of b (the element added to the right) is smaller than the
height of a, no action is taken (a is not modified) ; if, by contrast, it is
higher, then we only add the part of the same height as a, the rest of b
is ignored ;
— if the depth of b is smaller than the depth of a, no action is taken (a is
not modified) ; if, by contrast, it is larger, we only add the part of the
same depth as a, the rest of b is ignored.
10
+ =
+ =
+ =
+ =
+ =
+ =
Here are a few general examples more :
11
^ =
- =
^ =
+ =
(rouge[0].size() = 1 < blanc[0].size = 2)
5. the following operators
const Construction operator*(unsigned int n, Construction const& a);
const Construction operator/(unsigned int n, Construction const& a);
const Construction operator%(unsigned int n, Construction const& a);
allow us to repeat easily some operations :
— n * a is the same as a + a + ... + a, with a repeated n times ;
— n / a is the same as a ^ a ^ ...^ a, with a repeated n times ;
— n % a is the same as a - a - ... - a, with a repeated n times.
Examples of using these operators are given in the provided main() and the
output is also shown further below.
2.2 Methodology
We suggest that you work carefully, step by step, testing your code after each
step :
1. start with the class Brique (and test your implementation) ;
2. write the basics of the class Construction, including the constructor
and the printing operator ;
12
3. start by overloading the internal operator operator^= that simply adds
a layer of blocks above the building ; test it right away with two simple
“basic blocks” ;
4. next, add and test the operator operator-= that adds building blocks in
the 2nd dimension ;
5. then add the one that works in the 3rd dimension (that is operator+=) ;
6. continue with overloading their external counterparts (operators operator^,
operator- and operator+) ;
7. finally, add the “repeated operators” (operators operator/, operator%
and operator*) ;
8. verify that the main() gives the expected results.
Couche 4 :
(obliqueG, rouge) (obliqueD, rouge)
(obliqueG, rouge) (obliqueD, rouge)
(obliqueG, rouge) (obliqueD, rouge)
Couche 3 :
(obliqueG, rouge) ( pleine , rouge) ( pleine , rouge) (obliqueD, rouge)
(obliqueG, rouge) ( pleine , rouge) ( pleine , rouge) (obliqueD, rouge)
(obliqueG, rouge) ( pleine , rouge) ( pleine , rouge) (obliqueD, rouge)
Couche 2 :
( pleine , blanc) ( pleine , blanc) ( pleine , blanc) ( pleine , blanc)
( pleine , blanc) ( pleine , blanc) ( pleine , blanc) ( pleine , blanc)
( pleine , blanc) ( pleine , blanc) ( pleine , blanc) ( pleine , blanc)
Couche 1 :
( pleine , blanc) ( pleine , blanc) ( pleine , blanc) ( pleine , blanc)
( pleine , blanc) ( pleine , blanc) ( pleine , blanc) ( pleine , blanc)
( pleine , blanc) ( pleine , blanc) ( pleine , blanc) ( pleine , blanc)
Couche 0 :
( pleine , blanc) ( pleine , blanc) ( pleine , blanc) ( pleine , blanc)
( pleine , blanc) ( pleine , blanc) ( pleine , blanc) ( pleine , blanc)
( pleine , blanc) ( pleine , blanc) ( pleine , blanc) ( pleine , blanc)
13