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

EEN-103 Programming in C++: Dynamic Binding and Virtual Functions Polymorphism

1. The document discusses object-oriented programming concepts in C++ including polymorphism, virtual functions, abstract classes, and dynamic binding. 2. Polymorphism allows functions to have different behaviors depending on the type of object they refer to, and is implemented using virtual functions. 3. Virtual functions allow for dynamic/late binding at runtime rather than static binding at compile time. Abstract classes can contain pure virtual functions to define common interfaces for inheritance but cannot be used to create objects themselves.

Uploaded by

Shivam
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)
63 views

EEN-103 Programming in C++: Dynamic Binding and Virtual Functions Polymorphism

1. The document discusses object-oriented programming concepts in C++ including polymorphism, virtual functions, abstract classes, and dynamic binding. 2. Polymorphism allows functions to have different behaviors depending on the type of object they refer to, and is implemented using virtual functions. 3. Virtual functions allow for dynamic/late binding at runtime rather than static binding at compile time. Abstract classes can contain pure virtual functions to define common interfaces for inheritance but cannot be used to create objects themselves.

Uploaded by

Shivam
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/ 46

EEN-103

PROGRAMMING IN C++

Object Oriented Programming Concepts: Data hiding, abstract data types,


classes, access control; Class implementation-default constructor, constructors,
copy constructor, destructor, operator overloading, friend functions; use of
pointers in linked structures; Object oriented design (an alternative to functional
decomposition) inheritance and composition; Dynamic binding and virtual
functions; Polymorphism; Dynamic data in classes.
Virtual function, Abstract Classes
and
Polymorphism
Polymorphism
• poly’ originated from the greek word
meaning many
•‘morphism’ from a greek word
meaning form
• ‘polymorphism’ means many forms

•In OOP, polymorphism refers to identically


named methods (member functions) that
have different behavior depending on the
type of object they refer
• Polymorphism is the process of defining a
number of objects of different classes into
a group
• and call the methods to carry out operation
of the objects using different function calls.
• In other words, polymorphism means ‘to carry
out different processing steps by functions
having same messages’.

• The keyword virtual is used to perform


polymorphism concept in C++
• Choosing a function in normal way, during
compilation time is called early binding or
static binding or static linkage

• By default , C++ follows early binding.


Polymorphism with pointers
• Pointers are also central to polymorphism in C++.

• To enable polymorphism, C++ allows pointer in a


base class to point to either a base class object
or to any derived class object.
• An example to illustrate how a pointer is
assigned to point to object of the derived
class:
class Base_A{
……………..
……………..
};
class Derived_D : public base_A {
………………
………………
};
int main()
{
Base_A *ptr; // pointer to Base_A
Derived_D objd;
ptr= &objd; // indirect reference objd to the pointer
………………….
………………….
}
The pointer ptr points to the object of the derived class objd
Example:
class Base
{
public:
void show() { cout << ”Base” << endl; }
};
//******************************************************
class Derv1 : public Base
{
public:
void show() { cout << ”Derv1” << endl; }
};
//******************************************************
class Derv2 : public Base
{
public:
void show() { cout << ”Derv2” << endl; }
};
int main()
{
Derv1 obj1; //constructor 1
Derv2 obj2; //constructor 2
Base *objptr; //access with pointers
objptr = &obj1;
objptr -> show(); //print „Base”, base class function called
objptr = &obj2;
objptr -> show(); //print „Base”, base class function called
return 0;
}
Function show() in the base class is non- virtual
Non-Virtual Pointer Access
Virtual Functions
Virtual function is one that does not really
exist but it appears real in some parts of a
program.
Syntax
class user_defined_name{
private:
-----------
-----------
public:
virtual return_type function_name1(arguments);//function-body outside
virtual return_type function_name2(arguments);
virtual return_type function_name3(arguments);
------------
------------
};
• To make a member function virtual, the
keyword virtual is used in the methods while
it is declared in the class definition but not in
the member function definition.
class Base
{
public:
virtual void show() { cout << ”Base” << endl; }
};
//******************************************************
class Derv1 : public Base
{
public:
void show() { cout << ”Derv1” << endl; }
};
//******************************************************
class Derv2 : public Base
{
public:
void show() { cout << ”Derv2” << endl; }
}
int main()
{
Derv1 obj1; //constructor 1
Derv2 obj2; //constructor 2

Base *objptr; //access with pointers

objptr = &obj1;
objptr -> show(); //print „Derv1”, derived class function called

objptr = &obj2;
objptr -> show(); //print „Derv2”, derived class function called

return 0;
}
Virtual Pointer Access
• Example:
class sample {
private:
int x;
float y;
public:
virtual void display();
virtual int sum();
};
Late Binding
• Choosing functions during execution time is
called late binding or dynamic binding or
dynamic linkage

