-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b0dc7b
commit 3421c6f
Showing
2 changed files
with
130 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
# poo-exercices | ||
|
||
## Reference | ||
Exercice sur les références | ||
|
||
## Object | ||
|
||
## Héritage | ||
|
||
## Exception | ||
Exercices sur les exceptions et le danger de l'allocation. | ||
# Exercices POO | ||
## Ex 1 - Namespace | ||
- [namespace.md](https://github.com/tony-maulaz/poo-exercices/blob/main/ex1-namespace.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# Ex 1 | ||
Modifier le code suivant sans utiliser de `using` pour le rendre exécutable. | ||
|
||
```cpp | ||
#include <iostream> | ||
|
||
namespace lib1 { | ||
int calc(int val){ | ||
return val * 2; | ||
} | ||
} | ||
|
||
namespace lib2 { | ||
int var = 2; | ||
} | ||
|
||
int main() | ||
{ | ||
|
||
cout << "Ex namespace" << endl; | ||
|
||
cout << calc( var ) << endl; | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
# Ex 2 | ||
Reprenez le code de l'exercice 1 et rendez le exécutable en utilisant des `using` | ||
|
||
# Ex 3 | ||
Lors de l'exécution du code ci-dessous, quel est l'affichage. | ||
|
||
```C | ||
#include <iostream> | ||
|
||
namespace lib1 { | ||
void afficher(int val){ | ||
std::cout << "Afficher lib1" << std::endl; | ||
} | ||
} | ||
|
||
namespace lib2 { | ||
void afficher(int val){ | ||
std::cout << "Afficher lib2" << std::endl; | ||
} | ||
} | ||
|
||
using namespace lib1; | ||
|
||
int main() | ||
{ | ||
afficher(12); | ||
afficher(24); | ||
return 0; | ||
} | ||
``` | ||
|
||
# Ex 4 | ||
Dans l'exercice 3, modifier l'appel de la deuxième fonction `afficher` afin d'utiliser celle de la `lib2`. | ||
|
||
|
||
# solutions | ||
## Ex 1 | ||
```cpp | ||
#include <iostream> | ||
|
||
namespace lib1 { | ||
int calc(int val){ | ||
return val * 2; | ||
} | ||
} | ||
|
||
namespace lib2 { | ||
int var = 2; | ||
} | ||
|
||
int main() | ||
{ | ||
|
||
std::cout << "Ex namespace" << std::endl; | ||
|
||
std::cout << lib1::calc( lib2::var ) << std::endl; | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
## Ex 2 | ||
```cpp | ||
#include <iostream> | ||
|
||
namespace lib1 { | ||
int calc(int val){ | ||
return val * 2; | ||
} | ||
} | ||
|
||
namespace lib2 { | ||
int var = 2; | ||
} | ||
|
||
using namespace std; | ||
using namespace lib1; | ||
using namespace lib2; | ||
|
||
int main() | ||
{ | ||
|
||
cout << "Ex namespace" << endl; | ||
|
||
cout << calc( var ) << endl; | ||
|
||
return 0; | ||
} | ||
``` | ||
|
||
## Ex 3 | ||
```console | ||
Afficher lib1 | ||
Afficher lib1 | ||
``` | ||
|
||
## Ex 4 | ||
```cpp | ||
lib2::afficher(24); // appel lib2 | ||
``` |