More Related Content
Similar to Aae oop xp_09 (20)
Aae oop xp_09
- 1. Inheritance
Objectives
In this lesson, you will learn to:
Appreciate the following terms:
Relationship
Generalization
Recognize the need for generalization
Describe different types of relationship with examples
from the real world:
Inheritance
Composition
Utilization
Instantiation
©NIIT OOPS/Lesson 9/Slide 1 of 29
- 3. Inheritance
Relationships
Are specified among classes based on the behavior of
each class
Exist between classes because of two reasons:
To indicate some sort of sharing
To indicate some kind of a connection
Are of four types:
Inheritance
Composition
Utilization
Instantiation
©NIIT OOPS/Lesson 9/Slide 3 of 29
- 4. Inheritance
Inheritance Relationship
Between classes or objects means an object or
a class inherits a set of attributes from another
class
Example:
M AM M ALS
DO G S C ATS HUM ANS
L IO N S T IG E R S LEO PARDS
©NIIT OOPS/Lesson 9/Slide 4 of 29
- 5. Inheritance
Inheritance Relationship (Contd.)
Superclass
Is a class from which another class inherits a set of
attributes
Subclass
Is a class that inherits a set of attributes from
another class
©NIIT OOPS/Lesson 9/Slide 5 of 29
- 6. Inheritance
Inheritance Relationship (Contd.)
The types of inheritance are:
Single inheritance
Is displayed when a class inherits attributes from a
single class
Multiple inheritance
Is displayed when a class inherits attributes from
two or more classes
©NIIT OOPS/Lesson 9/Slide 6 of 29
- 8. Inheritance
Generalization
Is done by clubbing the structure and behavior that is
common to all the classes
Is represented by the superclass
Is implemented by using the Abstract class, which
Is not an existent entity
Is a base from which other classes inherit
attributes
©NIIT OOPS/Lesson 9/Slide 8 of 29
- 9. Inheritance
Just a Minute…
Given the following objects, build a hierarchy, and
generalize wherever possible: Mixer, VCR, Color
television, Washing machine, Stereo.
©NIIT OOPS/Lesson 9/Slide 9 of 29
- 10. Inheritance
Composition Relationship
Occurs when one class is made up of another
Example:
Relationship between a car and its parts like engine,
doors, steering wheel, gear box, seats, and so on
©NIIT OOPS/Lesson 9/Slide 10 of 29
- 11. Inheritance
Just a Minute…
State the relationship between the following class pairs:
1. Television – Speaker
2. Mammal – Tiger
3. Garment – Shirt
4. Cup – Tea
5. Computer – Microprocessor
©NIIT OOPS/Lesson 9/Slide 11 of 29
- 13. Inheritance
Just a Minute…
Identify the classes and the utilization relationships of a
departmental store from the following details:
There are several counters, each one manned by a
single salesperson selling a specific product. The
customer approaches any counter, depending on which
product he or she wishes to purchase. The salesperson
hands him or her the product and accepts payment.
©NIIT OOPS/Lesson 9/Slide 13 of 29
- 15. Inheritance
Uses of Inheritance
Reduces redundancy in code
Enables easy maintenance of code
Extends the functionality of an existing class by
adding functions to the subclass
©NIIT OOPS/Lesson 9/Slide 15 of 29
- 16. Inheritance
Sequence of Invoking Constructors and
Destructors
Constructors are called in the order of Base - to -
Derived
Destructors are called in the order of Derived - to -
Base
©NIIT OOPS/Lesson 9/Slide 16 of 29
- 17. Inheritance
Base Class Initialization
Is required before the member object initialization of
the derived class is done
class Line : public Figure
{
Point p2;
public:
Line(int x1 = 0,int y1 = 0,int
x2 = 0,int y2 = 0): p2(x2, y2),
Figure(x1, y1) {} //Member
Initialization List
};
©NIIT OOPS/Lesson 9/Slide 17 of 29
- 19. Inheritance
Public Access Specifier
Defines that all the:
private members of a base class remain
private in the object
protected members remain protected
the public members remain public
©NIIT OOPS/Lesson 9/Slide 19 of 29
- 20. Inheritance
Protected Access Specifier
Defines that all the:
the private members of a base class remain
private in the object
the protected members remain protected
but all the public members of the base class
become protected
©NIIT OOPS/Lesson 9/Slide 20 of 29
- 21. Inheritance
Private Specifier
Defines that all the:
private members of a base class remain
private in the object
public and protected members in the base
class become private
Depicts a composition relationship between the base
and the derived classes
©NIIT OOPS/Lesson 9/Slide 21 of 29
- 22. Inheritance
Implementing Inheritance in C++ by Deriving
Classes From the Base Class
Syntax:
class base_class
{
…
};
class derived_class : access-specifier
base_class
{
...
};
©NIIT OOPS/Lesson 9/Slide 22 of 29
- 23. Inheritance
Implementing Inheritance in C++ by Deriving
Classes From the Base Class
Sequence of invoking constructors and destructors
Constructors are called in the order of Base –
to – Derived
Destructors are called in the order of Derived – to
– Base
Access specifiers of derivation
The public access specifier
The protected access specifier
The private access specifier
©NIIT OOPS/Lesson 9/Slide 23 of 29
- 24. Inheritance
Problem Statement 7.D.1
Furniture and Fittings Company (FFC) deals with
custom-made furniture. Customers provide their
specifications for the furniture they want. To cope with
the volume of orders placed, FFC had decided to
computerize their order-processing system a few months
ago. The code should have representations for a
bookshelf and a chair. You have to develop the hierarchy
and the code.
©NIIT OOPS/Lesson 9/Slide 24 of 29
- 25. Inheritance
Problem Statement 7.P.1
Philip Anderson of Mega Technologies has written the
following code to implement a graphics package – to
draw simple graphic images. However, Philip has been
moved on to another project and you have been asked
to complete the code. Make necessary changes and
test the code.
©NIIT OOPS/Lesson 9/Slide 25 of 29
- 26. Inheritance
Summary
In this lesson, you learned that:
Relationships exist between two classes mainly for
two reasons:
A class-class relationship, which is a relationship
between two classes, indicating some sort of
sharing
A class-class relationship, indicating some kind of
a connection
©NIIT OOPS/Lesson 9/Slide 26 of 29
- 27. Inheritance
Summary (Contd.)
The four kinds of relationships that can be identified
are:
Inheritance
Composition
Utilization
Instantiation
Generalization means that multiple classes inherit
from the same superclass
When a class inherits a set of attributes from another
class, the class that inherits is called the subclass and
the class from which it inherits is called the superclass
©NIIT OOPS/Lesson 9/Slide 27 of 29
- 28. Inheritance
Summary (Contd.)
An abstract superclass is a conceptual class that does
not exist in the real world but acts as a base from
which other classes inherit properties
When a class inherits attributes from two or more
classes, it is said to be showing multiple inheritance
Composition relationship occurs when one class is
made up of another
Utilization relationships exist between two or more
classes, which make use of other classes
Instantiation relationship, as the name suggests, is a
relationship between a class and an instance of that
class
©NIIT OOPS/Lesson 9/Slide 28 of 29
- 29. Inheritance
Summary (Contd.)
In a class hierarchy, constructors are called in the
order of Base – to – Derived and destructors are
called in the order of Derived – to – Base
©NIIT OOPS/Lesson 9/Slide 29 of 29