Virtual Functions:
Chapter 11:
Ms. Munazza Mah Jabeen
Assistant Professor of Computer Science
Virtual Functions:
• Virtual means existing in appearance but not in reality. a program that
appears to be calling a function of one class may in reality be calling a
function of a different class.
• Completely different functions are executed by the same function call
i.e. draw(). This is called polymorphism, which means different forms.
• For the polymorphic approach to work, several conditions must be met.
• First, all the different classes of shapes, such as balls and triangles, must be
descended from a single base class.
• Second, the draw() function must be declared to be virtual in the base class.
Normal Member Functions Accessed with Pointers:
Virtual Member Functions Accessed with Pointers:
Binding:
Polymorphism is classified into 2 branches:
• Compile Time Polymorphism/Early Binding/Static
Binding
• Runtime Polymorphism/Late Binding/Dynamic
Binding
What is Binding?
• For every function call; compiler binds or links the
call to one function definition.
• This linking can happen at 2 different time
• At the time of compiling program, or
• At Runtime
Friend Functions & friend Classes:
Assignment and Copy Initialization/this pointer:

Virtual Functions

  • 1.
    Virtual Functions: Chapter 11: Ms.Munazza Mah Jabeen Assistant Professor of Computer Science
  • 2.
    Virtual Functions: • Virtualmeans existing in appearance but not in reality. a program that appears to be calling a function of one class may in reality be calling a function of a different class. • Completely different functions are executed by the same function call i.e. draw(). This is called polymorphism, which means different forms. • For the polymorphic approach to work, several conditions must be met. • First, all the different classes of shapes, such as balls and triangles, must be descended from a single base class. • Second, the draw() function must be declared to be virtual in the base class.
  • 3.
    Normal Member FunctionsAccessed with Pointers:
  • 4.
    Virtual Member FunctionsAccessed with Pointers:
  • 5.
    Binding: Polymorphism is classifiedinto 2 branches: • Compile Time Polymorphism/Early Binding/Static Binding • Runtime Polymorphism/Late Binding/Dynamic Binding What is Binding? • For every function call; compiler binds or links the call to one function definition. • This linking can happen at 2 different time • At the time of compiling program, or • At Runtime
  • 6.
    Friend Functions &friend Classes:
  • 7.
    Assignment and CopyInitialization/this pointer: