Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the uses of pure virtual functions in C++?

Tags:

I'm learning about C++ in a class right now and I don't quite grok pure virtual functions. I understand that they are later outlined in a derived class, but why would you want to declare it as equal to 0 if you are just going to define it in the derived class?

like image 554
patricksweeney Avatar asked Jul 01 '09 20:07

patricksweeney


People also ask

What is the use of pure virtual functions?

A pure virtual function is a virtual function in C++ for which we need 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 is a class in C++ which have at least one pure virtual function.

What is the use of pure virtual destructor?

When destroying instances of a derived class using a base class pointer object, a virtual destructor is used to free up memory space allocated by the derived class object or instance. Note: Only Destructors can be Virtual.

What is the purpose of pure virtual function Mcq?

2. What is a pure virtual function in C++? Explanation: Pure virtual function is a virtual function which has no definition/implementation in the base class.


1 Answers

Briefly, it's to make the class abstract, so that it can't be instantiated, but a child class can override the pure virtual methods to form a concrete class. This is a good way to define an interface in C++.

like image 115
Steven Sudit Avatar answered Oct 12 '22 10:10

Steven Sudit