Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtual functions and abstract classes

Tags:

c++

visual-c++

I read in my book:

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

Is it mandatory for an abstract class to have a virtual function? Why?

What is the difference between pure virtual function and virtual function and what is the need of them?

like image 929
Sadique Avatar asked Apr 16 '11 19:04

Sadique


People also ask

Is virtual function same as abstract class?

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 abstract class and virtual function?

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.

Do virtual functions make a class abstract?

Any class that has at least one pure virtual function is known as an abstract class. An abstract class cannot be instantiated (i.e. you cannot build an object of this class type).

Are virtual functions abstract?

Yes, they are the same thing. In C++, an abstract method is just another way of describing the characteristics of a pure virtual function. Both just mean a method with no implementation provided that needs to be implemented in a sub-class before the class can actually be instantiated.


2 Answers

A pure virtual function specifies an interface that must be overridden in a derived class to be able to create objects of the derived class.

A (non-pure) virtual function specifies an interface that can be overridden in a derived class, but the base class provides a default implementation of the interface.

For most practical purposes, yes, an abstract base class must contain at least one virtual function. The whole point of an abstract base class is to specify an interface that's implemented by derived classes. That interface is specified in terms of a number of virtual functions that can be called. Without virtual functions, you haven't specified an interface, which makes it pretty hard for the abstract base class to accomplish much.

like image 87
Jerry Coffin Avatar answered Nov 11 '22 18:11

Jerry Coffin


If you use an abstract class, that means you don't want to instantiate this class incorrectly. And you must use pure virtual function in that class.But declaring or writing function in class is your choice. You can write the function in derived class too.

like image 38
metin Avatar answered Nov 11 '22 18:11

metin