0% found this document useful (0 votes)
10 views11 pages

Polymorph Is M

The document discusses inheritance and polymorphism in object-oriented programming, emphasizing how inheritance allows for a family of classes with a common interface, while polymorphism enables type-independent manipulation of these classes. It highlights the importance of using pointers or references for polymorphic behavior, the implications of non-virtual destructors, and best practices for designing base classes. Key points include the need for virtual destructors and the avoidance of passing by value to maintain polymorphism.

Uploaded by

zainshahad005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

Polymorph Is M

The document discusses inheritance and polymorphism in object-oriented programming, emphasizing how inheritance allows for a family of classes with a common interface, while polymorphism enables type-independent manipulation of these classes. It highlights the importance of using pointers or references for polymorphic behavior, the implications of non-virtual destructors, and best practices for designing base classes. Key points include the need for virtual destructors and the avoidance of passing by value to maintain polymorphism.

Uploaded by

zainshahad005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Inheritance and Polymorphism

• Inheritance allows us to define a family of classes


that share a common interface.
• Polymorphism allows us to manipulate objects of
these classes in a type-independent way.
• We program the common interface through a
pointer or reference to an (abstract) base class. The
actual operation to invoke is not determined until
run-time based on the specific type of object
actually addressed.
1
C++ and Polymorphism
• Polymorphism is the ability of a base class
pointer (or reference) to refer transparently
to any of its derived classes.
• Polymorphism (and dynamic binding) are
supported only when we use pointers (or
references)

2
Inheritance
• A base class pointer can point to an object of a
derived class
– Derived class object “is-a” base class object
– But can’t use pointer to call methods only defined in
the derived class.
• A derived class pointer CANNOT point to the
base class
– The base class doesn’t have any of the extensions
provided by the derived class

3
Calling virtual methods from
within other methods
• Suppose virtual drawMe( ) is added to
Shape and inherited by Rectangle without
being overridden.

void Shape::drawMe ( void)


{
cout << “drawing: “ << endl;
draw( );
} 4
Which draw( ) gets called
From within drawMe( ) with this code?
Rectangle r1;
r1.drawMe ( );
The Rectangle version of draw( ), even though
drawMe( ) was only defined in Shape. Why?
Because inside of drawMe( ), the call to draw( ) is
really this->draw ( ); and since a pointer is used,
we get the desired polymorphic behavior.
5
Don’t redefine objectID( )
Note that neither Circle nor Rectangle redefined the objectID( )
method which was a nonvirtual function. But what if they had?
Suppose the Rectangle class had it’s own version of objectID().
Consider this code:
Rectangle R; // a Rectangle object
Shape *pS = &R; // base class pointer to a Rectangle
Rectangle *pR = &R; // derived class pointer to a Rectangle

what is the output of the following?


pS -> objectID ( );
pR -> objectID ( );
Bottom Line – don’t override nonvirtual functions 6
Old code and new code
• In the procedural world (like that of C), it’s easy for
new code to call old code using functions.
However, it’s difficult for old code to call new code
unless it’s modified to know about the new code.
• In the OO world, old code (polymorphic functions)
can dynamically bind to new code. This occurs
when a new derived class is passed (by pointer or
reference) to the polymorphic function. The
polymorphic function needs no modification.

7
Don’t Pass by Value
A function which has a base class parameter passed by
value should only be used with base class objects –
for two reasons
1. The function isn’t polymorphic. Polymorphism
only occurs with parameters passed by pointer or
reference
2. Even though a derived class object can be passed to
such a function (a D is-a B), none of the derived
class methods or data members can be used in that
function. This is a phenomenon called “member
slicing”.
8
Polymorphism and Destructors
• A problem – if an object (with a non-virtual
destructor) is explicitly destroyed by applying the
delete operator to a base class pointer, the base class
destructor is invoked.
Circle C;
Shape *sp = &C;
delete sp; // calls Shape’s destructor
// if the destructor is not
// virtual
• So what ??

9
Polymorphism and Destructors
• Solution -- Declare a virtual destructor for
any base class with at least one virtual
function.
• Then, when the derived class’s destructor is
invoked, the base class destructor will also
be invoked automatically

10
Designing A Base Class with
Inheritance and Polymorphism
in mind
For the base class
1. Identify the set of operations common to all
the children
2. Identify which operations are type-
independent (these become (pure) virtual to
be overridden in derived classes)
3. Identify the access level (public, private,
protected) of each operation
For a more complete discussion, see “Essential C++”, Stanley Lippman (section 5.4)
11

You might also like