SlideShare uma empresa Scribd logo
1 de 29
POINTERS,VIRTUAL FUNCTIONS AND
POLYMORPHISM
SUBMITTED BY
S.Nandhini
Msc (CS&IT)
NSCAS
SYNOPSIS
 Introduction
 Pointers
 Pointers to objects
 This pointer
 Pointer to derived classes
 Virtual functions
 Virtual constructors and destructors
ACHIEVING POLYMORPHISM
polymorphism
Compile time Run time
Function
overloading
Operator
overloading
Virtual
functions
Function overloading:
An overloaded function can have multiple
definitions for the same function name in the same
scope.
Function declarations cannot be
overloaded if they differ only by return type.
Operator overloading:
It is one of the many exciting features of
c++.
important technique that has enhanced
the power of extensibility of c++.
Virtual functions:
we use pointer to base class to refer to
all the derived objects.
INTRODUCTION POLYMORPHISM IN
C++
 Polymorphism is one of the crucial features of oop.
 Polymorphism divide in 2 types
 Compile time
 Run time
 Compile time Polymorphism
 Uses static or early binding
 Ex. Function and operator overloading
 Run time Polymorphism
 Uses Dynamic or early binding
 EX.Virtual Functions
COMPILE TIME POLYMORPHISM
 Function overloading is an example of
compile time polymorphism
 This decision of binding among several
function is taken by considering formal
arguments of the function , their data type
and their sequence.
RUN TIME POLYMORPHISM
 It is also known as dynamic binding, late binding
and overriding as well
 It provides slow execution as compare to early
binding because it is known at run time
 Run time polymorphism is more flexible as all
things execute at run time.
RUN TIME POLYMORPHISM
For Example:
Class A
{
int x;
public:
void show() {…..}
};
class B : public A
{
int y;
public:
void show() {…..}
};
POINTERS
 Pointer is a derived data type that refers to
another data variable by storing the
variable’s memory address rather than data.
 Pointer variable can also refer to (or point to)
another pointer in c++.
DECLARING AND INITIALIZING
POINTERS
 The declaration is based on the data type of the
variable it points to.
 The declaration is based on the data type of the
variable takes the following form
 Syntax:
 data-type *pointer –variable;
 Let us declare a pointer variable, which points to an
integer variable
 Int * ptr;
 we can initialize a pointer
 Int* ptr ,a; // declaration
 Ptr=&a; // initialization
EXAMPLE OF USING POINTERS
#include <iostream.h>
#include <conio.h>
Void main()
{
Int a,*ptr1,**ptr2;
Clrscr();
Ptr1=&a;
Ptr2=&ptr1;
cout <<“The address of a :”<<ptr1<<“n”;
C 0ut <<“The address of ptr1 :”<<ptr2;
Cout <<“nn”;
Cout <<“after incrementing the address values:n”;
Ptr1+=2;
cout <<“The address of a :”<<ptr1<<“n”;
Ptr2+=2;
Cout <<“The address of ptr1 :”<<ptr2<<“n”;
}
Output:
The address of a:0xfb6fff4
The address of ptr1:ox8fb6ff2
After incrementing the address values:
The address of a:ox8fb6fff8
The address of a:ox8fb6fff6
MANIPULATION OF POINTERS
We can manipulate a pointer with the
indirection operator ,i.e.,’*’which is also
known as dereference operator.
Syntax:
*pointer_variable
MANIPULATE OF POINTERS
#include<iostream.h>
#include<conio.h>
Int main()
{
Int a=10;
Int *ptr;
Clrscr();
Ptr=&a;
Cout<<“the value of a is:”<<*ptr;
*ptr=*ptr+a; // manipulate
Cout<<n the revised value of a is”<<a;
getch ();
return o;
}
Output:
The value of a is:10
The revised value of a
is:20
POINTER EXPRESSIONS AND POINTER
ARITHMETIC
 A pointer can be incremented(++) or decremented(--)
 Any integer can be added to or subtracted from a pointer
 One pointer can be subtracted from another
 Example:
