@@ -117,7 +117,7 @@ <h3>Spécialisation</h3>
117117 < section >
118118 < h2 > 4.1 Exemples</ h2 >
119119 < p > < b > Exemple de classe de base : < inline-code > < highlight > Circle</ highlight > </ inline-code > </ b > </ p >
120- < pre style ="width: 1600px ; "> < code
120+ < pre style ="width: 1700px ; "> < code
121121 class ="c++ "
122122 data-trim
123123 data-noescape
@@ -130,13 +130,13 @@ <h2>4.1 Exemples</h2>
130130 class Circle
131131 {
132132 public:
133- Circle(double _r =0) : r(_r ){}
134- double getRadius() const {return r ;}
135- void setRadius(double _r ) {r=_r ;}
136- double surface() {return r*r*3.14 ;}
133+ Circle(double r =0) : m_r(r ){}
134+ double getRadius() const {return m_r ;}
135+ void setRadius(double r ) {m_r=r ;}
136+ double surface() const {return m_r*m_r*std::numbers::pi ;}
137137
138138 private:
139- double r {0};
139+ double m_r {0};
140140 };
141141 </ code > </ pre >
142142 </ section >
@@ -160,11 +160,11 @@ <h2>4.1 Exemples</h2>
160160 {
161161 public:
162162 Cylinder(double=0, double=0);
163- double surface();
164- double volume();
163+ double surface() const ;
164+ double volume() const ;
165165
166166 private:
167- double h {0};
167+ double m_h {0};
168168 };
169169 </ code > </ pre >
170170 </ section >
@@ -176,9 +176,9 @@ <h2>4.1 Remarques</h2>
176176 membres de la classe < inline-code > Circle</ inline-code > , avec :
177177 </ p >
178178 < ul >
179- < li > 1 attribut supplémentaire < b > < inline-code > h </ inline-code > </ b > (hauteur du cylindre)</ li >
179+ < li > 1 attribut supplémentaire < b > < inline-code > m_h </ inline-code > </ b > (hauteur du cylindre)</ li >
180180 < li > 1 nouvelle méthode < b > < inline-code > volume()</ inline-code > </ b > </ li >
181- < li > 1 méthode redéfinie < b > < inlinde -code> surface()</ inlinde -code> </ b > (formule différente)</ li >
181+ < li > 1 méthode redéfinie < b > < inline -code> surface()</ inline -code> </ b > (formule différente)</ li >
182182 </ ul >
183183 < p > La méthode < inline-code > surface()</ inline-code > a été redéfinie (pas une surcharge)</ p >
184184 < br >
@@ -191,7 +191,7 @@ <h2>4.1 Remarques</h2>
191191 < section >
192192 < h2 > 4.1 Exemples</ h2 >
193193 < p > < b > Implémentation de la classe < highlight > Cylinder</ highlight > </ b > </ p >
194- < pre style ="width: 1600px ; "> < code
194+ < pre style ="width: 1700px ; "> < code
195195 class ="c++ "
196196 data-trim
197197 data-noescape
@@ -202,25 +202,21 @@ <h2>4.1 Exemples</h2>
202202 #include "cylinder.h"
203203
204204 Cylinder::Cylinder(double radius, double height)
205- < b > : Circle(radius)</ b >
205+ < b > : Circle(radius), m_h(height) </ b >
206206 {
207- h=height;
208207 }
209208
210- double Cylinder::surface()
209+ double Cylinder::surface() const
211210 {
212- double r = this-> getRadius(); // Circle::r is private
213- return 2 * 3.14 * r * (r + h );
211+ double r = this-> getRadius(); // Circle::m_r is private
212+ return 2 * std::numbers::pi * r * (r + m_h );
214213 }
215214 </ code > </ pre >
216215 </ section >
217216
218217 < section >
219218 < h2 > 4.1 Remarques</ h2 >
220- < p > Dans la méthode < inline-code > surface()</ inline-code > de < inline-code > Cylinder</ inline-code > , la variable
221- locale < inline-code > r</ inline-code > masque l'attribut du même nom (hérité de
222- < inline-code > Circle</ inline-code > ).</ p >
223- < p > Comme l'attribut < inline-code > r</ inline-code > a été déclaré < highlight > private</ highlight > dans
219+ < p > Comme l'attribut < inline-code > m_r</ inline-code > a été déclaré < highlight > private</ highlight > dans
224220 < inline-code > Circle</ inline-code > , la classe < inline-code > Cylinder</ inline-code > n'a pas accès à ce membre
225221 (bien qu'il soit présent, car hérité de < inline-code > Circle</ inline-code > ).</ p >
226222 < p > Un droit d'accès supplémentaire a été introduit pour pallier ce problème : < highlight > protected</ highlight >
@@ -257,7 +253,7 @@ <h2>4.2 Contrôle d'accès <span style="text-transform: lowercase;"><inline-code
257253 < section >
258254 < h2 > 4.2 Exemples</ h2 >
259255 < p > < b > Nouvelle classe de base < inline-code > < highlight > Circle</ highlight > </ inline-code > </ b > </ p >
260- < pre style ="width: 1600px ; "> < code
256+ < pre style ="width: 1700px ; "> < code
261257 class ="c++ "
262258 data-trim
263259 data-noescape
@@ -270,21 +266,21 @@ <h2>4.2 Exemples</h2>
270266 class Circle
271267 {
272268 public:
273- Circle(double _r =0) : r(_r ){}
274- double getRadius() const {return r ;}
275- void setRadius(double _r ) {r=_r ;}
276- double surface() {return r*r*3.14 ;}
269+ Circle(double r =0) : m_r(r ){}
270+ double getRadius() const {return m_r ;}
271+ void setRadius(double r ) {m_r = r ;}
272+ double surface() const {return m_r*m_r*std::numbers::pi ;}
277273
278274 < b > protected:</ b >
279- double r {0};
275+ double m_r {0};
280276 };
281277 </ code > </ pre >
282278 </ section >
283279
284280 < section >
285281 < h2 > 4.2 Exemples</ h2 >
286282 < p > < b > Nouvelle implémentation de la classe < inline-code > < highlight > Cylinder</ highlight > </ inline-code > </ b > </ p >
287- < pre style ="width: 1600px ; "> < code
283+ < pre style ="width: 1700px ; "> < code
288284 class ="c++ "
289285 data-trim
290286 data-noescape
@@ -295,14 +291,14 @@ <h2>4.2 Exemples</h2>
295291 #include "cylinder.h"
296292
297293 Cylinder::Cylinder(double radius, double height)
298- < b > : Circle(radius)</ b >
294+ < b > : Circle(radius), m_h(height) </ b >
299295 {
300- h=height;
301296 }
302297
303- double Cylinder::surface()
298+ double Cylinder::surface() const
304299 {
305- < b > return 2 * 3.14 * r * (r + h);</ b > // r is now accessible
300+ // m_r is now accessible
301+ < b > return 2 * std::numbers::pi * m_r * (m_r + m_h);</ b >
306302 }
307303 </ code > </ pre >
308304 </ section >
@@ -470,9 +466,9 @@ <h2>4.4 Création / Destruction des objets</h2>
470466 // Cylinder.cpp
471467 #include "cylinder.h"
472468
473- Cylinder::Cylinder(double _r , double _h ) < highlight > : Circle(_r )</ highlight >
469+ Cylinder::Cylinder(double r , double h ) < highlight > : Circle(r )</ highlight >
474470 {
475- this- > h = _h ;
471+ m_h = h ;
476472 }
477473 </ code > </ pre >
478474 < p > Appel < error > implicite</ error > des constructeurs de base</ p >
@@ -486,11 +482,10 @@ <h2>4.4 Création / Destruction des objets</h2>
486482 // Cylinder.cpp
487483 #include "cylinder.h"
488484
489- Cylinder::Cylinder(double _r, double _h) // Call to Circle()
490- {
491-
492- this-> r = _r; // ⛔ r is private in Circle
493- this-> h = _h;
485+ Cylinder::Cylinder(double r, double h) // Call to Circle()
486+ {
487+ m_r = r; // ⛔ r is private in Circle
488+ m_h = h;
494489 }
495490 </ code > </ pre >
496491 </ section >
@@ -553,7 +548,7 @@ <h2>4.5 Conversions standard Base ⟺ Dérivée </h2>
553548 Base(const Base&) = default;
554549 Base &operator=(const Base&) = default;
555550 private:
556- int b {0};
551+ int m_b {0};
557552 };
558553
559554 class Derived : public Base
@@ -563,7 +558,7 @@ <h2>4.5 Conversions standard Base ⟺ Dérivée </h2>
563558 Derived(const Derived&) = default;
564559 Derived &operator=(const Derived&) = default;
565560 private:
566- int d {0};
561+ int m_d {0};
567562 };
568563 </ code > </ pre >
569564 </ div >
@@ -735,67 +730,63 @@ <h3>Quelques rappels</h3>
735730
736731 < section >
737732 < h2 > 4.6 Exemple introductif</ h2 >
738- < div class ="container ">
739- < div class ="col-left ">
740- < pre style ="width: 800px; "> < code
733+ < pre style ="width: 1200px; "> < code
741734 class ="c++ "
742735 data-trim
743736 data-noescape
744- style ="font-size: 1.5em ; "
737+ style ="font-size: 1.4em ; "
745738 data-line-numbers ="">
746-
747739 class Animal
748740 {
749741 public:
750- void MakeSound() const
751- {
752- std::cout < < "..." < < std::endl ;
753- }
754- };
742+ virtual ~Animal() = default;
743+ std::string id() const { return "animal"; }
744+ };
745+
755746 class Cat : public Animal
756747 {
757748 public:
758- void MakeSound() const
759- {
760- std::cout < < "meow" < < std::endl;
761- }
762- };
749+ std::string id() const { return "cat"; }
750+ };
751+
752+ class StrayCat : public Cat
753+ {
754+ public:
755+ std::string id() const { return "stray cat"; }
756+ };
757+
763758 class Dog : public Animal
764759 {
765760 public:
766- void MakeSound () const
767- {
768- std::cout < < "woof" < < std::endl;
769- }
770- }; </ code > </ pre >
771- </ div >
772- < div class =" col-right " >
773- < pre style ="width: 1000px ; "> < code
761+ std::string id () const { return "dog"; }
762+ };
763+ </ code > </ pre >
764+ </ section >
765+
766+ < section >
767+ < h2 > 4.6 Exemple introductif </ h2 >
768+ < pre style ="width: 1800px ; "> < code
774769 class ="c++ "
775770 data-trim
776771 data-noescape
777- style ="font-size: 1.5em ; "
772+ style ="font-size: 1.7em ; "
778773 data-line-numbers ="">
779774
780775 int main()
781776 {
782- auto animal = new Animal;
783- auto cat = new Cat;
784- auto dog = new Dog;
785-
786- std::vector<Animal *> animals = {animal, cat, dog};
787-
788- for (const auto &animal : animals)
777+ ...
778+
779+ std::vector<Animal*> pA = { new Animal, new Dog, new Cat, new StrayCat };
780+ for (auto p : pA)
789781 {
790- animal- > < b > MakeSound(); </ b >
782+ std::println("{}", p- > id());
791783 }
792784
793785 return 0;
794786 }
795787 </ code > </ pre >
788+ < br >
796789 < p > < b > Quelle est la sortie de ce programme ?</ b > </ p >
797- </ div >
798- </ div >
799790 </ section >
800791
801792 < section >
@@ -808,12 +799,13 @@ <h2>4.6 Exemple introductif</h2>
808799 style ="font-size: 1.6em; "
809800 data-line-numbers ="">
810801
811- ...
812- ...
813- ...
802+ animal
803+ animal
804+ animal
805+ animal
814806 </ code > </ pre >
815807 < p > Le type statique est < inline-code > Animal *</ inline-code > donc on appelle la méthode
816- < inline-code > Animal::MakeSound ()</ inline-code > 3 fois de suite</ p >
808+ < inline-code > Animal::id ()</ inline-code > 4 fois de suite</ p >
817809 < p > Le < highlight > polymorphisme</ highlight > permet de résoudre ce problème et d'accéder automatiquement aux
818810 méthodes des classes dérivées en utilisant le type dynamique</ p >
819811 </ section >
@@ -841,9 +833,7 @@ <h2>4.6 Fonctions virtuelles</h2>
841833
842834 < section >
843835 < h2 > 4.6 Polymorphisme</ h2 >
844- < div class ="container ">
845- < div class ="col-left ">
846- < pre style ="width: 900px; "> < code
836+ < pre style ="width: 1600px; "> < code
847837 class ="c++ "
848838 data-trim
849839 data-noescape
@@ -853,48 +843,27 @@ <h2>4.6 Polymorphisme</h2>
853843 class Animal
854844 {
855845 public:
856- < b > virtual ~Animal() = default;</ b >
857- < b > virtual</ b > void MakeSound () const {... }
846+ virtual ~Animal() = default;
847+ < b > virtual</ b > std::string id () const { return "animal"; }
858848 };
859-
849+
860850 class Cat : public Animal
861851 {
862852 public:
863- < b > virtual ~Cat() = default;</ b >
864- void MakeSound() const < b > override</ b > {...}
853+ std::string id() const < b > override</ b > { return "cat"; }
865854 };
866-
867- class Dog : public Animal
855+
856+ class StrayCat : public Cat
868857 {
869858 public:
870- < b > virtual ~Dog() = default;</ b >
871- void MakeSound() const < b > override</ b > {...}
872- };</ code > </ pre >
873- </ div >
874- < div class ="col-right ">
875- < pre style ="width: 1000px; "> < code
876- class ="c++ "
877- data-trim
878- data-noescape
879- style ="font-size: 1.5em; "
880- data-line-numbers ="">
859+ std::string id() const < b > override</ b > { return "stray cat"; }
860+ };
881861
882- int main()
862+ class Dog : public Animal
883863 {
884- auto animal = new Animal;
885- auto cat = new Cat;
886- auto dog = new Dog;
887-
888- std::vector<Animal *> animals = {animal, cat, dog};
889-
890- for (const auto &animal : animals)
891- {
892- animal-> < b > MakeSound();</ b >
893- }
894-
895- return 0;
896- }
897- </ code > </ pre >
864+ public:
865+ std::string id() const < b > override</ b > { return "dog"; }
866+ };</ code > </ pre >
898867 < p > < b > Le programme principal (< inline-code > int main(){...}</ inline-code > reste le même</ b > </ p >
899868 </ div >
900869 </ div >
@@ -910,9 +879,10 @@ <h2>4.6 Polymorphisme</h2>
910879 style ="font-size: 1.6em; "
911880 data-line-numbers ="">
912881
913- ...
914- meow
915- woof
882+ animal
883+ dog
884+ cat
885+ straycat
916886 </ code > </ pre >
917887 < p > Le polymorphisme est un point essentiel de la POO (avec l'abstraction de données et l'héritage)</ p >
918888 < p > Il permet d'appeler la méthode d'un objet sans se soucier de son type statique, et qu'elle s'adapte au type
0 commit comments