SlideShare a Scribd company logo
5
Most read
10
Most read
11
Most read
Pointers,virtual functions and polymorphism cpp
 POLYMORPHISM
 POINTERS
 POINTERS TO OBJECT
 THIS POINTER
 POINTERS TO DERIVED CLASSES
 VIRTUAL FUNCTIONS
 PURE VIRUAL FUNCTIONS
Polymorphism is one of the crucial features of OOP.It simply means one
name, multiple forms. We have already seen how the concept of
polymorphism is implemented using the overload functions and
operators.
EXAMPLE:
Class A
{
Int x;
Public:
Void show() {……}//show in base class
};
Class B:public A
{
Int y;
Public:
Void show() {….}//show in derives class
};
POLYMORPHISM
Compile time
polymorphism
Run time
polymorphism
Function
overloading
Operator
overloading
Virtual
function
A pointer is a derived data type that refers to another data variable by
storing the variable is memory address rather than data declaring and
initialing pointers.
 DECLARING AND INITIALIZING POINTER
We can declare a pointer variable similar to other variables in C++.
 MANIPULATION OF POINTER
We can manipulate a pointer with the indirection operator . I e., which is
also known as deference operator.
Syntax: data_ type * pointer _variable
Syntax:*pointer _ variable
 POINTER EXPRESSION AND POINTER
ARITHMETIC
There are a substantial number of arithmetic operations that be Performed
with pointers.
o A pointer can be incremented(++)(or)decrement(--).
o Any integer can be added to or subtracted from a pointer.
o One pointer can be subtracted from another .
Example:
int a[6]; k
int*aptr;
aptr=&a[0];
 USING POINTERS WITH ARRAYS AND STRINGS
Pointers is one of the efficient to access elements of an
array. pointers are useful to allocate arrays dynamically.
I .e, we can decide the array size at run time.
We can declare the pointers to array as follows
int*nptr ;
nptr =number[0]; (or) nptr=number;
 ARRAY OF POINTER
The array of pointers represents a collection of address. by declaring array of
pointers we can save a subtantial amount of memory space.
 POINTERS AND STRINGS
C++ also allows us to handle the special kind of arrays, I e, strings with pointers.
we known that a string is one dimensional array of character, which start with
the index “0” and end with the null character in C++.
Syntax: Int*in array[10]
Syntax: char num[]=”one”;
Const char *number=”one”;
 POINTERS TO FUNCTIONS