int a[6];
int *aptr;
aptr=&a[0];
We can increment the pointer variable
aptr++ (or) ++aptr
We can decrement the pointer variable
aptr-- (or) --aptr
USING POINTERS WITH ARRAY AND
STRINGS
 Pointer is one of the efficient tools to access
elements of an array.
 We can declare the pointers to array
 Int *nptr;
 nptr=number[0];
nptr points to the first element of the integer
array, number[0].
float*fptr;
fptr=price[0];
ARRAY OF POINTERS
 The array of pointers represents a collection
of addresses.
 An array of pointers point to an array of data
items.
 We can declare an array of pointers as
int *inarray[10];
PROGRAM ARRAY OF POINTER
#include<iostream.h>
Const int MAX=4;
Int main()
{
Char*names[100]={“priya”,”nathiya”,”riya”,”sri”,”chitra”};
For (int i=0;i<100;i++)
{
Cout<<“value of names[“<<i<<“]=“;
Cout<<names[i]<<endI;
}
return 0;
}
POINTER AND STRINGS
 There are two ways to assign a value to a
string
 We can use the character array or variable
of type char*.
char num[]=“one”;
const char*numptr=“one”;
THIS POINTER
This unique pointer is automatically passed to a
member function when it is called
The pointer this acts as an implicit argument to
all the member functions
This pointer is an implicit parameter to all
member functions.
EXAMPLE FOR THIS POINTER
Class className
{
Private:
int dataMember;
Public:
Method(int a)
{
//This pointer stores the address of object obj and access dataMemberThis -> dataMember=a;
… … ..
}
}
int main()
{
className obj;
Obj.method(5);
….. … …
}
 Pointers to objects of a base class are type
compatiable with pointers to objects of a
derived class
 A single pointer variable can be made to point
to objects belonging to different classes.
POINTER TO DERIVED CLASS
VIRTUAL FUNCTIONS
 A virtual function is a member function of class
that is declared within a base and re-defined in
derived class.
 Syntax:
virtual return_type function_name()
{
……..
……..
}
RULES OF VIRTUAL FUNCTIONS
 Virtual functions are created for implementing
late binding
 The virtual functions must be members of
some class .
 They cannot be static members.
 They are accessed by using object pointers.
 A virtual function can be a friend of another
class.
Difference between virtual and pure
virtual Functions
BASIS FOR
COMPARISON
VIRTUAL FUNCTION PURE VIRTUAL FUNCTION
Base ‘virtual function’
has their definition
in the base class
‘pure virtual function’ has no
definition in the base class.
Declaration Funct_name(parame
ter_list){….};
Virtual
funct_name(parameter_list)=0;
Derived class All derived classes
may or may not
override the virtual
function of the base
class
All derived classes must
override the virtual
function of the base class.
PURE VIRTUAL FUNCTIONS
 A function virtual inside the base class and
redefine it in the derived classes.
 for ex. we have not defined any object of
class media and therefore the function
display().
 The base class has been defined ‘empty’.
 Virtual void display()=0;
PURE VIRTUAL FUNCTION REAL
WORLD EXAMPLE
int main()
{
Shape*sptr;
Rectangle rect;
Sptr=& ret;
Sptr->set_data (5,3);
Cout<<“area of rectangle is”<<sptr->area()<<endl;
Trangle tri;
Sptr=&tri;
Sptr->set_data(4,6);
Cout<<“area of triangle is”<<sptr->area()<<endLl;
Return 0;
}
VIRTUAL CONSTRUCTORS AND DESTRUCTOR
 “A constructor can not be virtual “.there are some valid reasons that justify this statement
 First to create an object the constructor of the object class must be of the same type as the class.
 Class declarations that make use of destructors
class A
{
public:
~a()
{
// base class destructor
}
};
class B:publicA
{
public:
~b()
{
//derived class destructor
}
};
Main()
{
A* ptr=new B();
.
.
Delete ptr;
}
We must declare the base class destructor
Class A
{
Public:
Virtual ~A()
{
// base class destructor
}
};
Thank you

Mais conteúdo relacionado

Mais procurados

Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Anil Bapat
 
