9

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?

10 Answers 10

7

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.

2

Yes, it must have at least one pure virtual function.

In case all the virtual functions for your base class have an implementation, and you would like to make it abstract nonetheless, you can use a pure virtual destructor:

class MyAbstractClass
{
    virtual ~MyAbstractClass() = 0;

    virtual void f() 
    {
        IHaveAnImplementation(); 
        SoICannotBePure();
    }
};

// The destructor can unfortunately not be defined inline
MyAbstractClass::~MyAbstractClass() {}

This only a conveniance: a pure destructor is not really a pure function since it has a definition. It is only a marker saying that the class cannot be instantiated, although it has no other abstract functions.

2

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.

1

The difference is that you cannot instantiate the abstract class - it acts as an interface.

3
  • Just curious (I am not doubting you), when I have used C# interface concepts, the interface has a link to the implementation class, for instance when using the Observer pattern. So how would another class be able to 'reach' the derived class through an abstract class (aka interface)?
    – user195488
    Apr 16, 2011 at 19:19
  • It's rather simple in C++ - the derived class IS-A base class, the caller just holds a pointer or a reference to the base part of it. Apr 16, 2011 at 19:24
  • for example, each instance of a class contains reference to virtual function table thus given an instance and a method call (offset in that table) one can 'reach' derived class' code Apr 16, 2011 at 19:25
1

To be abstract a class must have one pure virtual function. Only virtual function can be pure since it could be overriden and thus it's useful for polymorphism. Pure non-virtual function doesn't make sense because it doesn't do anything and couldn't be overriden, so it's useless, and doesn't exist :)

0

pure virtual means the method has no implementation so it forces any non abstract child class to provide that implementation.

1
  • no, pure virtual means it must have an implementation in a derived class, but it can still have an implementation in the base class
    – Chris Card
    Apr 16, 2011 at 19:32
0

In C++, the only way to make a class abstract is to put at least one pure virtual function in it. The compiler won't let you instantiate a class that contains a pure virtual function, because then you'd have an object with a function that has no definition. There are probably cryptic ways to get around this, but this is the standard practice.

The difference between a pure virtual and virtual function is that a pure virtual does not specify the implementation of the method. The =0 syntax tells the compiler that the class is not providing a definition for the function, which makes the function pure virtual and makes the class abstract. Any class deriving from the abstract base class must define the pure virtual function, or else the subclass will be abstract as well.

A "non-pure" virtual function is one which is marked with the virtual keyword, but a definition for the function is supplied in the base class. This means that the base class provides an implementation of the function, which any subclasses can override if desired. The virtual keyword allows polymorphism to work when you're using base class pointers that point to derived class objects.

0

A virtual function can be overridden in a derived class.

A pure virtual function must be overridden in a derived class.

A class with pure virtual functions cannot be instantiated.

0

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

It depends on the definition you use. The standard use

A class is abstract if it has at least one pure virtual function.

so yes it is mandatory. Informally you may use another definition but then you risk confusion in a C++ context.

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

A pure virtual member:

  • must be overridden in all non abstract derived class

  • can be left without definition (but giving a definition is possible, it is even mandatory in the case of a pure virtual destructor).

3
  • A pure virtual function may have an implementation
    – Chris Card
    Apr 16, 2011 at 19:31
  • @Chris Card, perhaps a problem as a non native english speaker. I wrote "may not have" to indicate that it is possible to not give a definition and would have written "must not have" to indicate that giving a definition is forbidden. I'll clarify. Apr 16, 2011 at 19:34
  • OK, I see, "may not" is ambiguous, but whether or not it has an implementation is not part of the meaning of "pure virtual"
    – Chris Card
    Apr 16, 2011 at 19:38
0

A pure virtual function is one which must be overridden by any concrete (i.e., non-abstract) derived class. This is indicated in the declaration with the syntax " = 0" in the member function's declaration.

Example:

class AbstractClass {
public:
  virtual void AbstractMemberFunction() = 0; // Pure virtual function makes
                                             // this class Abstract class.
  virtual void NonAbstractMemberFunction1(); // Virtual function.

  void NonAbstractMemberFunction2();
};

In general an abstract class is used to define an implementation and is intended to be inherited from by concrete classes. It's a way of forcing a contract between the class designer and the users of that class. If we wish to create a concrete class (a class that can be instantiated) from an abstract class we must declare and define a matching member function for each abstract member function of the base class. Otherwise, if any member function of the base class is left undefined, we will create a new abstract class (this could be useful sometimes).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.