Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
pointers,virtual functions and polymorphism
-
Upload
rattaj -
Category
News & Politics
-
view
3.625 -
download
2
description
Transcript of pointers,virtual functions and polymorphism
![Page 1: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/1.jpg)
Dr. BABASAHEB AMBEDKAR TECHNOLOGICAL UNIVERSITY
Presentation on
Pointers , Virtual Functions and Polymorphism. By ,
Ruturaj Nalawade
Sanjay Bidkar
Swapnil Sarwade
Under the Guidance of ,
Mrs . Ladda
![Page 2: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/2.jpg)
POINTERS
![Page 3: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/3.jpg)
INTRODUCTION
Pointers are the variables which holds the addresses of other variables.
Pointer variables are denoted by
‘ * ptr ’
Pointers are the derived data types.
![Page 4: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/4.jpg)
INTRODUCTION – CONT.E.g. :={
int i , *j;i = 3 ; j = & i ;cout<<“The value of i is \t”<<i<<endl;
cout<<“The value of *j is \t”<<*j;}
Output : :
The value of i is 3The value of *j is 3
![Page 5: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/5.jpg)
INTRODUCTION – CONT.E.g. :={
int i , *j;i = 3 ; j = & i ;cout<<“The value of i is \t”<<i<<endl;
cout<<“The value of *j is \t”<<*j;}
Output : :
The value of i is 3The value of *j is 3
* j
![Page 6: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/6.jpg)
Introduction - Cont. How the *j gets the value of i ?int i , *j ;
Variable Names
i j
Value
MemoryAddress
65524 65522
![Page 7: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/7.jpg)
Introduction - Cont. How the *j gets the value of i ?int i , *j ;i = 3 ;
Variable Names
i j
Value
MemoryAddress
65524 65522
3
![Page 8: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/8.jpg)
Introduction - Cont. How the *j gets the value of i ?int i , *j ;i = 3 ;j = & i ;
Variable Names
i j
Value 3
MemoryAddress
65524 65522
65524
*j refers to the value at address j.
![Page 9: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/9.jpg)
INTRODUCTION – CONT.
Pointers are used for memory management and achieving polymorphism.
C++ adds the concept of
CONSTANT POINTER & POINTER TO A CONSTANT . :=
![Page 10: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/10.jpg)
INTRODUCTION - CONT.
1.Constant Pointer ::
Declaration =
data type * const pointer
![Page 11: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/11.jpg)
2.Pointer to a Constant ::
data type const * pointer
3.const data type * const pointer
INTRODUCTION - CONT.
![Page 12: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/12.jpg)
POINTERS TO OBJECTS -
Pointers can point to an object created by class .
Declaration : classname object; classname * pointer; Definition : pointer = & object;
![Page 13: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/13.jpg)
POINTERS TO OBJECTS - CONT.
Object pointers are useful in creating objects at run time.
We can also use an object pointer to access the public members & member function of an object , by using ‘->’ operator and the object pointer .
![Page 14: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/14.jpg)
POINTERS TO OBJECTS –CONT.
E.g.pointer -> getdata( );
We can also use
( * pointer ) . function( );
![Page 15: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/15.jpg)
THIS POINTER
C++ uses keyword ‘ this ’ to represent an object that invokes a member function.
E.g. The function call A. max( ) will set the pointer this to the address of the object A.
E.g. To access private variables inside a member function
a=123; or
this -> a = 123;
![Page 16: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/16.jpg)
THIS POINTER - APPLICATIONS
In operator overloading using member function we use implicitly 'this’ pointer.
The another important application of the pointer 'this' is to return the object it points to .
![Page 17: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/17.jpg)
POINTERS TO DERIVED CLASS
Pointers to objects of a base class are type compatible with pointers to objects of a derived class.
A single pointer variable can be made to point to objects belonging to different classes.
![Page 18: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/18.jpg)
POINTERS TO DERIVED CLASS – CONT.
e.g. B *cptr; B b; D d; cptr = & b; we can also make cptr to point to the
object d as follows: cptr = & d
![Page 19: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/19.jpg)
POINTERS TO DERIVED CLASS – CONT.
Base Class
Public: a , bPrivate / Protected:c , d
Derived Class
Public / Private / Protected :
e , f , g , h
If cptr = & d;
cptr a , b
![Page 20: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/20.jpg)
POINTERS TO DERIVED CLASS – CONT.
This shows that , although a base pointer can be made to point to any number of derived objects, it can not directly access the members defined by a derived class.
To access the members defined by a derived class , cast base pointer to the derived class type.
![Page 21: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/21.jpg)
POINTERS TO DERIVED CLASS – CONT.
E.g . Casting
dptr -> show ( ) ;
( ( DC * ) bptr ) -> show ( ) ;
![Page 22: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/22.jpg)
VIRTUAL FUNCTIONS
![Page 23: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/23.jpg)
VIRTUAL FUNCTION
The application of polymorphism is the ability to refer the objects without any regard to their classes.
This necessitates the use of a single pointer variable to refer to the objects of different classes.
![Page 24: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/24.jpg)
VIRTUAL FUNCTION – CONT.
By making the function 'virtual' in base class C++ determines which function to use at run time based on the type of object pointed to by the base pointer , rather than the type of the pointer.
Runtime polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.
![Page 25: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/25.jpg)
VIRTUAL FUNCTIONS - RULES
1. The virtual functions must be members of some class.
2. They cannot be static members.3. They are accessed by using object
pointers.4. A virtual function can be friend of other
function.5. A virtual function in a base class must
be defined , even though it may not be used.
![Page 26: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/26.jpg)
RULES –CONT.
6. We cannot have a virtual constructors, but we can have virtual destructors.
7. While a base pointer can point to any type of derived object, the reverse is not true.
![Page 27: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/27.jpg)
RULES –CONT.
8. The prototypes of the base class version of a virtual function and all the derived class versions must be identical.
9. When a base pointer points to a derived class , incrementing or decrementing it will not make it to point to the next object of the derived class.
![Page 28: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/28.jpg)
RULES –CONT.
10.If a virtual function is define in the base class ,it need not be necessarily redefined in the derived class.
![Page 29: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/29.jpg)
POLYMORPHISM
![Page 30: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/30.jpg)
POLYMORPHISM
Polymorphism is crucial feature of Object Oriented Programming.
Polymorphism simply means one name having multiple forms.
![Page 31: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/31.jpg)
POLYMORPHISM - CONT.
“Polymorphism is the genie in OOP
who takes instruction from clients and
properly interprets their wishes.”
– Ira Pohl, “Object Oriented
Programming using C++”.
![Page 32: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/32.jpg)
POLYMORPHISM – CONT.
Definition:
Polymorphism is the ability to create a variable, a function or an object that has more than one form.
![Page 33: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/33.jpg)
EXAMPLE:
For example:
The + (plus) operator in C++:4 + 5 <-- Integer addition3.14 + 2.0 <-- Floating point
additions1 + "bar" <-- String
concatenation!
![Page 34: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/34.jpg)
TYPES OF POLYMORPHISM
Compile-time
Polymorphism
Polymorphism
Run-time Polymorphis
m
Function Overloading
Operator Overloadin
g
Virtual Function
![Page 35: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/35.jpg)
TYPES OF POLYMORPHISM
In compile time polymorphism, compiler is able to select the appropriate function a particular call at the compile time.
In run time polymorphism, an appropriate member function is selected while the program is running.
![Page 36: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/36.jpg)
BENEFITS OF POLYMORPHISM
Simplicity:This makes your code easier for you to
write and easier for others to understand.
Extensibility:Polymorphism design and implements
system that are more extensible.
![Page 37: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/37.jpg)
REFERENCES :
Let us C++ – by Yeshwant Kanetkar.
Object Oriented Programming with C++
–by E . BALAGURUSAMY.
Internet.
![Page 38: pointers,virtual functions and polymorphism](https://reader036.fdocuments.in/reader036/viewer/2022062512/5538ebf5550346f53d8b48bc/html5/thumbnails/38.jpg)
THANK YOU !!