Pure Virtual Function & Abstract Class in C++ Programming


A pure virtual function is a not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration. An abstract class in C++ is a class that has at least one pure virtual function (i.e., a function that has no definition). The classes inheriting the abstract class must provide a definition for the pure virtual function; otherwise, the subclass would become an abstract class itself.

The program defines a base class bike with a pure virtual method start(). A pure virtual function is a virtual function that is declared in the base class but has no implementation, which means it does not provide a default implementation and must be implemented by any derived class that inherits from it.

  • The bike class is an abstract class, meaning it cannot be instantiated on its own.
  • Then it defines a derived class Apache, which overrides the start() method to print the message "Apache Start".
  • In the main() function, a pointer p of type bike is created and assigned the address of a new Apache object created using the new keyword.
  • When the start() method is called using the p pointer, the derived class Apache's start() method is called, printing the message "Apache Start".

Overall, this program demonstrates the use of pure virtual functions and abstract classes in C++ programming to enforce implementation of certain methods by derived classes, providing a powerful mechanism for implementing polymorphism in object-oriented programming.

Note that the commented-out lines in the bike class demonstrate an alternative way to declare a virtual function with a default implementation in the base class. This method is not used in this program, as the intention is to create an abstract class that forces the implementation of the start() method by any derived class.

Source Code

#include<iostream>
using namespace std;
 
class bike
{
public:
   /* virtual void start()
    {
        cout<<"Bike Start"<<endl;
    }*/
    virtual void start()=0;
};
 
class Apache:public bike
{
   public:
    void start()
    {
        cout<<"Apache Start"<<endl;
    }
};
int main()
{
    bike *p= new Apache();
    p->start();
    return 0;
}
 

Output

Apache Start
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples