A Program based on 'Abstract Class & Pure Virtual Function' :-


#include<iostream.h>
#include<conio.h>

class base
{
public:
virtual void disp()=0;
};
class A:public base
{
public:
void disp()
{
cout<<"Derived Class";
}
};
void main()
{
clrscr();
A s1;
s1.disp();
getch();
}

Output:


Derived Class

			



Pure Virtual Functions and Abstract Classes in C++

Sometimes implementation of all function cannot be provided in a base class because we don’t know the implementation. Such a class is called abstract class. For example, let Shape be a base class. We cannot provide implementation of function draw() in Shape, but we know every derived class must have implementation of draw(). Similarly an Animal class doesn’t have implementation of move() (assuming that all animals move), but all animals must know how to move. We cannot create objects of abstract classes.

A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration.

Previous Next