E Balagurusamy Object Oriented Programming With C++

E Balagurusamy Object Oriented Programming With C++

Author:object oriented programming & c++
Language: eng
Format: epub
Tags: Programming


Pure Virtual Functions

C opy rig hted m ateri al

Object-Oriented Programming with C++

SUMMARY

to Polymorphism riimply means one name having multiple forms.

to "There art two types of polymorphism, namely, compile lime polymorphism and run time polymorphism.

to Functions and operators overloading are examples of compile time polymorphism, The overloaded member functions are selected for invoking by matching arguments, both type and number. The compiler knows this information at the compile time and, therefore, compiler is able to select the appropriate function for a particular c«ll at the compile time itself. This is called early or static binding ut static linking, it means that an object is bound to its function call at compile time.

to In run time polymorphism, an appropriate member function in selected w h ile Lhe program is running. C++ supports run time polymorphism with the help of virtual functions. It is called late or dynamic binding because the appropriate function is selected dynamically at run time. Dynamic binding requires use of pointers to objects and is one of the powerful features of C++-

to Object pointers are useful in creating objects at run time. It can be used to access the public members of an object, along with an arrow operator.

to A this [ mi nter refers to an object that currently invokes a member function. For example,

the function call lkIiqw( } will set the pointer 'this' lo thy: address of the object 'a'.

to Pointers to objects of a haee claw type are compatible with [jointers to objects of n derived class Therefore, we can use a single pointer variable to point to objects of base class as wall as derived classes.

to When a function is made virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer h rather than the type of the pointer. By making the base pointer to point to different objects, we can exet-ute different versions of the virtual function.

to Run time polymorphism is achieved only when a virtual function is accessed through a pointer to the base class. It cannot be achieved using object name along with the dot operator to access virtual function,

O We can have virtual destmctorH but not virtual constructors.

to If a virtual function I5 defined in the base class, it need not be nen-.-; airily redefined in the derived class. In such cases, the respective calls will invoke the base class function.

to A virtual function* equated to zero is called a pure virtual function. It is a function declared in a base class that lias no definition relative to the base class. A class containing such pure function is called an abstract class.

Copyrighted material

Pointers, Virtual Functions and Polymorphism

*2G3

Key Terms

| Review Questions

9.1 What dw» polymorphism mean in C++ language?

%.2 How is polymorphism achieved at (a) compile time, and (b) run time ?

9.3 Discuss the different ways by which we can access public member functions of an

object,,

9.4 Explain, with an example, how you would create space for an array of objectn using pointers,

9.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.