0% found this document useful (0 votes)
6 views

C++ Polymorphism-Abstract classes- Interfaces-Data Encapsulation in C++

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C++ Polymorphism-Abstract classes- Interfaces-Data Encapsulation in C++

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

C++ Polymorphism

Abstract classes- Interfaces


Data Encapsulation in C++
C++ Polymorphism
➢The word polymorphism means having many forms. Typically,
polymorphism occurs when there is a hierarchy of classes and they
are related by inheritance.
➢C++ polymorphism means that a call to a member function will cause
a different function to be executed depending on the type of object
that invokes the function.
➢Consider the following example where a base class has been derived
by other two classes −
Why Polymorphism?
Polymorphism allows us to create consistent code. For example,

Suppose we need to calculate the area of a circle and a square. To do


so, we can create a Shape class and derived two classes Circle and
Square from it.

In this case, it makes sense to create a function having the same name
calculateArea() in both the derived classes rather than creating
functions with different names, thus making our code more consistent.
outputs

The reason for the incorrect output is that the call of the
function area() is being set once by the compiler as the version
defined in the base class. This is called static resolution of the
function call, or static linkage - the function call is fixed before
the program is executed. This is also sometimes called early
binding because the area() function is set during the
compilation of the program.

But now, let's make a slight modification in our program and


precede the declaration of area() in the Shape class with the
keyword virtual so that it looks like this −
Virtual Function
A virtual function is a function in a base class that is declared using the
keyword virtual. Defining in a base class a virtual function, with another
version in a derived class, signals to the compiler that we don't want
static linkage for this function.

What we do want is the selection of the function to be called at any


given point in the program to be based on the kind of object for which
it is called. This sort of operation is referred to as dynamic linkage, or
late binding.
Virtual Functions
• It is possible that you want to include a virtual function in a base class so
that it may be redefined in a derived class to suit the objects of that class,
but that there is no meaningful definition you could give for the function in
the base class.
• The purpose of an abstract class is to provide the Desired base class form
which will be inherited by other classes.

Abstract class cannot be used to instantiate object.

We can change the virtual function area() in the base class to the following −
Abstract classes- Interfaces
• The C++ interfaces are implemented using abstract classes and these
abstract classes should not be confused with data abstraction which is
a concept of keeping implementation details separate from
associated data.
• A class is made abstract by declaring at least one of its functions
as pure virtual function. A pure virtual function is specified by placing
"= 0" in its declaration as follows −
• An interface describes the behavior or capabilities of a C++ class
without committing to a particular implementation of that class.
Abstract Class Example
Data Encapsulation in C++
• All C++ programs are composed of the following two fundamental
elements −
• Program statements (code) − This is the part of a program that
performs actions and they are called functions.
• Program data − The data is the information of the program which
gets affected by the program functions.
• Data encapsulation is a mechanism of bundling the data, and the
functions that use them and data abstraction is a mechanism of
exposing only the interfaces and hiding the implementation details
from the user.
C++ supports the properties of encapsulation and data hiding through
the creation of user-defined types, called classes. We already have
studied that a class can contain private,
protected and public members. By default, all items defined in a class
are private. For example −
The variables length, breadth, and height
are private. This means that they can be
accessed only by other members of the
Box class, and not by any other part of
your program. This is one way
encapsulation is achieved.

You might also like