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.
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.
#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; }
Apache StartTo download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions