In a C++ programming language , a class that contains at least one pure virtual function is called abstract class.
This pure virtual function is a function is declared within base class and defined by derived class.
Like a other class abstract class can also contains normal function.
We can not create object of an abstract class,Any attempt to so will result in an error, but we can create reference to an abstract class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; class A { //abstract base class public: virtual void show() = 0; // pure virtual function void display() { cout << "\n Display in abstract class"; } }; class B: public A { //derived class public: void show() { cout << " Show of derived class "; } }; int main() { B b; b.show(); // call to B’s show() b.display(); // call to A'a display() } |
Output
1 2 | Show of derived class Display in abstract class |
Description: In the above program, we have declared a pure virtual function “show()” in a base class A which is defined by derived class B.
Because class A contain a pure virtual function therefore class A is an abstract class.
Abstract class can also contain a normal member function (in this program member function is display() ).