Inheritance Mechanism for deriving new classes from existing classes Chapter 13.
1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance...
-
Upload
quentin-small -
Category
Documents
-
view
214 -
download
0
Transcript of 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance...
![Page 1: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/1.jpg)
1
What is Inheritance?
The mechanism of deriving a new class from an old one is called inheritance
Classes organised into a ‘classification hierarchy’
Classes can inherit attributes and methods from other classes
Inheriting class can add extra attributes and or methods of its own
![Page 2: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/2.jpg)
2
What is the purpose of Inheritance?
SpecialisationExtending the functionality of an existing
classGeneralisation
sharing commonality between two or more classes
Improved efficiency and greater robustness
![Page 3: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/3.jpg)
3
Terminology
Derived class or subclass or child class. A class which inherits some of its attributes and
methods from another class Base class or superclass or parent class.
A class from which another class inherits ancestor.
A class’s ancestors are those from which its own superclasses inherit
descendant.A class’s descendants are those which inherit
from its subclasses
![Page 4: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/4.jpg)
4
Terminology - a classification Hierarchy
Building
Commercial Public Domestic
Officeblock
Factory
Cathedral Hospital
Officeblock
ApartmentBlock
![Page 5: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/5.jpg)
5
Terminology - a classification Hierarchy
Generalisation
Specialisation
Building
Commercial Public Domestic
Officeblock
Factory
Cathedral Hospital
Officeblock
ApartmentBlock
![Page 6: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/6.jpg)
6
Terminology - a classification Hierarchy
Generalised
‘base class’Building
Commercial Public Domestic
Officeblock
Factory
Cathedral Hospital
Officeblock
ApartmentBlock
![Page 7: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/7.jpg)
7
Terminology - a classification Hierarchy
Building
Commercial Public Domestic
Officeblock
Factory
Cathedral Hospital
Officeblock
ApartmentBlock
A ‘kind of’ Building
(AKO)
![Page 8: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/8.jpg)
8
Terminology - a classification Hierarchy
Building
Commercial Public Domestic
Officeblock
Factory
Cathedral Hospital
Officeblock
ApartmentBlock
A ‘kind of’
Commercial building
(AKO)
![Page 9: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/9.jpg)
9
Terminology - a classification Hierarchy
Building
Commercial Public Domestic
Officeblock
Factory
Cathedral Hospital
Officeblock
ApartmentBlock
Arrow in diagram
means
’inherits from’
![Page 10: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/10.jpg)
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
![Page 11: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/11.jpg)
11
Car
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’?
![Page 12: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/12.jpg)
12
Need 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
TallBuilding
short building and tall buildingmight vary only in the value of
the height attribute - don’t need separate classes
Designing your classification hierarchy:Different classes or different states?
![Page 13: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/13.jpg)
13
What do objects inherit?
Line
Attributes:start positionend position
Methods:draw
Coloured Line
Attributes:colour
Methods:set colour
A ‘coloured line’ is a kind of linethe coloured line class inherits all
the attributes and methods ofthe 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 aswell as the attributes and methods
added by the derived class
![Page 14: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/14.jpg)
14
Specialisation
Extending the functionality of an existing class
eg a coloured line is a specialised kind of line
![Page 15: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/15.jpg)
15
Specialisation
A class is both closed in that it has an
encapsulated, private part which cannot be affected by external manipulation
and open in that it allows itself to be used as part of a larger software unit.
![Page 16: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/16.jpg)
16
Generalisation
Sharing commonality between two or more classes
If we were modelling animals in a zoo would we create a separate class for each animal type?
This would duplicate attributes such as legs and methods such as getAge()
Cow Whale EagleElephant
![Page 17: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/17.jpg)
17
Generalisation
Helpful to place common elements (attributes and methods) in a shared base class and organise problem into an inheritance hierarchy.
Cow Whale EagleElephant
Animal
Mammal Bird
![Page 18: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/18.jpg)
18
Generalisation
Sometimes this leads to the creation of abstract classes which can’t be instantiated directly
Cow Whale EagleElephant
Animal
Mammal Bird
Abstract classes
![Page 19: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/19.jpg)
19
Generalisation
concrete classes can be instantiated directly
Cow Whale EagleElephant
Animal
Mammal BirdConcrete classes
![Page 20: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/20.jpg)
20
C++ Syntax
The colon (:) operator to denote inheritance
Public and private derivation of object methods from a base class
The ‘protected’ keyword to allow derived classes to access inherited attributes
![Page 21: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/21.jpg)
21
C++ Syntax
class BaseClass{private:
int x;
public: void setX(int x_in){x=x_in;} int getX(){cout<<x;}
}
A simple base classwith one private attribute x
and two public methods
![Page 22: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/22.jpg)
22
Derived Class
class DerivedClass:public BaseClass{private:
int y;
public: void setY(int x_in){x=x_in;} int getY(){cout<<y;}
}
A simple derived classwith one private attribute y
and two public methods
![Page 23: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/23.jpg)
23
Implementing Derived Class Object
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)
![Page 24: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/24.jpg)
24
when the Derived Class Object is Created
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.)
The base class constructor is called to initialize the data members inherited from the base class.
The derived class constructor is then called to initialize the data members added in the derived class
The derived-class object is then usable
![Page 25: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/25.jpg)
25
When the Derived Class Object is Destroyed
When the object is destroyed (goes out of scope or is deleted) the derived class destructor is called on the object first
Then the base class destructor is called on the object
Finally the allocated space for the full object is reclaimed
![Page 26: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/26.jpg)
26
Types of Derivation
The class can be derived in three visibility modes
publicprivateprotected
![Page 27: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/27.jpg)
27
Public Derivation
When 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.
![Page 28: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/28.jpg)
28
Private Derivation
When 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.
![Page 29: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/29.jpg)
29
Protected Derivation
When 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.
![Page 30: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/30.jpg)
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 classinherits from the base class
![Page 31: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/31.jpg)
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 classcan access the public methodsand attributes of the base class
This is the most usual typeof derivation
![Page 32: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/32.jpg)
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 derivedclass can access methods
of the derived classand also methods of the
base class
![Page 33: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/33.jpg)
33
C++ Syntax: private derivation
class DerivedClass: private BaseClass
{private:
int y;public:
void setY(int y_in); int getY();
}
Another derived class - the privatederivation means that objects
of the derived class can’taccess the public methods and
attributes of the base class - butthe methods of the derived class
can!This is the least common type
![Page 34: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/34.jpg)
34
C++ Syntax: the ‘protected’ keyword
An object of a publicly derived class can access the public methods of the base class, but not the private attributes
Changing the private keyword in the base class to protected makes the attributes available to derived classes
![Page 35: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/35.jpg)
35
Access Specifiers
Access Specifier
Accessible from own class
Accessible from derived class
Accessible from objects from outside the class
public Yes Yes Yes
protected Yes Yes No
private Yes No No
![Page 36: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/36.jpg)
36
Effect of inheritance on the visibility of member
![Page 37: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/37.jpg)
37
C++ Syntax: inheriting destructors
Constructors are not inherited. They are called implicitly or explicitly by the child constructor.
The 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).
But if we want a constructor that will accept an int, we have to define it explicitly
![Page 38: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/38.jpg)
CM505.39 38
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.
![Page 39: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/39.jpg)
CM505.39 39
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.
![Page 40: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/40.jpg)
CM505.39 40
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.
![Page 41: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/41.jpg)
CM505.39 41
Advantages of Inheritance
• Inheritance permits the construction of reusable software components.
• Using inheritance one can concentrate on understanding the portion of the new system.
![Page 42: 1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’](https://reader034.fdocuments.us/reader034/viewer/2022051516/56649e3f5503460f94b30788/html5/thumbnails/42.jpg)
CM505.39 42
Advantages of Inheritance
• The development time in developing a system will be reduced rapidly.