• It provides increased power and flexibility

• Late binding is implemented through virtual


functions

• An object of a class must be declared either as a


pointer to a class or as a reference to a class
Example:
class baseA {
protected:
int x;
int y;
public:
virtual void display() { cout<<"Base class virtual display()"<<endl;}
int sum() { return (x+y);}; // non-virtual
void setup (int a, int b) { x= a; y= b;} // non-virtual
};
class derivedD : public baseA
{
public:
void display(); // virtual
int sum() { return (2*x+2*y);};
};
int main()
{
baseA *ptr;
derivedD objd;
ptr = &objd;
……………………
……………………
ptr -> display(); // Run time binding
ptr->setup(); // compile time binding
cout<<ptr -> sum(); // compile time binding
}
• The keyword virtual must be followed by a
return type of a member function if a run time
is to be bound

• Otherwise the compile time binding will be


effected as usual

• In above example, only the display() function


has been declared virtual in the base class
whereas the sum() and setup are non-virtual
• Even though the message is given from
pointer of the base class to the objects of the
derived class:

• It will not access sum() function of the derived


class as it has been declared non-virtual

• sum() function compiles only the static binding


Abstract Class
and
Pure Virtual Function
Example:
class Base //abstract base class
{
public:
virtual void show() = 0; //pure virtual funtion
};
//******************************************************
class Derv1 : public Base
{
public:
void show() { cout << ”Derv1” << endl; }
};
//******************************************************
class Derv2 : public Base
{
public:
void show() { cout << ”Derv2” << endl; }
};
int main()
{
Derv1 obj0; //constructor 1
Derv2 obj1; //constructor 2

Base *arr[2]; //access with pointers, object is not created!

arr[0] = &obj0;
arr[1] = &obj1;

arr[0] -> show();


arr[1] -> show();

cin.get()
return 0;
}
Summary of Virtual functions
• used to allow polymorphism
• works for inherited classes
• declare a function as virtual in base class
• then override this function in each derived class
• while invoking a virtual function through a base-class pointer, the
response depends on the actual object pointed by the pointer
Example
• Consider an inheritance hierarchy of shapes.
• base class is shape
• it has a virtual function as
• virtual void draw() const;
• the function draw exists in each of the derived classes such as
circle, square, pentagon, triangle etc.
• the function is defined differently in each of the derived class but
has same signature
• now consider a declaration
Virtual functions
shape *sptr;
• any call of the type, sptr-> draw() will be decided according to
the object pointed
• it is a case of dynamic binding or late binding
• if draw() is called via an object then it is a case of static
binding e.g.
square sobj;
sobj.draw();
• polymorphism refers to the possibility of different objects
(related via inheritance) to respond differently to same
member function call (the member function must have been
declared virtual in base class)
• overriding a non-virtual function of base class does not lead
to polymorphic behaviour
Abstract classes
Abstract classes
• a class from which we do not plan to make any objects
• meant for inheritance only
• may be called abstract base class
• classes from which objects are to be created - concrete
classes
• abstract classes are too general to define real objects
• they only provide a common root for making an inheritance
family of some concrete classes
• a class is made abstract by declaring one or more of its virtual
functions as pure virtual such as
virtual float area() const = 0;
Example
employee e, *eptr;// base class
eptr = &e;
prof p, *pptr;// derived class
pptr = &p;
eptr->print(); //base class print used;
pptr->print(); //derived class print used
eptr = &p;
eptr->print(); //derived class print used if declared virtual in base class
Constructors and destructors
• constructors cannot be declared as virtual
• destructors have different names in different classes of a
inheritance hierarchy. How to get polymorphic effect for them?
• declare the destructor in base class as virtual
• deleting a base class pointer pointing to a derived object will
invoke the destructor of the appropriate derived class. After
the derived class destructor, the base class destructor also
gets executed automatically

You might also like