0% found this document useful (0 votes)
54 views29 pages

Pointers and Virtual Functions Final - 3

1. Virtual functions allow dynamic binding at runtime based on an object's actual type, rather than its declared type. 2. A virtual function is declared with the virtual keyword in a base class and can be overridden in derived classes. 3. When calling a virtual function through a base class pointer or reference, the version executed depends on the actual object type. This allows polymorphic behavior.

Uploaded by

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

Pointers and Virtual Functions Final - 3

1. Virtual functions allow dynamic binding at runtime based on an object's actual type, rather than its declared type. 2. A virtual function is declared with the virtual keyword in a base class and can be overridden in derived classes. 3. When calling a virtual function through a base class pointer or reference, the version executed depends on the actual object type. This allows polymorphic behavior.

Uploaded by

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

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-2


Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Object Oriented Programming using C++
Subject Code: 22CSH-103

Pointers & Virtual Functions DISCOVER . LEARN . EMPOWER


Object Oriented
Programming
using C++
Course Objectives

To enable the students to understand various


stages and constructs of C++ programming
language and relate them to engineering
programming problems.

To improve their ability to analyze and address


variety of problems in programming domains.

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

• In C++, we can declare a • #include<iostream.h>


pointer points to the class base
base class as well as {
derive class.       public:
Consider this example to      int n1;
understand pointer to      void show()
derived class.      {
         cout<<”\nn1 =
“<<n1;
     }
};
8
Pointer to Derived Classes
• class derive : public base •     bptr=&b;  //address of base
{ class
     public:     bptr->n1=44;          
     int n2; //access base class via base pointer
     void show()
     {     bptr->show();
         cout<<”\nn1 = “<<n1;     derive d;
         cout<<”\nn2 = “<<n2;               cout<<”\n”;
     }
};
    bptr=&d;      //address of derive
class
• int main()     bptr->n1=66;           
{ //access derive class via base
    base b; pointer
    base *bptr;      //base pointer
    cout<<”Pointer of base class     bptr->show();
points to it”;     return 0;
} 9
Pointer to Derived Classes

• Output • Here the show() method


Pointer of base class is the overridden
points to it method, bptr execute
n1 = 44 show() method of ‘base’
Pointer of base class class twice and display its
points to derive class content. Even though
n1=66 bptr first points to ‘base’
and second time points
to ‘derive’ ,both the time
bptr->show() executes
the ‘base’ method show()

10
Static Binding

• Static binding occurs when an object is associated with a


member function based on the static type of the object.
• The static type of an object is the type of its class or the type
of its pointer or reference.
• A member function statically bound to an object can be either
a member of its class or an inherited member of a direct or
indirect base class.
• Since static binding occurs at compile time, it is also called
compile time 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

• Q1 Can a derived class pointer point to a base class object?


• A derived pointer cannot be assigned a pointer to a base type
without casting simply because it cannot tell if the base pointer is
of the Derived type or one of its children.
• Q2 What is difference between static binding and dynamic
binding?
• Static binding happens at compile-time while dynamic
binding happens at runtime. Binding of private, static and final
methods always happen at compile time since these methods
cannot be overridden. ... The binding of overloaded methods
is static and the binding of overridden methods is dynamic.

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>

using namespace std; In Base


class Base B. In Base
{
In Derived
public:

virtual void show() { cout<<" In Base n"; }


C In Derived
}; In Derived
D. In Derived
class Derived: public Base
In Base
{

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>

using namespace std;

class Base

public:

void show()

cout<<" In Base ";

};

class Derived: public Base

public:

int x;

void show()

cout<<"In Derived ";

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

You might also like