Download presentation
Presentation is loading. Please wait.
Published byWidya Johan Modified over 6 years ago
1
Pointers Dr. Bhargavi Goswami Department of Computer Science
Christ University Bangalore.
2
Pointers, virtual, polymorphism
Polymorphism means, one name, multiple forms. Means? Overloading? Yes/No. Yes. Two Types: 1) Early Binding/Static Binding/Static Linking. 2) Late Binding/Dynamic Binding/ Early Binding Information is known to compiler before execution starts. Called Compile time polymorphism. Means: Object is bound to its function call at compile time.
3
Pointers, virtual, polymorphism
Problem: We cannot select appropriate function during the execution of the program. Solution? 2) Late Binding / dynamic binding / run time polymorphism. Here, selection of appropriate function is done dynamically at run time. How do we do that? Ans: we need pointers to objects and virtual functions for that. How? That’s what we will study in this chapter.
4
Examples refVar.cpp refVar1.cpp refVarPointerEx.cp p
ReturnByReference .cpp firstPointer.cpp morePointers.cpp CallbyValAddrRef.c pp PointerArray.cpp PointersAsArgumen tsConstants.cpp voidPointer.cpp PointertoMember.c pp PointerToFunction. cpp DereferencingOper ator.cpp
5
With Pointers we did Introduction to Pointers
Declaring and Initializing Pointers Manipulation of Pointers Arithmetic Operations on Pointers Pointers with arrays and arrays of pointers Pointers to functions Pointers to objects Array of pointers to objects If you have forgotten, please revise by referring to previously listed programs. Lets see some advanced concepts of pointers now.
6
This pointer This is a keyword to represent an object that invokes a member function. This is a pointer to object for which this function was called. Eg. A.max() will set pointer this to address of obj A. Important: This pointer acts as implicit argument to all the member function calls. Object is passed implicitly to called function in unary operator overloading. Similarly we pass 1st operand implicitly to binary operator overloading function call and 2nd operand as an argument. Eg. Suppose A is member variable. Both are same: A = 123; //easy so frequently used. this->A = 123; //complex so not frequently used. Another application is to return the object. Eg. return *this; Don’t get confused, lets see example to clarify doubts. See Program of This Pointer: ThisPointer.cpp
7
Pointer to Derived Objects
We can use pointers not only to the base objects but also to the objects of derived classes Pointers to base class has type compatibility to derived class. Means? Same pointer can point to any of the class (derived or base). Rule: We can access only those members which are inherited from Base Class and not the member of Derived Class. If the function names are same, pointer call will access base class and not derived class member. Then? We need to define separate pointer to derived class. See Program of Pointer to Derived Objects. PointersToDerivedObjetcs.cpp
8
Virtual functions Virtual Function is a function in base class, which is overridden in the derived class, and which tells the compiler to perform Late Binding on this function. Virtual Keyword is used to make a member function of the base class Virtual. When we use Base class's pointer to hold Derived class's object, base class pointer or reference will always call the base version of the function On using Virtual keyword with Base class's function, Late Binding takes place and the derived version of function will be called, because base class pointer pointes to Derived class object. Lets see the example. All your confusion will be resolved. PureVirtualFunction.cpp VirtualFunctions.cpp RuntimePolymorphism.cpp
9
Rules for Virtual functions
Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call. They are mainly used to achieve Runtime polymorphism Functions are declared with a virtual keyword in base class. The resolving of function call is done at Run-time. 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. It is not mandatory for derived class to override (or re- define the virtual function), in that case base class version of function is used. A class may have virtual destructor but it cannot have a virtual constructor.
10
Pure Virtual function Pure Virtual functions can be given a small definition in the Abstract class, which you want all the derived classes to have. Still you cannot create object of Abstract class. Also, the Pure Virtual function must be defined outside the class definition. If you will define it inside the class definition, complier will give an error. Inline pure virtual definition is Illegal. We have done this during abstract class.
11
Chapter Over. Thank you. See u soon.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.