The pointer to function is known as callback function, we can use these
function pointers to refer to a function. using function pointers ,we can allow
a C++ program to select a function dynamically at run time.
POINTER USING C++:
#include<iostream.h>
#include<conio.h>
Void main( )
{
Int a,*ptr1;
Ptr1=&a;
Cout<<“The address of a:”<<ptr1<<“/n”;
Getch( );
}
Syntax: data_type(*function_ name);
As started earlier ,a pointer can point to an object created by a
class item x; when item is and x is an object defined to be of
type item. similarly we can define a pointer.
Example:
class item
{
Int code;Float price;
Public:
Void getdata(int a, float b)
{Code=a; price=b;}
Void show(void)
{cout<<“code:”<<code<<“/n”;
Cout<<“price:”<<price<<“/n”;}
};
item*it_ ptr;
Example:
#include< iostream. h>
#include<conio.h>
Class test
{private:
Int x;
Public:
Void set x(int x)
{this->x=x;}
void print(){ cout<<“x=“};
int main()
{
Test obj;
Int x=20;
Obj. setx(x);
Obj . print();
Return 0;}
 This this pointer is a constant
pointer that holds the memory
address of the current object
,The this pointer is not
available in static member
functions can be called
without any object. static
member functions can be
called with class name.
OUTPUT: X=20
All derived classes inherit properties from the common
base class. Pointers can be declared to the point base or
derived class. pointers to objects of the base class are type
compatible with pointers to objects of derived class .A
base class pointer can point to objects of both the baseand
derived class.
B*cptr; //pointer to class b type variable
B b; / /base object
D d; //derived object
Cptr=&b; //cptr points to object b
Cptr=&d; //cptr points to object d
A virtual function is a member function that you expect to
Be refined in derived classes. When you refer to a derived
Class objects using a pointer or a reference to the base
class, you can call a virtual function for that object and
execute the derived class's version of the function.
Pointers,virtual functions and polymorphism cpp
Pure virtual function is also known as abstract
function. A class with at least one Pure virtual
function or abstract function is called abstract
class. We can’t create an member function
of abstract class will be invoked by derived class
object.
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp

More Related Content

What's hot (20)

PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
Functions in c++
Rokonuzzaman Rony
 
PPT
RECURSION IN C
v_jk
 
PPTX
Recursion in c++
Abdul Rehman
 
PPTX
Friend function
zindadili
 
PPT
C++ classes tutorials
Mayank Jain
 
PPT
Class and object in C++
rprajat007
 
PPTX
Virtual base class
Tech_MX
 
PPTX
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
PPTX
Inline function
Tech_MX
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
Object Oriented Programming Using C++
Muhammad Waqas
 
PPSX
Files in c++
Selvin Josy Bai Somu
 
PPSX
Function in c
savitamhaske
 
PPTX
07. Virtual Functions
Haresh Jaiswal
 
PPTX
Strings in Java
Abhilash Nair
 
PPT
structure and union
student
 
PPTX
Operator overloading
Ramish Suleman
 
PPTX
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 
PPTX
This pointer
Kamal Acharya
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Functions in c++
Rokonuzzaman Rony
 
RECURSION IN C
v_jk
 
Recursion in c++
Abdul Rehman
 
Friend function
zindadili
 
C++ classes tutorials
Mayank Jain
 
Class and object in C++
rprajat007
 
Virtual base class
Tech_MX
 
Pointers, virtual function and polymorphism
lalithambiga kamaraj
 
Inline function
Tech_MX
 
Object Oriented Programming Using C++
Muhammad Waqas
 
Files in c++
Selvin Josy Bai Somu
 
Function in c
savitamhaske
 
07. Virtual Functions
Haresh Jaiswal
 
Strings in Java
Abhilash Nair
 
structure and union
student
 
Operator overloading
Ramish Suleman
 
Datatype in c++ unit 3 -topic 2
MOHIT TOMAR
 
This pointer
Kamal Acharya
 

Similar to Pointers,virtual functions and polymorphism cpp (20)

PPTX
pointer, virtual function and polymorphism
ramya marichamy
 
PPTX
pointers,virtual functions and polymorphism
rattaj
 
PPTX
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
PPTX
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Mysteriousexpert
 
PPTX
Pointer and polymorphism
SangeethaSasi1
 
PDF
polymorphism in c++ with Full Explanation.
UdayGumre
 
PDF
Polymorphism
SherabGyatso
 
PPTX
Presentations_PPT_ Unit-6_OOP.pptx
ZeelGoyani
 
PDF
Object Oriented Programming (OOP) using C++ - Lecture 4
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
PPT
Polymorphism
Nilesh Dalvi
 
PDF
Object Oriented Programming using C++ - Part 4
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
PPTX
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
PPTX
C++ Object Oriented Programming
Gamindu Udayanga
 
PPTX
Object oriented programming slides for presentation
abdullahkhann3534
 
PPTX
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
PDF
22 scheme OOPs with C++ BCS306B_module4.pdf
sindhus795217
 
PPTX
Polymorphism
prabhat kumar
 
PDF
CS225_Prelecture_Notes 2nd
Edward Chen
 
PPT
Virtual Function
Lovely Professional University
 
pointer, virtual function and polymorphism
ramya marichamy
 
pointers,virtual functions and polymorphism
rattaj
 
OOPS & C++(UNIT 4)
Dr. SURBHI SAROHA
 
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Mysteriousexpert
 
Pointer and polymorphism
SangeethaSasi1
 
polymorphism in c++ with Full Explanation.
UdayGumre
 
Polymorphism
SherabGyatso
 
Presentations_PPT_ Unit-6_OOP.pptx
ZeelGoyani
 
Object Oriented Programming (OOP) using C++ - Lecture 4
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Polymorphism
Nilesh Dalvi
 
Object Oriented Programming using C++ - Part 4
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Object Oriented Programming using C++: Ch11 Virtual Functions.pptx
RashidFaridChishti
 
C++ Object Oriented Programming
Gamindu Udayanga
 
Object oriented programming slides for presentation
abdullahkhann3534
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
22 scheme OOPs with C++ BCS306B_module4.pdf
sindhus795217
 
Polymorphism
prabhat kumar
 
CS225_Prelecture_Notes 2nd
Edward Chen
 
Ad

More from rajshreemuthiah (20)

PPTX
oracle
rajshreemuthiah
 
PPTX
quality
rajshreemuthiah
 
PPTX
bigdata
rajshreemuthiah
 
PPTX
polymorphism
rajshreemuthiah
 
PPTX
solutions and understanding text analytics
rajshreemuthiah
 
PPTX
interface
rajshreemuthiah
 
PPTX
Testing &ampdebugging
rajshreemuthiah
 
PPTX
concurrency control
rajshreemuthiah
 
PPTX
Education
rajshreemuthiah
 
PPTX
Formal verification
rajshreemuthiah
 
PPTX
Transaction management
rajshreemuthiah
 
PPTX
Multi thread
rajshreemuthiah
 
PPTX
System testing
rajshreemuthiah
 
PPTX
software maintenance
rajshreemuthiah
 
PPTX
exception handling
rajshreemuthiah
 
PPTX
e governance
rajshreemuthiah
 
PPTX
recovery management
rajshreemuthiah
 
PPTX
Implementing polymorphism
rajshreemuthiah
 
PPSX
Buffer managements
rajshreemuthiah
 
PPTX
os linux
rajshreemuthiah
 
polymorphism
rajshreemuthiah
 
solutions and understanding text analytics
rajshreemuthiah
 
interface
rajshreemuthiah
 
Testing &ampdebugging
rajshreemuthiah
 
concurrency control
rajshreemuthiah
 
Education
rajshreemuthiah
 
Formal verification
rajshreemuthiah
 
Transaction management
rajshreemuthiah
 
Multi thread
rajshreemuthiah
 
System testing
rajshreemuthiah
 
software maintenance
rajshreemuthiah
 
exception handling
rajshreemuthiah
 
e governance
rajshreemuthiah
 
recovery management
rajshreemuthiah
 
Implementing polymorphism
rajshreemuthiah
 
Buffer managements
rajshreemuthiah
 
os linux
rajshreemuthiah
 
Ad

Recently uploaded (20)

PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 

Pointers,virtual functions and polymorphism cpp

  • 2.  POLYMORPHISM  POINTERS  POINTERS TO OBJECT  THIS POINTER  POINTERS TO DERIVED CLASSES  VIRTUAL FUNCTIONS  PURE VIRUAL FUNCTIONS
  • 3. Polymorphism is one of the crucial features of OOP.It simply means one name, multiple forms. We have already seen how the concept of polymorphism is implemented using the overload functions and operators. EXAMPLE: Class A { Int x; Public: Void show() {……}//show in base class }; Class B:public A { Int y; Public: Void show() {….}//show in derives class };
  • 5. A pointer is a derived data type that refers to another data variable by storing the variable is memory address rather than data declaring and initialing pointers.  DECLARING AND INITIALIZING POINTER We can declare a pointer variable similar to other variables in C++.  MANIPULATION OF POINTER We can manipulate a pointer with the indirection operator . I e., which is also known as deference operator. Syntax: data_ type * pointer _variable Syntax:*pointer _ variable
  • 6.  POINTER EXPRESSION AND POINTER ARITHMETIC There are a substantial number of arithmetic operations that be Performed with pointers. o A pointer can be incremented(++)(or)decrement(--). o Any integer can be added to or subtracted from a pointer. o One pointer can be subtracted from another . Example: int a[6]; k int*aptr; aptr=&a[0];  USING POINTERS WITH ARRAYS AND STRINGS Pointers is one of the efficient to access elements of an array. pointers are useful to allocate arrays dynamically. I .e, we can decide the array size at run time.
  • 7. We can declare the pointers to array as follows int*nptr ; nptr =number[0]; (or) nptr=number;  ARRAY OF POINTER The array of pointers represents a collection of address. by declaring array of pointers we can save a subtantial amount of memory space.  POINTERS AND STRINGS C++ also allows us to handle the special kind of arrays, I e, strings with pointers. we known that a string is one dimensional array of character, which start with the index “0” and end with the null character in C++. Syntax: Int*in array[10] Syntax: char num[]=”one”; Const char *number=”one”;
  • 8.  POINTERS TO FUNCTIONS The pointer to function is known as callback function, we can use these function pointers to refer to a function. using function pointers ,we can allow a C++ program to select a function dynamically at run time. POINTER USING C++: #include<iostream.h> #include<conio.h> Void main( ) { Int a,*ptr1; Ptr1=&a; Cout<<“The address of a:”<<ptr1<<“/n”; Getch( ); } Syntax: data_type(*function_ name);
  • 9. As started earlier ,a pointer can point to an object created by a class item x; when item is and x is an object defined to be of type item. similarly we can define a pointer. Example: class item { Int code;Float price; Public: Void getdata(int a, float b) {Code=a; price=b;} Void show(void) {cout<<“code:”<<code<<“/n”; Cout<<“price:”<<price<<“/n”;} }; item*it_ ptr;
  • 10. Example: #include< iostream. h> #include<conio.h> Class test {private: Int x; Public: Void set x(int x) {this->x=x;} void print(){ cout<<“x=“}; int main() { Test obj; Int x=20; Obj. setx(x); Obj . print(); Return 0;}  This this pointer is a constant pointer that holds the memory address of the current object ,The this pointer is not available in static member functions can be called without any object. static member functions can be called with class name. OUTPUT: X=20
  • 11. All derived classes inherit properties from the common base class. Pointers can be declared to the point base or derived class. pointers to objects of the base class are type compatible with pointers to objects of derived class .A base class pointer can point to objects of both the baseand derived class. B*cptr; //pointer to class b type variable B b; / /base object D d; //derived object Cptr=&b; //cptr points to object b Cptr=&d; //cptr points to object d
  • 12. A virtual function is a member function that you expect to Be refined in derived classes. When you refer to a derived Class objects using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.
  • 14. Pure virtual function is also known as abstract function. A class with at least one Pure virtual function or abstract function is called abstract class. We can’t create an member function of abstract class will be invoked by derived class object.