2

Here is an interview question :

How can we implement an abstract base class without using pure virtual functions. What we can do so that we cannot create any object of a class because in that case we can say our class is an abstract base class.

At first I thought of using an virtual destructor but I am not sure about this solution because of virtual keyword. Can you please help?

L.ppt
  • 524
  • 6
  • 21

2 Answers2

5

You ask two questions, which we will answer in turn:

How can we implement an abstract base class without using pure virtual functions?

It is impossible, per the definition of an abstract class: "A class is abstract if it has at least one pure virtual function" (C++11 §10.4/2). Therefore, in order to be abstract, a class must declare a pure virtual function or it must inherit one from another class from which it derives.

What we can do so that we cannot create any object of a class?

This question can be interpreted in a number of different ways, each of which has a different solution.

  1. Taken literally, the question asks for a type of which no instance may be created. A class with no defined constructors cannot be constructed.

    To accomplish this, one should declare (but not define) a default constructor and copy constructor for the class. If one is using a compiler with support for C++11's deleted special member functions, they should be declared as deleted.

  2. Taken in the context of the first question, it seems more likely that the intent is to define a class that can only be instantiated as a base class subobject of another class.

    This can be accomplished by declaring all constructors as protected, not providing any static factory member function that creates instances of the class, and by not befriending any other classes or functions.

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • As per L.ppt's comment on Mahesh's answer, protected constructors aren't enough for your solution 2, as any derived class could create an independent instance of the base class - at least, that sounds about right to me, haven't tried it or checked the Standard... – Tony Delroy Jul 08 '12 at 03:13
  • 1
    @TonyDelroy: No, a derived class may only access protected members of a base class using its `this` pointer (or by forming a pointer-to-member, which doesn't apply in this case because it is impossible to obtain a pointer to a constructor). It cannot access protected members of any other instance of the base type. So far as I can tell, I do not see a hole so long as _all_ of the constructors are protected. I'd love to see a (non-pathological) example demonstrating that I'm wrong, though :-) – James McNellis Jul 08 '12 at 05:20
0

A virtual destrutor won't do the required job.You can do either of the following:

  1. Declare a pure virtual destructor with a definition.
  2. Make constructor of the base class protected.

for more clear explanation refer to Making a class abstract without any pure virtual methods.

Community
  • 1
  • 1
vagrawal13
  • 475
  • 2
  • 6
  • 15