Академический Документы
Профессиональный Документы
Культура Документы
www.lsp4you.com
Inheritance: Extending
Classes
Learners Support Publications
www.lsp4you.com
Introduction
Reusability is an important feature of
OOP.
Introduction
continue …
Introduction
continue …
Single Inheritance
A derived class with only one base
class.
A
B
Learners Support Publications
www.lsp4you.com
Multiple Inheritance
A derived class with several base
classes.
A B
C
Learners Support Publications
www.lsp4you.com
Hierarchical Inheritance
A traits of one class may be inherited
by more than one class.
B C D
Learners Support Publications
www.lsp4you.com
Multilevel Inheritance
The mechanism of deriving a class
from another derived class.
C
Learners Support Publications
www.lsp4you.com
Hybrid Inheritance
The mechanism of deriving a class by
using a mixture of different methods.
B C
D
Learners Support Publications
www.lsp4you.com
Derived Classes
A derived class can be defined by
specifying its relationship with the
base class in addition to its own
details.
class derived-class-name : visibility-mode base-class-name
{
………//
………// members of derived class
………//
};
Learners Support Publications
www.lsp4you.com
Derived Classes
continue …
Derived Classes
continue …
Derived Classes
continue …
Derived Classes
continue …
Inheritance
In inheritance, some of the base class data
elements and member functions are inherited
into the derived class.
Single Inheritance
B
Learners Support Publications
www.lsp4you.com
class alpha
{
private : // optional
……… // visible to the member within its class
………
protected :
……… // visible to member functions
……… // of its own and immediate derived class
public :
……… // visible to all functions
……… // in the program
};
Learners Support Publications
www.lsp4you.com
Protected Member
continue …
Protected Derivation
It is possible to inherit a base class in
protected mode – protected derivation.
Protected
Private Private
Protected Protected
Public Public
Class X : public D1, protected D2
Private
Protected
Public
Learners Support Publications
www.lsp4you.com
Visibility
protected
fy2
fx2 data
friend of X
class Z
Function 1
fz1 friend of X
fz2
Inherited from X
Learners Support Publications
www.lsp4you.com
Multilevel Inheritance
The class A serves as a
base class for the derived
class B, which in turn Base Class A
serves as a base class for
the derived class C.
Intermediate
Base Class B
Class B provides a link for
the inheritance between A
and C.
Derived Class C
The chain ABC is known as
inheritance path.
Learners Support Publications
www.lsp4you.com
Multilevel Inheritance
continue …
Multiple Inheritance
A class can inherit the B-1 B-2
attributes of two or
more classes.
D
Multiple inheritance
allows us to combine
the features of several It is like a child
existing classes as a inheriting the physical
starting point for features of one parent
defining new classes. and the intelligence of
another.
Learners Support Publications
www.lsp4you.com
Multiple Inheritance
continue …
In Multiple Inheritance
Learners Support Publications
www.lsp4you.com
In Single Inheritance
Learners Support Publications
www.lsp4you.com
Hierarchical Inheritance
Inheritance support
hierarchical design of a
program. Account
Hybrid Inheritance
Applying Two or more
types of inheritance student
together.
test sports
result
Learners Support Publications
www.lsp4you.com
base class.
result
Learners Support Publications
www.lsp4you.com
inherited from
student.
Learners Support Publications
www.lsp4you.com
class student
{
………
}; student
class test : virtual public student
{
………
}; test sports
class sports : public virtual student
{
………
}; result
class result : public test, public sports
{
………\
};
Learners Support Publications
www.lsp4you.com
Abstract Classes
An abstract class is one that is not used to create
objects.
}
Learners Support Publications
www.lsp4you.com
class gamma
{
………
alpha a;
beta b;
public:
gamma(arglist): alpha(arglist1), beta(arglist2)
Thank You