Pointers and Virtual Functions Final - 3
Pointers and Virtual Functions Final - 3
2
Course Outcomes
CO Course Outcome
Number
CO1
Understand the concepts of object-oriented programming
including programming process and compilation process.
CO2
Apply different techniques to decompose a problem and
programmed a solution with its sub modules.
CO3 Analyze and explain the behavior of simple programs involving
the programming addressed in the course.
CO4
Implement and evaluate the programs using the syntax and
semantics of object-oriented programming.
CO5 Design the solution of real-world problems in order to
determine that the program performs as expected.
3
Scheme of Evaluation
4
Contents
• Pointer to derived classes
• Static and dynamic binding
5
Pointer to Derived Classes
• It is possible to declare the pointer that points to base
class as well as derived class.
• One pointer can point to different classes.
• For example, X is a base class and Y is a derived class.
• The pointer pointing to X can also point to Y.
6
Pointer to Derived Classes
class base { void main() {
public: base b1;
void show() { b1.show(); // base
cout << “base\n”; derived d1;
} d1.show(); // derived
base *pb = &b1;
};
pb->show(); // base
class derived : public base {
pb = &d1;
public:
pb->show(); // base
void show() {
}
cout << “derived\n”;
All the function calls here are
} statically bound
};
7
Pointer to Derived Classes
10
Static Binding
11
Dynamic Binding
• Dynamic binding occurs when a pointer or reference is
associated with a member function based on the dynamic type
of the object.
• The dynamic type of an object is the type of the object
actually pointed or referred to rather than the static type of its
pointer or reference.
• The member function that is dynamically bound must
override a virtual function declared in a direct or indirect
base class.
• Since dynamic binding occurs at run time, it is also called run
time binding.
12
Dynamic Binding
• Whenever we want a function to be dynamically bound, we
should define that function as virtual in a direct or indirect base
class.
• By doing so, we are turning on the dynamic binding mechanism
and allowing member functions to be selected at run time based
on the type of object pointed or referred to.
• Virtual functions should be used when we want to provide
member functions in our base class that define an interface for
application programs to use.
• The actual implementation of the virtual functions is either
provided by the base class or is overridden and implemented as
appropriate in derived classes.
Dynamic Binding Rules
• Virtual functions cannot be static member functions.
• The signature and return type must be the same for all
implementations of the virtual function.
• While the function must be defined as a virtual function within a
direct or indirect base class, it need not be defined in those
derived classes where the inherited behavior does not need to
differ.
• And finally, the keyword virtual is only required within the base
class itself; derived class implementations of the overridden
function do not need to repeat the use of that keyword.
• Once a member function is declared to be virtual, it remains virtual
for all derived classes.
Virtual function
• A virtual function a member function which is
declared within base class and is re-defined
(Overriden) by derived class. When you refer to a
derived class object using a pointer or a reference to
the base class, you can call a virtual function for that
object and execute the derived class’s version of the
function.
• They are mainly used to achieve run time
polymorphism.
• Functions are declared with a virtual keyword in base
class.
15
Rules for Virtual
function
• They Must be declared in public section of class.
• Virtual functions cannot be static and also cannot be
a friend function of another class.
• Virtual functions should be accessed using pointer or
reference of base class type to achieve run time
polymorphism.
• The prototype of virtual functions should be same in
base as well as derived class.
• They are always defined in base class and
overridden in derived class.
16
NOTE: If we have created virtual function in base
class and it is being overridden in derived class then
we don’t need virtual keyword in derived class,
functions are automatically considered as virtual
functions in derived class.
17
Program to implement virtual function
#include <iostream>
using namespace std;
class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}
void show()
{
cout << "show base class" << endl;
}
}; 18
class derived : public base {
public:
void print()
{
cout << "print derived class" << endl;
}
void show()
{
cout << "show derived class" << endl;
}
};
19
int main()
{
base* bptr;
derived d;
bptr = &d;
// virtual function, binded at runtime
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
}
20
Output
print derived class
show base class
21
Frequently Asked question
22
• Q3 What is meant by dynamic binding?
• Dynamic binding or late binding is the mechanism a
computer program waits until runtime to bind the name of a
method called to an actual subroutine. It is an alternative to
early binding or static binding where this process is
performed at compile-time
Q4What classes Cannot be base class?
• A sealed class cannot be used as a base class. For this
reason, it cannot also be an abstract class.
Sealed classes prevent derivation. Because they can
never be used as a base class, some run-time optimizations
can make calling sealed class members slightly faster
23
Assessment Questions:
1. Predict the Output of following program
A In Base
#include<iostream>
public: Ans:-D
void show() { cout<<"In Derived n"; }
};
int main(void)
Base *bp, b;
Derived d;
bp = &d;
bp->show();
bp = &b;
bp->show();
return 0;
24
}
2.Predict the output of given code:-
#include< iostream>
class Base
public:
void show()
};
public:
int x;
void show()
Derived()
x = 10;
}
25
(A) Compiler Error in line ” bp->show()”
(B) Compiler Error in line ” cout <x”
(C) In Base 10
(D) In Derived 10
Ans:-B
26
Discussion forum.
Can we override static method? Why is main method
static?
Can a derived class pointer point to a base class
object? Justify
27
REFERENCES
Reference Books
[1] Programming in C++ by Reema Thareja.
[2] Programming in ANSI C++ by E.
Balaguruswamy, Tata McGraw Hill.
[3] Programming with C++ (Schaum's Outline
Series) by Byron Gottfried Jitender
Chhabra, Tata McGraw Hill.
Websites:
• https://www.techiedelight.com/difference-betwe
en-static-dynamic-binding-cpp/
YouTube Links:
• https://www.youtube.com/watch?v=m9p_shyDh
Y0
28
THANK YOU