SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
PDF
Concurrency
Sri Prasanna
 
PDF
Constructors and destructors
Nilesh Dalvi
 
PPT
Introduction to oop
Kumar
 
PPT
Unit 4 designing classes
gopal10scs185
 
PPTX
Polymorphism in c++(ppt)
Sanjit Shaw
 
PPTX
Data Type Conversion in C++
Danial Mirza
 
PPTX
Presentation on Function in C Programming
Shuvongkor Barman
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
Software requirements specification
lavanya marichamy
 
PPTX
Object model
James Wong
 
PPT
C# Exceptions Handling
sharqiyem
 
PPTX
DYNAMIC MEMORY ALLOCATION.pptx
LECO9
 
PPTX
C decision making and looping.
Haard Shah
 
PPTX
Constructor overloading in C++
Learn By Watch
 
PPTX
CSharp Presentation
Vishwa Mohan
 
PPTX
Recursive Function
Kamal Acharya
 
PPTX
Dynamic memory allocation
Viji B
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Concurrency
Sri Prasanna
 
Constructors and destructors
Nilesh Dalvi
 
Introduction to oop
Kumar
 
Unit 4 designing classes
gopal10scs185
 
Polymorphism in c++(ppt)
Sanjit Shaw
 
Data Type Conversion in C++
Danial Mirza
 
Presentation on Function in C Programming
Shuvongkor Barman
 
Software requirements specification
lavanya marichamy
 
Object model
James Wong
 
C# Exceptions Handling
sharqiyem
 
DYNAMIC MEMORY ALLOCATION.pptx
LECO9
 
C decision making and looping.
Haard Shah
 
Constructor overloading in C++
Learn By Watch
 
CSharp Presentation
Vishwa Mohan
 
Recursive Function
Kamal Acharya
 
Dynamic memory allocation
Viji B
 

Similar to pointer, virtual function and polymorphism (20)

PPTX
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
PDF
Polymorphism
SherabGyatso
 
PPTX
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Mysteriousexpert
 
PPTX
pointers,virtual functions and polymorphism
rattaj
 
PPTX
C++ Class & object pointer in c++ programming language
HariTharshiniBscIT1
 
PPTX
Pointer to function 2
Abu Bakr Ramadan
 
PDF
polymorphism in c++ with Full Explanation.
UdayGumre
 
PPTX
Pointer to function 1
Abu Bakr Ramadan
 
PPTX
Pointer in C
bipchulabmki
 
PPT
presentation_pointers_1444076066_140676 (1).ppt
georgejustymirobi1
 
PPT
c program.ppt
mouneeshwarans
 
PDF
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
PPT
8 Pointers
Praveen M Jigajinni
 
PPTX
1. DSA - Introduction.pptx
hara69
 
PPTX
C++ FUNCTIONS-1.pptx
ShashiShash2
 
PPT
Advanced pointers
Koganti Ravikumar
 
PPTX
Pointers
Samsil Arefin
 
PPT
Virtual Function and Polymorphism.ppt
ishan743441
 
PPTX
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
PDF
Polymorphism and Type Conversion.pdf pot
e13225064
 
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
Polymorphism
SherabGyatso
 
6be10b153306cc41e65403247a14a4dba5f9186aCHAPTER 2_POINTERS, VIRTUAL FUNCTIONS...
Mysteriousexpert
 
pointers,virtual functions and polymorphism
rattaj
 
C++ Class & object pointer in c++ programming language
HariTharshiniBscIT1
 
Pointer to function 2
Abu Bakr Ramadan
 
polymorphism in c++ with Full Explanation.
UdayGumre
 
Pointer to function 1
Abu Bakr Ramadan
 
Pointer in C
bipchulabmki
 
presentation_pointers_1444076066_140676 (1).ppt
georgejustymirobi1
 
c program.ppt
mouneeshwarans
 
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
1. DSA - Introduction.pptx
hara69
 
C++ FUNCTIONS-1.pptx
ShashiShash2
 
Advanced pointers
Koganti Ravikumar
 
Pointers
Samsil Arefin
 
Virtual Function and Polymorphism.ppt
ishan743441
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
Polymorphism and Type Conversion.pdf pot
e13225064
 
Ad

More from ramya marichamy (19)

PPTX
NETWORK DEVICE SECURITY NETWORK HARDENING
ramya marichamy
 
PPTX
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
ramya marichamy
 
PPTX
Image processing
ramya marichamy
 
PPTX
Classical encryption techniques
ramya marichamy
 
PPTX
Servlets api overview
ramya marichamy
 
PPTX
Divide and conquer
ramya marichamy
 
PPTX
Region based segmentation
ramya marichamy
 
PPTX
Design notation
ramya marichamy
 
PPTX
Mining single dimensional boolean association rules from transactional
ramya marichamy
 
PPTX
Architecture of data mining system
ramya marichamy
 
PPTX
segmentation
ramya marichamy
 
PPTX
File Management
ramya marichamy
 
PPTX
Arithmetic & Logic Unit
ramya marichamy
 
PPTX
SHADOW PAGING and BUFFER MANAGEMENT
ramya marichamy
 
PPTX
B+ tree
ramya marichamy
 
PPTX
Managing console i/o operation,working with files
ramya marichamy
 
PPTX
Operator overloading
ramya marichamy
 
PPTX
microcomputer architecture - Arithmetic instruction
ramya marichamy
 
PPTX
High speed lan
ramya marichamy
 
NETWORK DEVICE SECURITY NETWORK HARDENING
ramya marichamy
 
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
ramya marichamy
 
Image processing
ramya marichamy
 
Classical encryption techniques
ramya marichamy
 
Servlets api overview
ramya marichamy
 
Divide and conquer
ramya marichamy
 
Region based segmentation
ramya marichamy
 
Design notation
ramya marichamy
 
Mining single dimensional boolean association rules from transactional
ramya marichamy
 
Architecture of data mining system
ramya marichamy
 
segmentation
ramya marichamy
 
File Management
ramya marichamy
 
Arithmetic & Logic Unit
ramya marichamy
 
SHADOW PAGING and BUFFER MANAGEMENT
ramya marichamy
 
Managing console i/o operation,working with files
ramya marichamy
 
Operator overloading
ramya marichamy
 
microcomputer architecture - Arithmetic instruction
ramya marichamy
 
High speed lan
ramya marichamy
 
Ad

Recently uploaded (20)

PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
Introduction presentation of the patentbutler tool
MIPLM
 
Controller Request and Response in Odoo18
Celine George
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 

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 } };