Download presentation
Presentation is loading. Please wait.
1
Programming with ANSI C ++
A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
2
Chapter 9 Inheritance
3
The need Mechanism of creating a new class from already defined class
The new class contains all attributes of the old class in addition to some of its own attributes Additionally, it can also override some of the features of the old class. Not reinventing the wheel!
4
Continue We do not need to start from scratch
A class once defined can be used multiple times by other applications with modifications Proper use of inheritance helps in getting better and more robust solutions quickly Class which for creating and processing windows on the screen
5
The Advantage The generic class may contain caption of the window, specific border, specific corners, specific size, specific look and feel, maybe minimize, maximize and close buttons as well Additional functionalities provided concentrate on our additional part quick Robust
6
What is Inheritance Mechanism of getting a derived class from base class Common in the real-world Properties of a base class are automatically available in the derived class
7
Hierarchy is not forced in C++!
Java has a root class called Object C++ has no such notion We can program without such a hierarchy (object based programming)
8
Other advantages Programming using inheritance is known as object oriented programming Approach using object based method provides better performance but less flexible C++ gives you a choice Making Common Attributes Explicit in Base Class
9
Advantages: Mapping a Real World Hierarchy
Help modeling isa relationship Part of relationship is different than isa it requires programming discipline not to inherit when the relationship is not subset-of or isa.
10
isa and Part of Relationships
If we need to use functions that are defined in that class, it is better to rewrite them in our new class or design in such a way that such inheritance does not take place.
11
Meaning of Inheritance in C++
Which data member is available to derived class and in which form depends on the type of inheritance (private, public or protected) It also depends on the original access specifiers associated with the base class members
12
Derivation Using Public
class Base {//Body of the base} class Derived : public Base { //body of the derived }
13
Derivation Using Private
class Base {//Body of the base} class Derived : private Base { //body of the derived }
14
Protected Access Specifier
When the class containing protected data members is inherited, the protected members are available to derived class member functions as well But they are not available to objects of derived class
15
Derivation Using Protected
class Base {//Body of the base} class Derived : protected Base { //body of the derived }
16
Resultant access specifier
Member type in a base class Type of derivation Member type in derived class Private Not available Protected Public
17
Effect on further inheritance
The further inheritance access specifier The Effective Access Specifier value in further inheritance Public Private Protected Not Available
18
Implementation of Inheritance in C++ Object Model
The compiler embeds the base class completely in a derived class at the time of derivation Consistent performance while accessing derived or base class element This efficiency has come for a price. Implementation using pointer is not efficient in terms of time.
19
Access Control All member functions of the derived class
Public members of the class Private members of the class Protected members of the class All objects of the class as well as derived class All member functions of the derived class All member functions of the class and friends Access Control
20
Access Declaration What If we want to derive a class and want only a few and not all of the public members to be available ? Using access declaration, we can provide public access to some of the base class members even after deriving them as private The access specifier cannot be modified to raise the status of the original access specifier
21
Deriving Multiple Classes from a Single Base Class
Suppose there are some student classes, say school student, undergraduate student and postgraduate students. Need to find out common elements in them The student class (the base class for all of them) is to be created with those elements Thus the base class contents are decided after derived class contents are determined.
22
Multiple Inheritance It is possible to derive a single class from more than one class. Ex. SportsStudent class is inherited from the student class; and it also inherits from SportsPerson class
23
Multiple Inheritance The object of SportsStudent class contains properties like name, address, etc. derived from the student class, and name of sport he/she is playing for, details about national and international sports events taken part in, medals won so far etc. which are derived from the SportsPerson class.
24
Multiple Inheritance There are some other attributes like sports subjects taken, sports points obtained (from practical exams and participation in various events), which are specific to a sports student
25
Normal Inheritance case
Base class data Derived class personal data A further derived class personal data The contents of the class derived from a derived class To get base class object out of DDerived object this portion is to be taken out The address of the object To get derived class object out of DDerived object this portion is to be taken out
26
Multiple-Derivation Example
To get Base2 class out of DerivedObject this portion is to be taken out Base Class1 personal data Base Class2 Personal data Derived class Personal data The contents of the class derived from a derived class in case of multiple inheritance The address of the object
27
The code which does not requires compiler intervening
Base BaseObject; Derived DerivedObject; DDerived DDerivedObject; Base *BaseObjPtr = &DerivedObject Derived *DerivedObjPtr = &DDerivedObject
28
The code which does requires compiler intervening
Class Derived: public Base1, public Base2 {..} Derived DerivedObject; Base2 *BasePtr2 = &DerivedObject; Above requires compiler to set the address to &DerivedObject + size of Base1 subobject If Base1 *BasePtr1 = &DerivedObject is written, the compiler does not need to intervene
29
The Designer’s Problem
A tangled hierarchy We need to find out the value of an attribute for an object which has inherited from multiple classes If we cannot find that attribute, we need to traverse the hierarchy up to find the value. In a case of multiple-inheritance, it is possible to get multiple values for the same attribute
30
The tangled hierarchy Birds Can Fly! PetBirds Ostreaches Can not Fly!
MyBirds
31
Deriving a Class from an Already Derived Class
The C++ object model implementation is such that the further derived class will contain all ancestors within its body The suboject concept
32
Virtual Base Class A specific type of further inheritance
class Base derived into two different classes Derived1 and Derived2. new class DDerived multiply derived from Derived1 and Derived2 Now there are two copies of Base copied in DDerived; one from Derived1 and the other from Derived2.
33
Multiple Further Derivation of Base
We obviously need the scope resolution operator to say either “I need a base class object derived from Derived1” or “I need a base class object derived from Derived2”. DD.Derived1::BaseInt = 0; DD.Derived2::BaseInt = 10
34
Virtual to have a single copy
qualification with a base class name is a serious problem in some cases. To solve this problem we have to precede the first derivation by keyword virtual We can either write virtual public or public virtual; there is no difference between them Now the BaseInt is a single one and there is no ambiguity
35
Example Whenever compiler finds virtual keyword with derivation, it would ensure that two instances of the same base class are not inherited into the class derived from the derived class of base. It is a challenge for the compiler to provide single instance of a class from two instances available and still provide compatibility when one type of object is assigned to another;
36
The Problem
37
The virtual base class structure
Single copy of base class is available now. Compiler, because virtual keyword is found in Derived1 and Derived2 inheritance definitions, cares to include a single copy. DDerived Class Original members Derived1 Class original members Base Class Derived2 Class original members The DDerived class
38
Abstract Classes The classes without any objects are known as abstract classes. These classes are usually outcome of generalization process which finds out common elements of the classes of interest and stores them in a base class.
39
Abstract Classes Whenever a designer has to model an abstract concept which cannot have objects, then abstract classes are handy.
40
Abstract Classes Consider the class shape. It can be inherited to rectangle, ellipse, which in turn can be inherited to parallelograms into squares and circles. Except shape, all other can have objects of them (in real sense). Another example is of class Furniture.
41
Applications of Constructors and Destructors
If there is a base class constructor available, the derived class must define one constructor for itself. In case of a default constructor though the situation is different the compiler will define one default constructor for us when we do not do so in derived
42
Applications of Constructors and Destructors
When we have defined class1:public class2, public class3, public class4 … , classN then the constructor for class2 is called first, then the constructor for class 3 is called The call to base class constructors is to be defined outside the body of the constructor, in the member initialization list. Virtual base class is an exception
43
Applications of Constructors and Destructors
The destructors of the base classes are called exactly in reverse order of their initialization when the derived object is destroyed.
44
Composite Objects (Container Objects)
Sometimes, the object itself contains some other objects as members. Such objects are known as composite objects This is the part of relationship unlike isa relationship discussed earlier we may not need to call the constructor of the component classes whenever we need to call the constructor of the container class
45
Containership and Inheritance
Nose (a triangle) is part of Face. Left and right eyes (circles) are again part of Face. But all instances of triangles and circles are not instances of Face as well. Both type of members are treated by C++ in a similar way (embedded in the target class) so they seem similar while programming.
46
Containership and Inheritance
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.