Question: 1. What is called pure virtual function ?
Pure
Virtual Function
• Definition : A pure virtual
function is a virtual function which is to be implemented by derived class. The
class that contains the pure virtual function is called the abstract class.
•
The pure virtual functions are declared using pure specifier i.e. = 0
•
Following program demonstrate the use of pure virtual function.
•
The class A is an abstract class in which the pure virtual function Display()
is declared. This function is overridden by the two derived classes ‒ class B
and class C.
/**************************************************************************
Program for demonstrating the pure virtual function
**************************************************************************/
#include<iostream>
using namespace std;
class A
{
public:
virtual void
Display()=0; // Pure
Virtual Function
};
class B: public A
{
public:
void Display()
{
cout<<"\n In
Derived Class B"<<endl;
}
};
class C: public A
{
public:
void Display()
{
cout<<"\n In
Derived Class C"<<endl;
}
};
int main()
{
A* p
B b;
C c;
p = &b;
p‒>Display(); //using pointer the method of derived
class is accessed
p = &c;
p‒>Display();
return 0;
}
In Derived Class B
In Derived Class C
•
The base class A derived two classes
namely ‒ B and C. In class A, the pure
virtual function Display is defined
by assigning = 0.
•
The Display function is overridden
in class B and class C.
• In
main function, the pointer object to the base class is declared. It is denoted
by variable p. By assigning the
address of class B to the pointer
variable p, the function Display of class B is invoked. Similarly by assigning the address of class C the Display function of class C
is invoked.
Review Question
1. What is called pure
virtual function ?
Data Structures using C PlusPlus: Chapter 2: Inheritance and Polymorphism : Tag: Data Structure, C++, C Programing : C++ Program - Pure Virtual Function
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation