From 27fb48dd49a8e3b6680b38611e89f391919d09e5 Mon Sep 17 00:00:00 2001 From: Maulaz Tony Date: Mon, 14 Mar 2022 14:47:31 +0100 Subject: [PATCH] Add exercice heritage-pointeur 61 --- README.md | 3 ++ ex61-heritage-pointeur.md | 89 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 ex61-heritage-pointeur.md diff --git a/README.md b/README.md index 3bc43fd..2e92ea5 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ ## Ex 60 - Heritage - [heritage.md](https://github.com/tony-maulaz/poo-exercices/blob/main/ex60-heritage.md) +## Ex 61 - Heritage et pointeurs +- [heritage-pointeur.md](https://github.com/tony-maulaz/poo-exercices/blob/main/ex61-heritage-pointeur.md) + ## Ex 70 - Polymorphisme - [polymorphism.md](https://github.com/tony-maulaz/poo-exercices/blob/main/ex70-polymorphism.md) diff --git a/ex61-heritage-pointeur.md b/ex61-heritage-pointeur.md new file mode 100644 index 0000000..004aa4a --- /dev/null +++ b/ex61-heritage-pointeur.md @@ -0,0 +1,89 @@ +## Ex 1 + +Quel est l'affichage du programme suivant ? + +```CPP +#include + +using namespace std; + +struct Parent{ + void execute(){ cout << "From parent" << endl; } +}; + +struct Child : public Parent{ + void execute(){ cout << "From child" << endl; } +}; + +void func(Parent* p){ + p->execute(); +} + +int main(){ + Child child; + child.execute(); + + Parent* p = &child; + p->execute(); + + func(&child); +} +``` + +## Ex 2 + +Est-ce que le code suivant est exécutable ? + +```CPP +struct Parent{ + void execute(){ cout << "From parent" << endl; } +}; + +struct Child : public Parent{ + void execute(){ cout << "From child" << endl; } + + int val; +}; + +int main(){ + Child child; + Parent* p = &child; + + p->val = 12; +} +``` + +## Ex 3 + +Quels sont les attributs accessibles de l'objet `p` dans la fonction `func` ? + +```CPP +#include + +using namespace std; + +class Parent{ + public: + int val1; + + protected: + int val2; +}; + +struct Child : public Parent{ + + int val3; +}; + +void func(Parent& p){ + //p.??? + // val1 ? + // val2 ? + // val3 ? +} + +int main(){ + Child child; + func(child); +} +``` \ No newline at end of file