Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
41 views49 pages

C++ Pointers & Virtual Functions

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)
41 views49 pages

C++ Pointers & Virtual Functions

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/ 49

CE144

OBJECT ORIENTED PROGRAMMING


WITH C++

UNIT-9
Pointers and Virtual Functions

N. A. Shaikh
[email protected]
Topics to be covered
 Introduction
 pointer to object
 this pointer
 pointer to derived class
 Virtual functions
 pure virtual functions

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


2
Introduction
 Polymorphism means One name, multiple forms

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


3
Introduction
 Compile time polymorphism simply means that an
object is bound to its function call at compile time.

 Compile time polymorphism is also known as early


binding / static binding /static linking.

 Ex: Function and operator overloading.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


4
Introduction
 Run time polymorphism simply means that an object is
bound to its function call at run time.

 Run time polymorphism is also known as late binding /


Dynamic binding /Dynamic linking.

 Ex: Virtual Functions

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


5
Pointer to Objects
 We can use pointers to access the class members.

 A pointer can point to an object created by a class.

 Also known as Object pointers

 Pointer containing address of an object is called Object


pointer

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


6
Pointer to Objects
 How to create an object?
class_name object_name;
item x;

How to create & initialize a pointer?


class_name * pointer_name=&object_name;
item *ptr=&x;

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


7
Pointer to Objects
 Accessing member-function of a class using an object,
object_name.function_name;
x.show();
 Accessing member-function of a class using a pointer,
pointer_name->function_name;
ptr->show();
OR
(*pointer_name).function_name;
(*ptr).show();
Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh
8
Pointer to Objects
 Creating an object at run time using pointer and new
operator,
class_name * pointer_name=new class_name;
item *ptr=new item;

 Creating an array of objects using pointers,


class_name * pointer_name=new class_name[10];
item *ptr=new item[10];

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


9
Pointer to Objects

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


10
Pointer to Objects

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


11
Practical 36(a): Pointer to Objects

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


12
this pointer
 this is a keyword which is used to refer caller object/hold
the address of caller/current object(an object that
invokes a member function)
 this is a const pointer
 this pointer is passed as a hidden argument to non-static
member function
 Compiler will automatically changes all data member
access with this pointer
 this is a local object pointer in every instance member
function containing address of the caller object.
 As Friend functions are not members of a class, they do
not have a this pointer.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


13
this pointer

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


14
this pointer
Following are the situations where ‘this’ pointer is used:
1) When local variable’s name is same as member’s name

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


15
this pointer
2) To return reference to the calling object

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


16
this pointer
NOTE:
 Recall that, when a binary operator is overloaded using
a member function, we pass only one argument to the
function.

 The other argument is implicitly passed using the


pointer this.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


17
Practical 36(b): this pointer

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


18
Pointers to Derived Classes
 We can Use pointer not only to the base objects but
also to the objects of derived classes.
 Pointers to objects of a base class are type-compatible
with pointers to objects of a derived class.
 Therefore, a single pointer variable can be made to
point to objects belonging to different classes.
 Ex: if B is a base class and D is a derived class from B,
then a pointer declared as a pointer to B can also be a
pointer to D.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


19
Pointers to Derived Classes
 We can make bptr to point to the object obD because
obD is an object derived from the class B but its
converse is not true

NOTE:
 Using bptr, we can access only those members which
are inherited from B and not the members that
originally belong to D.
 In case a member of D has the same name as one of
the members of B, then any reference to that member
by bptr will always access the base class member.
 We may have to use another pointer declared as
pointer to the derived type.
Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh
20
Pointers to Derived Classes

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


21
Pointers to Derived Classes

Create a class SOLID_CARBON with data members: string s_name


(diamond or graphite) and o_name (owner name). Write a member
function to display the details. Create another class DIAMOND which is
inherited from the class SOLID_CARBON, which has data members: int
carat and string color. Write a member function to display the data of
both the classes. Write a program which passes the value to the data
members of both the classes through main() function using the concept of
pointer to the derived class Object.
Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh
22
Practical 36(c): Pointers to Derived Classes

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


23
Virtual Functions
 There is a necessity to use the single pointer to refer to
all the objects of the different classes.

 So, we create the pointer to the base class that refers to


all the derived objects.

 But, when base class pointer contains the address of


the derived class object, always executes the base
class function.

 This issue can only be resolved by using the 'virtual'


function.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


24
Virtual Functions
 Virtual function is a member function in the base class that
you redefine in a derived class. It is declared using the
virtual keyword.
 A 'virtual' is a keyword preceding the normal declaration of
a function.
 It is used to tell the compiler to perform dynamic linkage or
late binding on the function.
 When the function is made virtual, C++ determines which
function is to be invoked at the runtime based on the type
of the object pointed by the base class pointer and not by
the type of the pointer.
 In late binding function call is resolved during runtime.
Therefore compiler determines the type of object at
runtime, and then binds the function call.
Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh
25
Virtual Functions

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


26
Virtual Functions
NOTE:
 Run time polymorphism is achieved only when a virtual
function is accessed through a pointer to the base class.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


27
Rules of Virtual Function
1. Virtual functions must be members of some class.
2. Virtual functions cannot be static members.
3. They are accessed through object pointers.
4. They can be a friend of another class.
5. A virtual function must be defined in the base class,
even though it is not used.
6. The prototypes of a virtual function of the base class
and all the derived classes must be identical. If the two
functions with the same name but different prototypes,
C++ will consider them as the overloaded functions
and the virtual function mechanism is ignored
7. We cannot have a virtual constructor, but we can have
a virtual destructor
Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh
28
Rules of Virtual Function
8. While a base pointer can point to any type of the
derived object, the reverse is not true. That is to say,
we can not use a pointer to a derived class to access an
object of base type
9. When a base pointer points to a derived class,
incrementing or decrementing it will not make it to
point to the next object of the derived class. It is
incremented or decremented only relative to it’s base
type. Therefore, we should not use this method to
move the pointer to the next object.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


29
Practical 37: Virtual function
Create a class Media that stores the title (a string) and price (float).
Class Media has two argument constructor which initializes data
members of class Media. Also declare a virtual function display () in
Class Media. From the class Media derive two classes: Class book,
which contains data member page count (int): and Class tape, which
contains data member playing time in minutes (float). Both Class
book and Class tape should have a constructor which initializes base
class constructor as well as its own data members and display ( )
function which displays book details and tape details respectively.
Write a main ( ) to test book and tape classes by creating instances of
them, asking the user to fill data and displaying them.

Use the concept of Virtual function and Constructor in Derived


Class.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


30
Practical 37: Virtual function

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


31
Practical 37: Virtual function

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


32
Practical 37: Virtual function

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


33
Size of Virtual function

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


34
Size of Virtual function

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


35
Virtual function Working Concept

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


36
Virtual function Working Concept

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


37
Abstract class & Pure Virtual Functions
 It is normal practice to declare a function virtual inside
the base class and redefine it in the derived classes.
 Such function inside base class is not used for
performing any task, it only serves as a placeholder.
 For ex, display() function in class Media in previous
example. Such functions are called “do-nothing”
functions.
 A “do-nothing” function may be defined as follows:

 Such functions are called pure virtual functions

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


38
Abstract class & Pure Virtual Functions
 A pure virtual function is a function declared in a base
class that has no definition relative to the base class.

 In such case compiler requires each derived class to


either define the function or redeclare it as a pure
virtual function.

 Class containing pure virtual functions cannot be used


to declare any objects of its own and such classes are
called abstract base classes

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


39
Abstract class & Pure Virtual Functions

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


40
Abstract class & Pure Virtual Functions
NOTE:
1. A class is abstract if it has at least one pure virtual
function.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


41
Abstract class & Pure Virtual Functions
2. We can have pointers and references of abstract class
type.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


42
Abstract class & Pure Virtual Functions
3. If we do not override the pure virtual function in
derived class, then derived class also becomes abstract
class.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


43
Abstract class & Pure Virtual Functions
4. An abstract class can have constructors.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


44
Practical 38:Abstract class & Pure Virtual Functions

Create an Abstract class vehicle having average as data and


pure virtual function getdata() and putdata(). Derive class car
and truck from class vehicle having data members: fuel type
(petrol, diesel, CNG) and no of wheels respectively. Write a
main ( ) that enters the data of two cars and a truck and display
the details of them.

Use the concept of Abstract Base class and Pure Virtual


functions.

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


45
Practical 38:Abstract class & Pure Virtual Functions

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


46
Practical 38:Abstract class & Pure Virtual Functions

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


47
Practical 38:Abstract class & Pure Virtual Functions

Unit 9: Pointers and Virtual Functions Prepared By: Nishat Shaikh


48
End of Unit-9

You might also like