Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
Session13 Draft

Session13 Draft

Ratings: (0)|Views: 6|Likes:
Published by api-3845857

More info:

Published by: api-3845857 on Oct 18, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

Session 13
Inheritance and Derived classes
Contents
Objectives
Introduction

13.1 What is Inheritance
13.1.1 How to declare a derived class
13.1.2 The Protected Keyword
13.2.3 A demonstration on creating a base class and derived classes

13.2 Constructors and destructors in inheritance
13.2.1 A demonstration on the use of constructors and destructors in inheritance
Summary
Objectives
After reading this lesson you should be able to:

\ue000Understand the concepts behind inheritance
\ue000Understand how to derive one class from another.
\ue000Understand the correct use of the \u2018protected\u2019 access specifier.
\ue000Understanding ho to use constructors and destructors in inheritance

Introduction

In the previous sessions you learnt how to declare classes and create objects using them. In all the sessions we have created only a single class. In OOP it is possible to declare many classes in a single programme.

Previously class data were declared as private and public. This session introduces the
protected keyword in more detail with examples.
13.1 What is Inheritance

Inheritance is a way of creating new classes using existing classes that has been already defined. The new classes that are created are called derived classes and the already existing class is called the base class. A base class will have more than one or more

1
derived class. The derived classes inherit all the characteristics (data member) and
behaviours (methods) of the base class.

Inheritance is sometimes called generalization, because the relationship between the base class and the derived classes represent a hierarchy between classes of objects. The concept of inheritance can be easily understood using simple real world examples.

Example 13.1: If there\u2019s a base class called \u2018birds\u2019, derived classes like crow, parrot,
eagle etc can be created.
Figure 13.1 \u2013 Base class and derived classes

In example 13.1, in order to create data members it is vital to consider the characteristics of a bird. All the birds have feathers and beaks. Hence two attributes of the birds class can be \u2018feathers\u2019 and \u2018beaks\u2019. If birds like crow, parrot or eagle are derived from the birds class they inherit those attributes as well. As a result these three derived classes have the characteristics or data members, \u2018feathers\u2019 and \u2018beaks\u2019. In the real world also it is obvious that those birds have feathers and beaks.

In considering the behavior, birds can fly; they eat using the beak etc. These can be defined as methods of the base class. Then all the derived classes inherit these methods and the programmer doesn\u2019t have to state explicitly that the crow, parrot and the eagle fly, because it inherit that from the bird class.

The derived classes can be further divided. There are several types of parrots. For example Eclectus parrot, African gray parrot, Hawkheaded parrot etc. They also have all the characteristics of a parrot but they differ in other aspects like color, type of food they eat etc. Then the classes like Eclectus parrot, African gray parrot, Hawkheaded parrot becomes the derived classes of parrot and the parrot class in the base class for these derived classes.

2
Birds
Crow
Parrot
Eagle
Base class
Derived classes
In a similar manner, using the crow class and the eagle class, more derived classes can be
created since there are different types of crows and eagles.
13.1.1 How to declare a derived class
A derived class must be declared before it is used. Therefore it is vital to specify what
class it derives from. The general format of declaring a derived class is:
class derived_class_name : type_of_derivation base_class_name {
//member data
::
//methods
::
};

The keyword \u2018class\u2019 followed by the name of the derived class, needs to be specified first, just like declaring a normal class. Then a colon after the class name should be used followed by type of derivation (public, private or protected) and then the class from which it derives.

Example 13.2 : class Parrot : public bird{
//member data
//methods
};
13.1.2 The Protected Keyword

When the data members are declared using the protected access specifier, the members are accessible from within the methods of the same class and from the members of their derived classes. Protected data members and functions are fully visible to derived classes, but are otherwise private.

The below given table summarizes different types of access specifiers according to which
methods can access them:
Access
public protected private
members of the same class yes
yes
yes
members of derived classes yes
yes
no
not members
yes
no
no
Table 13.1 \u2013 Access specifier
3

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->