SlideShare uma empresa Scribd logo
1 de 16
 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.
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

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Functions in c
Functions in cFunctions in c
Functions in c
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
SPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in CSPL 9 | Scope of Variables in C
SPL 9 | Scope of Variables in C
 
Type conversion
Type  conversionType  conversion
Type conversion
 
Function in C program
Function in C programFunction in C program
Function in C program
 
functions of C++
functions of C++functions of C++
functions of C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Enums in c
Enums in cEnums in c
Enums in c
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 

Semelhante a Pointers,virtual functions and polymorphism cpp

1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
hara69
 

Semelhante a Pointers,virtual functions and polymorphism cpp (20)

pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function 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
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Pointers
PointersPointers
Pointers
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
pointers,virtual functions and polymorphism
pointers,virtual functions and polymorphismpointers,virtual functions and polymorphism
pointers,virtual functions and polymorphism
 
Unit 8
Unit 8Unit 8
Unit 8
 
Unit 4.pdf
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 

Mais de rajshreemuthiah (20)

oracle
oracleoracle
oracle
 
quality
qualityquality
quality
 
bigdata
bigdatabigdata
bigdata
 
polymorphism
polymorphismpolymorphism
polymorphism
 
solutions and understanding text analytics
solutions and understanding text analyticssolutions and understanding text analytics
solutions and understanding text analytics
 
interface
interfaceinterface
interface
 
Testing &ampdebugging
Testing &ampdebuggingTesting &ampdebugging
Testing &ampdebugging
 
concurrency control
concurrency controlconcurrency control
concurrency control
 
Education
EducationEducation
Education
 
Formal verification
Formal verificationFormal verification
Formal verification
 
Transaction management
Transaction management Transaction management
Transaction management
 
Multi thread
Multi threadMulti thread
Multi thread
 
System testing
System testingSystem testing
System testing
 
software maintenance
software maintenancesoftware maintenance
software maintenance
 
exception handling
exception handlingexception handling
exception handling
 
e governance
e governancee governance
e governance
 
recovery management
recovery managementrecovery management
recovery management
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
 
Buffer managements
Buffer managementsBuffer managements
Buffer managements
 
os linux
os linuxos linux
os linux
 

Último

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

Pointers,virtual functions and polymorphism cpp

  • 1.
  • 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.
  • 13.
  • 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.