0% found this document useful (0 votes)
4 views11 pages

Remov

The document outlines an exercise to create a C++ program that simulates mixing liquids and building structures using classes and operators. It details the requirements for implementing classes Flacon, Brique, and Construction, including their attributes, methods, and operator overloads. Additionally, it provides execution examples and a step-by-step methodology for completing the tasks.

Uploaded by

7yx7k4cfjp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views11 pages

Remov

The document outlines an exercise to create a C++ program that simulates mixing liquids and building structures using classes and operators. It details the requirements for implementing classes Flacon, Brique, and Construction, including their attributes, methods, and operator overloads. Additionally, it provides execution examples and a step-by-step methodology for completing the tasks.

Uploaded by

7yx7k4cfjp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

An example output of the code is provided further below.

For this exercise, there


will be a 5-point bonus for those who will also provide the class Flacon that
has an internal operator operator+= which allows the mix of the « flacon »
itself : even if this is not realistic according to Physics (that is why we use quo-
tation marks), such a « flacon » may see its volume increase as described above ;
meaning : after a += b; ; the volume a is indeed modified (as it will be the case
for its name and pH). If you are doing this and would like to ask for the bonus (i.e.
be graded out of 35 points), just add the following line in your file :

#define BONUS

(otherwise, you will be graded out of 30 points).

1.2 Execution examples


Si je mélange
"Eau : 600 ml, pH 7"
avec
"Acide chlorhydrique : 500 ml, pH 2"
j’obtiens :
"Eau + Acide chlorhydrique : 1100 ml, pH 2.34242"
Si je mélange
"Acide chlorhydrique : 500 ml, pH 2"
avec
"Acide perchlorique : 800 ml, pH 1.5"
j’obtiens :
"Acide chlorhydrique + Acide perchlorique : 1300 ml, pH 1.63253"

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”.

The provided code


— declares the types Forme (English : shape) and Couleur (English : co-
lor) which are useful for the “building blocks” ; for simplicity, we use the
type string ;
— starts the declaration of the class Brique (English : brick), described
further below, which is used for representing the “building blocks” ;
— inside main(), creates multiple “building blocks” and creates a “buil-
ding” which we can represent graphically like this :

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.

2.1.1 The class Brique

The class Brique represents the “building blocks” of our buildings, as shown in
the following example :

Every Brique is characterised by a shape and a color, as defined in the provided


construction.cc file.
You must complete the definition of the class Brique by :
— adding a constructor taking two parameters of type Forme and Couleur
(in this order) ; there is no default constructor for this class ;
— adding a public method with the protoype
ostream& afficher(ostream& sortie) const ;
which prints the content of the object using the format :
— if the color is not an empty string :
(shape, color)
for example (other examples are provided further below) :
(obliqueG, rouge)
— if the color is an empty string, it displays only the shape (without any-
thing else) ;
— overloading the printing operator << for this class ; the implementation
should use the method afficher.

2.1.2 The class Construction

The class Construction represents the “buildings” in our program. A “buil-


ding” is a collection of “building blocks” placed in a 3-dimensional space.
This class must have an attribute contenu (respect strictly this name) which is a
3-dimensional dynamic array of “building blocks”, i.e. a vector of vector of
vector of Brique.
To ease automatic grading, YOU MUST START THE CLASS Construction
IN THE FOLLOWING WAY :

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

The following ten operators must be added to the class Construction 1 :


1. the (external) operator for printing << which should use the method afficher ;
2. the internal operator operator^= and the external operator operator^
which adds a Construction on top :
— a ^= b; adds the Construction b on top of Construction
a, as shown here :
1. Those operators are only used to describe the layers of the construction. It is not their pur-
pose to check whether the corresponding result is physically possible or not.

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.

2.3 Execution examples


The example of output below corresponds to the provided program.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy