Page 217 - DCAP404 _Object Oriented Programming
P. 217

Object-oriented Programming                                Ajay Kumar Bansal, Lovely Professional University




                    Notes                  Unit 10: Virtual Functions and Polymorphism


                                     CONTENTS
                                     Objectives
                                     Introduction
                                     10.1 Virtual Functions

                                          10.1.1  Late Binding
                                     10.2 Pure Virtual Functions
                                     10.3 Abstract Classes

                                     10.4 Polymorphism
                                     10.5 Summary
                                     10.6 Keywords
                                     10.7 Review Questions
                                     10.8 Further Readings

                                   Objectives


                                   After studying this unit, you will be able to:
                                      Demonstrate the virtual functions
                                      Recognize the pure virtual functions

                                      Describe the abstract classes
                                      Explain the polymorphism

                                   Introduction

                                   By default, C++ matches a function call with the correct function definition at compile time. This
                                   is called static binding. You can specify that the compiler match a function call with the correct
                                   function definition at run time; this is called dynamic binding. You declare a function with the
                                   keyword virtual if you want the compiler to use dynamic binding for that specific function.

                                   10.1 Virtual Functions

                                   Virtual functions, one of advanced features of OOP is one that does not really exist but it appears
                                   real in some parts of a program. This section deals with the polymorphic features which are
                                   incorporated using the virtual functions.
                                   The general syntax of the virtual function declaration is:
                                   class  use_defined_name{

                                   private:
                                   public:
                                   virtual  return_type  function_name1  (arguments);
                                   virtual  return_type  function_name2(arguments);



          210                               LOVELY PROFESSIONAL UNIVERSITY
   212   213   214   215   216   217   218   219   220   221   222