EISE4/MAIN4 C++ Course Notes (Autumn 2023) - Part III - Inheritance - PDF
Document Details
2023
EISE4/MAIN4
Tags
Summary
These are course notes for a C++ programming course, titled "EISE4/MAIN4 : Cours de C++". Part III focuses on the concept of inheritance, with examples illustrating basic cases and class hierarchies.
Full Transcript
EISE4/MAIN4 : Cours de C++ 2023-10-12 Part III...
EISE4/MAIN4 : Cours de C++ 2023-10-12 Part III Inheritance Part III Inheritance EISE4/MAIN4 : Cours de C++ - Les Classes et les Objets - Automne 2023 78 / 105 EISE4/MAIN4 : Cours de C++ Basic cases 2023-10-12 A Intro side Basic cases height width radius C B Basic cases A side height width radius C B EISE4/MAIN4 : Cours de C++ - Les Classes et les Objets - Automne 2023 79 / 105 EISE4/MAIN4 : Cours de C++ Basic cases 2023-10-12 A Intro side Basic cases height width radius C B Square Rectangle Circle Triangle _center _center _center _center Basic cases _side get_center() draw() _width _height get_center() _radius get_center() draw() _pointA _pointB _pointC A erase() draw() erase() get_center() get_sides() erase() draw() get_sides() erase() get_sides() side height width radius C B Square Rectangle Circle Triangle _center _center _center _center _side _width _radius _pointA get_center() _height get_center() _pointB draw() get_center() draw() _pointC erase() draw() erase() get_center() get_sides() erase() draw() get_sides() erase() get_sides() EISE4/MAIN4 : Cours de C++ - Les Classes et les Objets - Automne 2023 79 / 105 EISE4/MAIN4 : Cours de C++ Look more closer 2023-10-12 Intro Square Rectangle Circle Triangle Look more closer _center _side get_center() draw() erase() _center _width _height get_center() draw() _center _radius get_center() draw() erase() _center _pointA _pointB _pointC get_center() get_sides() erase() draw() Look more closer get_sides() erase() get_sides() Square Rectangle Circle Triangle _center _center _center _center _side _width _radius _pointA get_center() _height get_center() _pointB draw() get_center() draw() _pointC erase() draw() erase() get_center() get_sides() erase() draw() get_sides() erase() get_sides() EISE4/MAIN4 : Cours de C++ - Les Classes et les Objets - Automne 2023 80 / 105 EISE4/MAIN4 : Cours de C++ Look more closer 2023-10-12 Intro Look more closer Square Rectangle Circle Triangle _center _center _center _center _side _width _radius _pointA get_center() _height get_center() _pointB draw() get_center() draw() _pointC erase() draw() erase() get_center() Look more closer get_sides() erase() draw() get_sides() erase() get_sides() Square Rectangle Circle Triangle _center _center _center _center _side _width _radius _pointA get_center() _height get_center() _pointB draw() get_center() draw() _pointC erase() draw() erase() get_center() get_sides() erase() draw() get_sides() erase() get_sides() EISE4/MAIN4 : Cours de C++ - Les Classes et les Objets - Automne 2023 80 / 105 EISE4/MAIN4 : Cours de C++ Look more closer 2023-10-12 Intro Look more closer Square Rectangle Circle Triangle _center _center _center _center _side _width _radius _pointA get_center() _height get_center() _pointB draw() get_center() draw() _pointC erase() draw() erase() get_center() Look more closer get_sides() erase() draw() get_sides() erase() get_sides() Square Rectangle Circle Triangle _center _center _center _center _side _width _radius _pointA get_center() _height get_center() _pointB draw() get_center() draw() _pointC erase() draw() erase() get_center() get_sides() erase() draw() get_sides() erase() get_sides() EISE4/MAIN4 : Cours de C++ - Les Classes et les Objets - Automne 2023 80 / 105 EISE4/MAIN4 : Cours de C++ Class hierarchy Figure 2023-10-12 _center get_center() Intro draw() erase() Class hierarchy Polygon draw() get_sides() Circle _radius draw() Figure Class hierarchy Square _side Rectangle _width Triangle _pointA _center draw() get_sides() _height draw() get_sides() _pointB _pointC draw() get_sides() get_center() draw() Every members of figure are members of the derived class except the erase() constructor, the destructor, assignment operator Can redefine function Polygon Circle draw() _radius get_sides() draw() Square Rectangle Triangle _side _width _pointA draw() _height _pointB get_sides() draw() _pointC get_sides() draw() get_sides() EISE4/MAIN4 : Cours de C++ - Les Classes et les Objets - Automne 2023 81 / 105 EISE4/MAIN4 : Cours de C++ Defining class hierarchy 2023-10-12 class F i g u r e { Intro private : Point _center ; # include "figure.h" class C i r c l e : public F i g u r e { Defining class hierarchy public : F i g u r e ( P o i n t& c e n t e r ) ; P o i n t& g e t _ c e n t e r ( ) ; void draw ( ) const ; void erase ( ) ; private : double _ r a d i u s ; public : Circle ( ) ; void draw ( ) const ; }; Defining class hierarchy }; Inheritance limit The constructor, the destructor, assignment operator of Figure are not members of the derived class. class F i g u r e { private : # include "figure.h" define function Point _center ; class C i r c l e : public F i g u r e { invariant public : private : F i g u r e ( P o i n t& c e n t e r ) ; double _ r a d i u s ; with default behavior P o i n t& g e t _ c e n t e r ( ) ; public : abstract void draw ( ) const ; Circle ( ) ; void erase ( ) ; void draw ( ) const ; }; }; Inheritance limit The constructor, the destructor, assignment operator of Figure are not members of the derived class. EISE4/MAIN4 : Cours de C++ - Les Classes et les Objets - Automne 2023 82 / 105 EISE4/MAIN4 : Cours de C++ Public inheritance 2023-10-12 Let B and C be two classes such that C derived from B publicaly. private and public Protection private members of B : Only class B may access to these Public inheritance members public members of B : Everyone may access to these members What the compiler will say about that ? Public inheritance void C i r c l e : : Draw ( ) { s t d : : c o u t