Q:

What is a pure virtual function in C++?

0

What is a pure virtual function in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have an implementation, we only declare it. A pure virtual function is declared by assigning 0 in the declaration.  We can not instantiate the abstract class and we have to define it in the derived class.

Let see the below example.

#include<iostream>
using namespace std;
class Base
{
public:
    //pure virtual function
    virtual void fun() = 0;
};
class Child: public Base
{
public:
    void fun()
    {
        cout << "Child class fun is called";
    }
};
int main()
{
    Child d;
    d.fun();
    return 0;
}

Output: Child class fun is called

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now