[go: up one dir, main page]

0% found this document useful (0 votes)
6 views21 pages

Polymorphism

The document provides an overview of polymorphism in object-oriented programming, particularly in C++. It explains concepts such as dynamic binding, virtual functions, and the use of base class pointers to invoke derived class methods. Additionally, it covers the implementation of virtual tables, the distinction between static and dynamic binding, and the significance of pure virtual functions and abstract classes.

Uploaded by

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

Polymorphism

The document provides an overview of polymorphism in object-oriented programming, particularly in C++. It explains concepts such as dynamic binding, virtual functions, and the use of base class pointers to invoke derived class methods. Additionally, it covers the implementation of virtual tables, the distinction between static and dynamic binding, and the significance of pure virtual functions and abstract classes.

Uploaded by

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

Object Oriented Programming

Lecture
Polymorphism
& Related Concepts
Polymorphism

Technical Definition
“ Polymorphism can be achieved through Dynamic binding.
binding.
Dynamic binding is enabled when a virtual function
function isis invoked
invoked
through
through aa derived
derived class object which is referred
referred indirectly
indirectlyby by
either a base class pointer
pointer or reference ””
or reference
Pointers to the base class of derived objects
Pointers to the base class of derived objects

class Parent {
• Inheritance implies an is-a relationship public:
between two classes void Hello() { cout << "Hi from a Parent!\n"; }
// ...
};

• When we create a Derived object, it contains class Child : public Parent {


a Base part (which is constructed first), and a public:
Derived part (which is constructed second) void Hello() { cout << "Hi from a Child!\n"; }
};

int main() {
Parent* P;
• C++ allow to set a Base pointer or reference Note: P can only calls
Child C;
to a Derived object. those members which
P = &C;
P->Hello(); are part of Parent class
}
Static binding vs. Dynamic Binding
Static binding (Compile-time binding )

•Static binding is to associate a function’s name with the entry


point of the function at compile time.

•Example:
#include <iostream>
using namespace std;
In C++, all non-virtual functions are
void sayHi() bound at compile-time (static).
{ However, virtual functions can
cout << "Hello, World!\n"; have dynamic binding
}
int main()
{
sayHi();}
Dynamic binding ( Run-time binding )

•Run-time binding is to associate a function’s name with the


entry point at run-time.

•C++ supports run-time binding through virtual functions.


Virtual Function

class Parent {
• A virtual function is a member public:
function that you expect to be virtual void Hello() { cout << "Hi from a
redefined in derived classes. Parent!\n"; }
// ...
};

• When you refer to a derived class class Child : public Parent {


public:
object using a pointer to the base virtual void Hello() { cout << "Hi from a
class, you can call a virtual function Child!\n"; }
for that object and execute the };

derived class's version of the int main() {


function. Parent* P;
Child C;
P = &C;
P->Hello();
}
Virtual Functions

• To declare a function virtual, we use the Keyword virtual.


class Parent {
public:
virtual void sayHi() { cout << "Just hi!\n"; }
};

• If the member function definition is outside the class, the keyword virtual must not
be specified again.
class Parent {
public:
virtual void sayHi();
};
virtual void Parent::sayHi() { // error
cout << "Just hi!\n";
}

• Virtual functions can not be stand-alone functions or static methods.


Virtual Functions
• A virtual function can be used same as non-virtual member functions.
class Parent {
public:
virtual void print() { cout << "Hello!\n"; }
};

int main() {
Parent obj;
obj.print();
}

• A virtual function can be inherited from a base class by a derived class, like
other class member functions.
class Parent {
public: int main() {
virtual void print() { cout << "Hello!\n"; Child obj;
} obj.print();
}; }
class Child : public Parent {};
Virtual Function

• To let derived classes have their own implementation for the virtual function, we
override base class virtual functions in derived class.

• In order for a derived class virtual function instance to override the base class virtual
function instance, its signature must match the base class virtual function exactly.

• The use of keyword virtual is optional in derived classes.

class Child : public Parent {


class Parent {
public:
public:
// overrides Shape::sayHi(), automatically virtual
virtual void sayHi() { cout << "Just hi!\n"; }
void sayHi() { cout << "Hi from a Child!\n"; }
};
};
Example

class Parent
{
public: int main() {
virtual void sayHi() { cout << "Just hi!\n"; } Parent* p;
}; int choice;
cout << "1 -parent, 2 – child_1, 3 –child_2\n ";
cin >> choice;
class Child_1 : public Parent
{ switch (choice)
public: {
virtual void sayHi() { cout << "Hi from a case 1: p = new Parent; break;
Child_1!\n"; } case 2: p = new Child_1; break;
}; case 3: p = new Child_2; break;
}
p->sayHi(); // dynamic binding of sayHi()
class Child_2 : public Parent delete p;
{ }
public:
virtual void sayHi() { cout << "Hi from a
Child_2!\n"; }
};
Example

class Parent
{
public: int main() {
virtual void sayHi() { cout << "Just hi!\n"; } Parent* p;
}; int which;
cout << "1 -shape, 2 -triangle, 3 -rectangle\n ";
class Child_1 : public Parent cin >> which;
{ switch (which)
public:
Polymorphism is thus implemented by virtual functions
{
and run-time binding mechanism in C++. A class is
virtual void sayHi() { cout << "Hi from a case 1: p = new Parent; break;
case 2: p = new Child_1; break;
Child_1!\n"; }
}; called polymorphic if it contains virtual functions.
case 3: p = new Child_1; break;
}
class Child_2 : public Parent p->sayHi(); // dynamic binding of sayHi()
{ delete p;
public: }
virtual void sayHi() { cout << "Hi from a
Child_2!\n"; }
};
Polymorphism

A typical scenario of polymorphism in C++:


• There is an inheritance hierarchy

• The first class that defines a virtual function is the base class

• Each of the derived classes in the hierarchy must have a virtual


function with same signature.

• There is a pointer of base class type; and this pointer is used to


invoke virtual functions of derived class.
Virtual Tables

C++ uses the virtual table (vtable) mechanism to implement the


dynamic binding of virtual functions.

• A class with virtual member functions has a virtual table which contains the
address of its virtual functions.
• An object of such a class has a pointer(vptr) to point to the virtual table of
the class.

• Dynamic binding is done by looking up the virtual table for the entry point of
the appropriate function at run-time.
Virtual Functions

Constructors and Destructors


• A constructor cannot be virtual since it is used to construct an object.

• A destructor can be virtual. Virtual destructors are very useful when some
derived classes have cleanup code.

• Example
class B {
public:
virtual B(); // error
virtual ~B(); // ok
virtual void f(); // ok
};
Dynamic v.s. Static binding

When to choose use different kinds of bindings:

• Use compile-time binding when you are sure that any derived class
will not want to override the function dynamically.

• Use run-time binding when the derived class may be able to provide a
different implementation that should be selected at run-time.
Pure Virtual Function

• A pure virtual function is a virtual function in base class that has no


definition. It is declared using specifier “= 0 ”.
class B {
public:
virtual void getname() = 0; //virtual
};

• Only a virtual member function can be pure.


void f() = 0; //error! f() is a stand alone
function
class B {
public:
void getname() = 0; //error! getname not virtual
// ...
};

• Declaring a virtual function pure is not the same as defining a virtual


function with an empty body.
Abstract Class

• A class that has a pure virtual function is an abstract class. Abstract class
is used as an interface for its derived classes.

• If a class derived from an abstract class, and this class doesn’t override all
the pure virtual function in the base class, then this class is also an
abstract class.

• No object can be created for an abstract class ! body.


Thanks a lot

You might also like