1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’ zClasses can inherit attributes and methods from other classes zInheriting class can add extra attributes and or methods of its own
2 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two or more classes zImproved efficiency and greater robustness
3 Terminology zDerived class or subclass or child class. A class which inherits some of its attributes and methods from another class zBase class or superclass or parent class. A class from which another class inherits zancestor. A class’s ancestors are those from which its own superclasses inherit zdescendant. A class’s descendants are those which inherit from its subclasses
4 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block
5 Terminology - a classification Hierarchy Generalisation Specialisation Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block
6 Terminology - a classification Hierarchy Generalised ‘base class’ Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block
7 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block A ‘kind of’ Building (AKO)
8 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block A ‘kind of’ Commercial building (AKO)
9 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block Arrow in diagram means ’inherits from’
10 Designing your classification hierarchy: ‘A kind of’ or ‘a part of’? Car Vehicle A car is ‘a kind of’ vehicle car class can inherit from vehicle class
11 Car Wheel Vehicle A car is ‘a kind of’ vehicle car class can inherit from vehicle class A wheel isn’t ‘a kind of’ car. A wheel is ‘a part of’ a car -this is dealt with by aggregation Designing your classification hierarchy: ‘A kind of’ or ‘a part of’?
12 yNeed to analyse whether differences between objects are dependant on type (such as a house being different to a factory) or state. (different values of the class’s attributes) Building Short Building Tall Building short building and tall building might vary only in the value of the height attribute - don’t need separate classes Designing your classification hierarchy: Different classes or different states?
13 What do objects inherit? Line Attributes: start position end position Methods: draw Coloured Line Attributes: colour Methods: set colour A ‘coloured line’ is a kind of line the coloured line class inherits all the attributes and methods of the line class and adds attributes and methods of its own An object of the ‘coloured line’ class has all the attributes and methods of the ‘line’ base class as well as the attributes and methods added by the derived class
14 Specialisation Extending the functionality of an existing class eg a coloured line is a specialised kind of line
15 Specialisation A class is both yclosed in that it has an encapsulated, private part which cannot be affected by external manipulation yand open in that it allows itself to be used as part of a larger software unit.
16 Generalisation Sharing commonality between two or more classes xIf we were modelling animals in a zoo would we create a separate class for each animal type? xThis would duplicate attributes such as legs and methods such as getAge() CowWhaleEagleElephant
17 Generalisation xHelpful to place common elements (attributes and methods) in a shared base class and organise problem into an inheritance hierarchy. CowWhaleEagleElephant Animal MammalBird
18 Generalisation xSometimes this leads to the creation of abstract classes which can’t be instantiated directly CowWhaleEagleElephant Animal MammalBird Abstract classes
19 Generalisation xconcrete classes can be instantiated directly CowWhaleEagleElephant Animal MammalBird Concrete classes
20 C++ Syntax zThe colon (:) operator to denote inheritance zPublic and private derivation of object methods from a base class zThe ‘protected’ keyword to allow derived classes to access inherited attributes
21 C++ Syntax class BaseClass { private: int x; public: void setX(int x_in){x=x_in;} int getX(){cout<<x;} } A simple base class with one private attribute x and two public methods
Derived Class 22 class DerivedClass:public BaseClass { private: int y; public: void setY(int x_in){x=x_in;} int getY(){cout<<y;} } A simple derived class with one private attribute y and two public methods
Implementing Derived Class Object 23 main() { DerivedClass obj1; obj1.SetY(10); obj1.SetX(5); obj1.getX(); obj1.getY(); } Obj1 is Derived class object, Which can access the BaseClass Public Methods(SetX, getX)
when the Derived Class Object is Created z Space is allocated (on the stack or the heap) for the full object (that is, enough space to store the inherited data members from the base class and defined data members in the derived class itself.) z The base class constructor is called to initialize the data members inherited from the base class. zThe derived class constructor is then called to initialize the data members added in the derived class z The derived-class object is then usable 24
When the Derived Class Object is Destroyed zWhen the object is destroyed (goes out of scope or is deleted) the derived class destructor is called on the object first zThen the base class destructor is called on the object zFinally the allocated space for the full object is reclaimed 25
Types of Derivation zThe class can be derived in three visibility modes x public xprivate xprotected 26
Public Derivation zWhen a class is derived in the public mode it does not change the mode of the inherited members in the derived class. The public members remain public and protected members remain protected for the derived class in public inheritance. 27
Private Derivation zWhen a class is derived in the private mode it changes the mode of the inherited members in the derived class. The public and protected members become the private members for the derived class. So the inherited members can be accessed only through the member functions of the derived class. 28
Protected Derivation zWhen a class is derived in protected mode it changes the mode of the inherited members in the derived class. The public and protected members become the protected members for the derived class in protected inheritance. So the inherited members can be accessed only through the member functions of the derived class. 29
30 C++ Syntax: public derivation class DerivedClass: public BaseClass { private: int y; public: void setY(int y_in) {y=y_in;} int getY(){cout<<y;} } A derived class. The colon operator means the derived class inherits from the base class
31 C++ Syntax: public derivation class DerivedClass: public BaseClass { private: int y; public: void setY(int y_in) {y=y_in;} int getY() {cout<<y;} } the public derivation means that objects of the derived class can access the public methods and attributes of the base class This is the most usual type of derivation
32 C++ Syntax: public derivation class BaseClass { private: int x; public: void setX(int x_in) {x=x_in;} int getX() {cout<<x;} } class DerivedClass: public BaseClass { private: int y; public: void setY(int y_in){y=y_in;} int getY(){cout<<y;} } main() { BaseClass base_object; DerivedClass derived_object; base_object.setX(7); derived_object.setX(12); derived_object.setY(1); base_object.getX(); derived_object.getY(); derived_object.getX(); return 0; } Object of the derived class can access methods of the derived class and also methods of the base class
33 C++ Syntax: private derivation zclass DerivedClass: private BaseClass z{ zprivate: yint y; zpublic: yvoid setY(int y_in); yint getY(); z} Another derived class - the private derivation means that objects of the derived class can’t access the public methods and attributes of the base class - but the methods of the derived class can! This is the least common type
34 C++ Syntax: the ‘protected’ keyword zAn object of a publicly derived class can access the public methods of the base class, but not the private attributes zChanging the private keyword in the base class to protected makes the attributes available to derived classes
Access Specifiers Access Specifier Accessible from own class Accessible from derived class Accessible from objects from outside the class publicYes protectedYes No privateYesNo 35
Effect of inheritance on the visibility of member 36
37 C++ Syntax: inheriting destructors zConstructors are not inherited. They are called implicitly or explicitly by the child constructor. zThe compiler creates a default constructor (one with no arguments) and a default copy constructor (one with an argument which is a reference to the same type). z But if we want a constructor that will accept an int, we have to define it explicitly
CM Advantages of Inheritance When one class is inherited from another class the code that provides a behavior required in the derived class need not have to be rewritten that is code be reused which increases reliability.
CM Advantages of Inheritance Code sharing can occur at several levels. For example, At a higher level, many users can use the same class. At lower level, code can be shared by two or more classes.
CM Advantages of Inheritance When multiple classes inherit from the same base class, it guarantees that the behavior they inherit will be the same in all classes.
CM Advantages of Inheritance Inheritance permits the construction of reusable software components. Using inheritance one can concentrate on understanding the portion of the new system.
CM Advantages of Inheritance The development time in developing a system will be reduced rapidly.