pure virtual function in C++ with example: It is also known as pure virtual methods. It is implemented from the derived class. But that class should not be an Abstract class. If the class is having virtual functions in it, then that is said to be an “C++ Abstact class“. These pure virtual functions will have a declaration and will not have an implementation. A tutorial for this CPP program is given at the bottom of this page.
Pure Virtual Method Program:
Question: Write a C++ program to calculate Area & Perimeter using Pure Virtual Functions.
[SEE: C Program Tutorials] & [IT Companies Details]
// Header Files section #include<iostream.h> #include<conio.h> class rect1 { public: float L,B; virtual void inpu()=0; virtual void APcalculation()=0; }; class Area : public rect1 { public: void inpu() // Function to get input { cout<<"\n \t Input for Rectangle's Length:"; cin>>L; cout<<"\n \t Input for Rectangle's Breadth:"; cin>>B; } void APcalculation() // Function to calculate the Area and Perimeter { cout<<"\n \t Rectangle's Area="<<L*B; cout<<"\n \t Rectangle's Perimeter="<<2*(L+B); } }; void main() { rect1 *ptr; Area a1; clrscr(); cout<<"\n \t Area & Perimeter Calculation using Pure Virtual Function \n"; ptr=&a1; ptr->inpu(); ptr->APcalculation(); getch(); }