Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
Inheritance and
Polymorphism
Inheritance
• Derive a new class (subclass) from an existing
class (base class or superclass).
• Declaring subclasses
class B extends A
{ . . . }
– means class B is a specialization of class A
– the "is a" relationship exists
– a B object is an A object
A
B
"is a"
increasingly
general
increasingly
specialized
Inheritance
• Other names:
– superclass also called "parent class"
– subclass also called "child class"
• These names help understand concept of
inheritance
• Child class inherits characteristics of parent
class
– attributes
– methods
C++ Example
class A
{
public:
integer d;
};
class B : public A
{
public:
};
C++ Example
• The class B in the example does not have any direct data
member does it?
• Yes, it does. It inherits the data member d from class A.
• When one class inherits from another, it
acquires all of its methods and data.
• We can then instantiate an object of class B and call
into that data member.
void func()
{
B b;
b.d = 10;
};
Example: A Trip to Aviary
• Consider a collection of birds
which have different properties
– name
– color (some of the same name
are of different colors)
– they eat different things
– they make different noises
– some make multiple kinds of
sounds
Heirarchy Bird
call: ?
color:?
food:?
movement:?
WalkingBird
call: ?
color:?
food:?
movement:walked
FlyingBird
call: ?
color:?
food:?
movement:flew
Goose
call: honk
color: gray
food: bugs
Ostrich
call: neek-neek
color: brown
food: grass
Parrot
call: Squawk
color:?
food: fruit
Owl
call:?
color:?
food:mice
TalkingParrot
. . .
Bird class
• Note Bird class is a super class, previous fig
• All subclasses are derived from this bird class.
• Attributes common to all birds
– color
– food
– movement
Inheritance
• When we say …
class TalkingParrot extends Parrot
{ … }
– then a TalkingParrot object inherits all
Parrot attributes
– (which, in turn, inherits both FlyingBird
and Bird attributes)
• In general, descendant classes inherit the
attributes of ancestor classes
Results of Inheritance
• Used to eliminate redundant coding
• When we send toString() message to a
Goose or Parrot or TalkingParrot
object
– none of these classes implement the
toString() method
– but … they inherit it from Bird
–toString() need not be redefined in the
subclasses.
Don’t
• Consider the declaration:
Bird abird = new Goose();
– this is legal
– a Goose object "is a" Bird object
• Contrast
Goose aGoose = new Bird("gray",
"walking", "bugs");
– this is NOT legal
– A Bird object is not necessarily a Goose object
Types of Inheritance
• Multiple Inheritance
Multiple inheritance
Multilevel Inheritance
Hybrid Inheritance
Polymorphism
Polymorphism
• polymorphism (from the Greek meaning
"having multiple forms") is the characteristic
of being able to assign a different meaning to
a particular symbol or "operator" in different
contexts.
• Polymorphism is about an objects ability to
provide context when methods or operators
are called on the object.
Example
• class A
{
public:
virtual void f()=0;
};
• class B
{
public:
virtual void f()
{std::cout << "Hello from B" << std::endl;};
};
Example
• class C
{
public:
virtual void f()
{std::cout << "Hello from C" << std::endl;};
};
Example
• If I have an object A, then calling the method
f() will produce different results depending on
the context, the real type of the object A.
• func(A & a)
{
A.f();
};
Overloading
• Ability of one function to perform different
tasks.
• Creating several methods with the same
name which differ from each other in the type
of the input and the output of the function.
• The overloaded function must differ by data
types.
Example
• main()
• { cout<<volume(10); cout<<volume(2.5,8);
cout<<volume(100,75,15); }
• // volume of a cube
int volume(int s)
{ return(s*s*s); }
• // volume of a cylinder
double volume(double r,int h)
{ return(3.14*r*r*h); }
• // volume of a cuboid
long volume(long l,int b,int h)
{ return(l*b*h); }
Overriding
•  overriding is a concept used in inheritance 
which involves a base class implementation of 
a method .
•  Then in a subclass, you would make another 
implementation of the method. 
• Here is one simple example
Example
• class Base
{
public:
virtual void DoSomething() {x = x + 5;}
private:
int x;
};
class Derived : public Base
{
public:
virtual void DoSomething() { y = y + 5; Base::DoSomething(); }
private:
int y;
}; 
Any Questions ?
If this presentation helped you, please visit our page 
facebook.com/baabtra and like it. 
Thanks in advance. 
 
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us

Inheritance and polymorphism