Storage class in c
Storage class in cStorage class in c
Storage class in ckash95
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]Rome468
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphismkiran Patel
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpprajshreemuthiah
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS ConceptBoopathi K
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c languagegourav kottawar
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)Ritika Sharma
 

Mais procurados (20)

Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
Storage class in c
Storage class in cStorage class in c
Storage class in c
 
Functions in C
Functions in CFunctions in C
Functions in C
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphism
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
POINTERS IN C
POINTERS IN CPOINTERS IN C
POINTERS IN C
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 

Semelhante a pointer, virtual function and polymorphism

pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismrattaj
 
C++ Class & object pointer in c++ programming language
C++ Class & object pointer in c++ programming languageC++ Class & object pointer in c++ programming language
C++ Class & object pointer in c++ programming languageHariTharshiniBscIT1
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptgeorgejustymirobi1
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptxhara69
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxShashiShash2
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptishan743441
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programmingClaus Wu
 

Semelhante a pointer, virtual function and polymorphism (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
C++ Class & object pointer in c++ programming language
C++ Class & object pointer in c++ programming languageC++ Class & object pointer in c++ programming language
C++ Class & object pointer in c++ programming language
 
Pointer to function 2
Pointer to function 2Pointer to function 2
Pointer to function 2
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
8 Pointers
8 Pointers8 Pointers
8 Pointers
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Pointers
PointersPointers
Pointers
 
Virtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.pptVirtual Function and Polymorphism.ppt
Virtual Function and Polymorphism.ppt
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Functions
FunctionsFunctions
Functions
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
 
The STL
The STLThe STL
The STL
 

Mais de ramya marichamy

NETWORK DEVICE SECURITY NETWORK HARDENING
NETWORK DEVICE SECURITY NETWORK HARDENINGNETWORK DEVICE SECURITY NETWORK HARDENING
NETWORK DEVICE SECURITY NETWORK HARDENINGramya marichamy
 
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATIONDIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATIONramya marichamy
 
Classical encryption techniques
Classical encryption techniquesClassical encryption techniques
Classical encryption techniquesramya marichamy
 
Region based segmentation
Region based segmentationRegion based segmentation
Region based segmentationramya marichamy
 
Mining single dimensional boolean association rules from transactional
Mining single dimensional boolean association rules from transactionalMining single dimensional boolean association rules from transactional
Mining single dimensional boolean association rules from transactionalramya marichamy
 
Architecture of data mining system
Architecture of data mining systemArchitecture of data mining system
Architecture of data mining systemramya marichamy
 
SHADOW PAGING and BUFFER MANAGEMENT
SHADOW PAGING and BUFFER MANAGEMENTSHADOW PAGING and BUFFER MANAGEMENT
SHADOW PAGING and BUFFER MANAGEMENTramya marichamy
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with filesramya marichamy
 
microcomputer architecture - Arithmetic instruction
microcomputer architecture - Arithmetic instructionmicrocomputer architecture - Arithmetic instruction
microcomputer architecture - Arithmetic instructionramya marichamy
 

Mais de ramya marichamy (19)

NETWORK DEVICE SECURITY NETWORK HARDENING
NETWORK DEVICE SECURITY NETWORK HARDENINGNETWORK DEVICE SECURITY NETWORK HARDENING
NETWORK DEVICE SECURITY NETWORK HARDENING
 
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATIONDIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
 
Image processing
Image processingImage processing
Image processing
 
Classical encryption techniques
Classical encryption techniquesClassical encryption techniques
Classical encryption techniques
 
Servlets api overview
Servlets api overviewServlets api overview
Servlets api overview
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Region based segmentation
Region based segmentationRegion based segmentation
Region based segmentation
 
Design notation
Design notationDesign notation
Design notation
 
Mining single dimensional boolean association rules from transactional
Mining single dimensional boolean association rules from transactionalMining single dimensional boolean association rules from transactional
Mining single dimensional boolean association rules from transactional
 
Architecture of data mining system
Architecture of data mining systemArchitecture of data mining system
Architecture of data mining system
 
segmentation
segmentationsegmentation
segmentation
 
File Management
File ManagementFile Management
File Management
 
Arithmetic & Logic Unit
Arithmetic & Logic UnitArithmetic & Logic Unit
Arithmetic & Logic Unit
 
SHADOW PAGING and BUFFER MANAGEMENT
SHADOW PAGING and BUFFER MANAGEMENTSHADOW PAGING and BUFFER MANAGEMENT
SHADOW PAGING and BUFFER MANAGEMENT
 
B+ tree
B+ treeB+ tree
B+ tree
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
microcomputer architecture - Arithmetic instruction
microcomputer architecture - Arithmetic instructionmicrocomputer architecture - Arithmetic instruction
microcomputer architecture - Arithmetic instruction
 
High speed lan
High speed lanHigh speed lan
High speed lan
 

Último

Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Último (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

pointer, virtual function and polymorphism

  • 2. SYNOPSIS  Introduction  Pointers  Pointers to objects  This pointer  Pointer to derived classes  Virtual functions  Virtual constructors and destructors
  • 3. ACHIEVING POLYMORPHISM polymorphism Compile time Run time Function overloading Operator overloading Virtual functions
  • 4. Function overloading: An overloaded function can have multiple definitions for the same function name in the same scope. Function declarations cannot be overloaded if they differ only by return type. Operator overloading: It is one of the many exciting features of c++. important technique that has enhanced the power of extensibility of c++. Virtual functions: we use pointer to base class to refer to all the derived objects.
  • 5. INTRODUCTION POLYMORPHISM IN C++  Polymorphism is one of the crucial features of oop.  Polymorphism divide in 2 types  Compile time  Run time  Compile time Polymorphism  Uses static or early binding  Ex. Function and operator overloading  Run time Polymorphism  Uses Dynamic or early binding  EX.Virtual Functions
  • 6. COMPILE TIME POLYMORPHISM  Function overloading is an example of compile time polymorphism  This decision of binding among several function is taken by considering formal arguments of the function , their data type and their sequence.
  • 7. RUN TIME POLYMORPHISM  It is also known as dynamic binding, late binding and overriding as well  It provides slow execution as compare to early binding because it is known at run time  Run time polymorphism is more flexible as all things execute at run time.
  • 8. RUN TIME POLYMORPHISM For Example: Class A { int x; public: void show() {…..} }; class B : public A { int y; public: void show() {…..} };
  • 9. POINTERS  Pointer is a derived data type that refers to another data variable by storing the variable’s memory address rather than data.  Pointer variable can also refer to (or point to) another pointer in c++.
  • 10. DECLARING AND INITIALIZING POINTERS  The declaration is based on the data type of the variable it points to.  The declaration is based on the data type of the variable takes the following form  Syntax:  data-type *pointer –variable;  Let us declare a pointer variable, which points to an integer variable  Int * ptr;  we can initialize a pointer  Int* ptr ,a; // declaration  Ptr=&a; // initialization
  • 11. EXAMPLE OF USING POINTERS #include <iostream.h> #include <conio.h> Void main() { Int a,*ptr1,**ptr2; Clrscr(); Ptr1=&a; Ptr2=&ptr1; cout <<“The address of a :”<<ptr1<<“n”; C 0ut <<“The address of ptr1 :”<<ptr2; Cout <<“nn”; Cout <<“after incrementing the address values:n”; Ptr1+=2; cout <<“The address of a :”<<ptr1<<“n”; Ptr2+=2; Cout <<“The address of ptr1 :”<<ptr2<<“n”; } Output: The address of a:0xfb6fff4 The address of ptr1:ox8fb6ff2 After incrementing the address values: The address of a:ox8fb6fff8 The address of a:ox8fb6fff6
  • 12. MANIPULATION OF POINTERS We can manipulate a pointer with the indirection operator ,i.e.,’*’which is also known as dereference operator. Syntax: *pointer_variable
  • 13. MANIPULATE OF POINTERS #include<iostream.h> #include<conio.h> Int main() { Int a=10; Int *ptr; Clrscr(); Ptr=&a; Cout<<“the value of a is:”<<*ptr; *ptr=*ptr+a; // manipulate Cout<<n the revised value of a is”<<a; getch (); return o; } Output: The value of a is:10 The revised value of a is:20
  • 14. POINTER EXPRESSIONS AND POINTER ARITHMETIC  A pointer can be incremented(++) or decremented(--)  Any integer can be added to or subtracted from a pointer  One pointer can be subtracted from another  Example: int a[6]; int *aptr; aptr=&a[0]; We can increment the pointer variable aptr++ (or) ++aptr We can decrement the pointer variable aptr-- (or) --aptr
  • 15. USING POINTERS WITH ARRAY AND STRINGS  Pointer is one of the efficient tools to access elements of an array.  We can declare the pointers to array  Int *nptr;  nptr=number[0]; nptr points to the first element of the integer array, number[0]. float*fptr; fptr=price[0];
  • 16. ARRAY OF POINTERS  The array of pointers represents a collection of addresses.  An array of pointers point to an array of data items.  We can declare an array of pointers as int *inarray[10];
  • 17. PROGRAM ARRAY OF POINTER #include<iostream.h> Const int MAX=4; Int main() { Char*names[100]={“priya”,”nathiya”,”riya”,”sri”,”chitra”}; For (int i=0;i<100;i++) { Cout<<“value of names[“<<i<<“]=“; Cout<<names[i]<<endI; } return 0; }
  • 18. POINTER AND STRINGS  There are two ways to assign a value to a string  We can use the character array or variable of type char*. char num[]=“one”; const char*numptr=“one”;
  • 19. THIS POINTER This unique pointer is automatically passed to a member function when it is called The pointer this acts as an implicit argument to all the member functions This pointer is an implicit parameter to all member functions.
  • 20. EXAMPLE FOR THIS POINTER Class className { Private: int dataMember; Public: Method(int a) { //This pointer stores the address of object obj and access dataMemberThis -> dataMember=a; … … .. } } int main() { className obj; Obj.method(5); ….. … … }
  • 21.  Pointers to objects of a base class are type compatiable with pointers to objects of a derived class  A single pointer variable can be made to point to objects belonging to different classes. POINTER TO DERIVED CLASS
  • 22. VIRTUAL FUNCTIONS  A virtual function is a member function of class that is declared within a base and re-defined in derived class.  Syntax: virtual return_type function_name() { …….. …….. }
  • 23. RULES OF VIRTUAL FUNCTIONS  Virtual functions are created for implementing late binding  The virtual functions must be members of some class .  They cannot be static members.  They are accessed by using object pointers.  A virtual function can be a friend of another class.
  • 24. Difference between virtual and pure virtual Functions BASIS FOR COMPARISON VIRTUAL FUNCTION PURE VIRTUAL FUNCTION Base ‘virtual function’ has their definition in the base class ‘pure virtual function’ has no definition in the base class. Declaration Funct_name(parame ter_list){….}; Virtual funct_name(parameter_list)=0; Derived class All derived classes may or may not override the virtual function of the base class All derived classes must override the virtual function of the base class.
  • 25. PURE VIRTUAL FUNCTIONS  A function virtual inside the base class and redefine it in the derived classes.  for ex. we have not defined any object of class media and therefore the function display().  The base class has been defined ‘empty’.  Virtual void display()=0;
  • 26. PURE VIRTUAL FUNCTION REAL WORLD EXAMPLE int main() { Shape*sptr; Rectangle rect; Sptr=& ret; Sptr->set_data (5,3); Cout<<“area of rectangle is”<<sptr->area()<<endl; Trangle tri; Sptr=&tri; Sptr->set_data(4,6); Cout<<“area of triangle is”<<sptr->area()<<endLl; Return 0; }
  • 27. VIRTUAL CONSTRUCTORS AND DESTRUCTOR  “A constructor can not be virtual “.there are some valid reasons that justify this statement  First to create an object the constructor of the object class must be of the same type as the class.  Class declarations that make use of destructors class A { public: ~a() { // base class destructor } }; class B:publicA { public: ~b() { //derived class destructor } }; Main() { A* ptr=new B(); . . Delete ptr; }
  • 28. We must declare the base class destructor Class A { Public: Virtual ~A() { // base class destructor } };