Prepared By: SELVIN JOSY BAI. S
Prepared By: SELVIN JOSY BAI. S
Prepared By: SELVIN JOSY BAI. S
S
Itis the capability of one
class to inherit properties
from another class.
The technique of building
new classes from the
existing classes is called
inheritance.
Base Class /
Super Class
Derived from
Derived Class /
Sub Class
It is the class whose
properties are inherited by
another class.
It is also called Super
Class.
It is the class that inherit
properties from base class(es).
It is also called Sub Class.
It inherits all the properties of
the base class and can add
additional features to the
derived class.
CODE REUSABILITY
EASY
TO IMPLEMENT REAL
WORLD MODELS
TRANSITIVE NATURE
It is any of the access labels:
private, public or protected.
It defines the accessibility of the
members of the base class within
the derived class.
If the visibility mode is not
specified, it will be taken as
private by default.
Privatemembers can never be
inherited. Only the public and
protected members can be
inherited to the derived class. This
is the difference between the
private and protected members.
VISIBILITY MODES
Derived class visibility
Base Class
Visibility Public Private Protected
derivation derivation derivation
PRIVATE YES NO NO
X BASE CLASS
Y DERIVED CLASS
class DerivedClassName : [visibility mode]
BaseClassName
{
//DataMembers and MemberFunctions;
}
Example:
class Automobile : public Vehicle
{
//DataMembers and MemberFunctions;
}
Derivation of a class from SEVERAL
(TWO OR MORE) base classes is
called MULTIPLE Inheritance.
In the figure class Z is derived from
both the classes X & Y.
X Y BASE CLASSES
Z DERIVED CLASS
class DerivedClassName : [visibility mode]
BaseClassName1, [visibility mode] BaseClassName1
{
//DataMembers and MemberFunctions;
}
Example:
class CHILD : public FATHER, public MOTHER
{
//DataMembers and MemberFunctions;
}
Derivation of SEVERAL classes from
SINGLE base class is called
HIERARCHICAL Inheritance.
In the figure the classes Y & Z is
derived from the same class X.
X BASE CLASSES
Y Z DERIVED CLASS
class DerivedClassName1 : [visibility mode] BaseClassName
{
----------;
}
Example: Example:
class Y : public X class Z : public X
{ {
---------; ---------;
} }
When a sub class is
derived from a base X
class which itself is
derived from another
class, it is known as
MULTILEVEL Inheritance. Y
In the figure the class Z is
derived from class Y,
which is a derived class
that is inherited from the Z
class X.
class DerivedClassName1 : [visibility mode] BaseClassName
{
----------;
}
Example: Example:
class Y : public X class Z : public Y
{ {
---------; ---------;
} }
Derivation of a class
involving more than W
one form of Inheritance
is known as HYBRID
inheritance. X Y
As it is the derivation of
a class from other
derived classes, which Z
are derived from the
same base class.
The derived class need have a
constructor as long as the base
class has a no-argument
constructor.
If the base class has constructors
with arguments, then it is mandatory
for the derived class to have a
constructor and pass the arguments
to the base class constructor.
When an object of a derived class is
created, the constructor of the base
class is executed first and later the
constructor of the derived class.
Unlike constructors, destructors in
the class hierarchy are invoked in
the reverse order of the constructor
invocation.
It is one that has no instances and is
not designed to create objects.
It is only designed to be inherited
from.
It specifies an interface at a certain
level of inheritance and provides a
framework or skeleton, upon which
other classes can be built.
When classes are derived in the
form of hybrid inheritance, there can
be a problem by which multiple
copies of the base class members
come in the lowest level derived
class through the various
intermediate subclasses. Here
comes the virtual base class for
rescue.
class A
{ public:
int a;
};
Class B
class B : public A
{ public: contains
int b; a and b
};
class C : public A
Class C
{ public: contains
int c; a and c
};