13

Is this a pure abstract class?

class C
{
public:
    static const std::string S;
    C() {}
    virtual ~C() {}
    virtual void v() = 0;
}

I believe it is not, following this definition from WikiBooks:

A pure Abstract class has only abstract member functions and no data or concrete member functions.

It can't be pure abstract because it does not have only abstract member functions:

  1. It has a virtual destructor, which is a member function, but not abstract. It is not a pure destructor. Note that "abstract method" and "pure virtual function" are the same things, and that the term "method" and "function" are synonymous.
  2. It has an attribute S, which represents data.

Now my teachers claim it is a pure abstract class, because:

Constants included in a pure virtual class are not considered attributes. They are immutable elements of a class and therefore they don't violate its abstractness. The same holds for static methods.

9
  • The destructor in your class is virtual.
    – aslg
    Oct 18, 2015 at 12:18
  • 1
    The constructor and destructor are kind-of special. The point is that they must exist for every class that can be instantiated, even if it can only be instantiated as part of a derived class' instance. The reason is that creating a derived class first constructs the base class, for which it needs a constructor. Assuming that the abstract baseclass doesn't have any data, this constructor can be trivial though. Oct 18, 2015 at 13:46
  • 10
    The C++ standard doesn't define the term "pure abstract class" at all, so it could be argued that pure abstract classes don't exist in C++; or alternatively, that you could declare a particular C++ construct to be a "pure abstract class", and that's fine as long as other participants in the discussion agree to use your definition. Just ask your teacher to spell out the precise definition of the term he or she is laboring under; nothing like a definitional argument to get one's juices flowing. Oct 18, 2015 at 14:07
  • 1
    Some site on the Internet uses that definition of “pure.” However, the only requirement in the language for an abstract base class is that it have at least one pure virtual function, which can even have an implementation. Can you clarify why the definitional issue of whether that counts as “pure” or not makes any difference?
    – Davislor
    Oct 19, 2015 at 2:01
  • 4
    The definition you provided is kinda useless in context of C++. For one thing, C++ doesn't have abstract member functions (it has pure virtual ones). A C++ class (almost) always has a constructor (possibly an implicit one), which cannot be virtual, so if you insist on counting a constructor as a member function, then no C++ class would match the definition. As to static data members, even languages that have an explicit notion of an abstract class (e.g. Java) don't disallow those. The point is that the class shouldn't have a per-instance data. Oct 19, 2015 at 2:03

4 Answers 4

1

Summing up the comments above:

The constructor and destructor are kind-of special. The point is that they must exist for every class that can be instantiated, even if it can only be instantiated as part of a derived class' instance. The reason is that creating a derived class first constructs the base class, for which it needs a constructor. Assuming that the abstract baseclass doesn't have any data, this constructor can be trivial though.

(Oct. 18, '15 @ 13:46 UTC by Ulrich Eckhardt)

The C++ standard doesn't define the term "pure abstract class" at all, so it could be argued that pure abstract classes don't exist in C++; or alternatively, that you could declare a particular C++ construct to be a "pure abstract class", and that's fine as long as other participants in the discussion agree to use your definition. Just ask your teacher to spell out the precise definition of the term he or she is laboring under; nothing like a definitional argument to get one's juices flowing.

(Oct. 18, '15 @ 14:07 UTC by Igor Tandetnik)

The definition you provided is kinda useless in context of C++. For one thing, C++ doesn't have abstract member functions (it has pure virtual ones). A C++ class (almost) always has a constructor (possibly an implicit one), which cannot be virtual, so if you insist on counting a constructor as a member function, then no C++ class would match the definition. As to static data members, even languages that have an explicit notion of an abstract class (e.g. Java) don't disallow those. The point is that the class shouldn't have a per-instance data.

(Oct. 19, '15 @ 02:03 UTC by Igor Tandetnik)

0
1

An abstract class is one in which there is a declaration but no definition

A pure abstract class in C++ is considered as an interface, too.

Hence, any constant declarations doesn't violate the purity of class abstractness. In IDL you can declare constants inside and outside of interfaces.

However, static members, constructors (even empty one), and a non-abstract destructor break the purity. So pure abstract class would looks like following

class C
{
public:
    const std::string S = "Message";
    virtual ~C() = 0;
    virtual void V() = 0;
};
1

The C++ standard doesn't define what is a "pure abstract" class, nor is there any commonly accepted language-independent definition that fits all languages equally.

The definition 1 you quote doesn't make a lot of sense to me in the context of C++. What your teachers claim is more consistent with the language design. Use whatever definition is convenient and necessary. In any case the notion itself is not at all important.

0

The definition from WikiBooks is definitely coming from the java interfaces. The latter had been created to work around the limitation of single inheritance rule. Java classes can have a single super class and implement multiple interfaces.

A Java interface has:

  1. abstract methods
  2. constants
  3. static variables and methods
  4. default methods.

It does not have constructors, variables, nor this pointers. Default or static methods can access static variables only.

Using above as a template, your class example does not fit in the definition, because it defines a constructor. Interfaces do not have constructors. So, the class with only default constructor would fit. There is no concept of destructors in Java, so it does not matter if you have the destructor or not in your case.

There is no need in C++ to handle Java-type interfaces, because it supports multiple inheritance. The classes can arbitrary have abstract functions.

Java has also a concept of abstract classes. This are classes which have one or more abstract function members. They are very similar to the C++ classes with abstract functions, except for the single inheritance rules. There is no concept of pure abstract classes in Java as well (unless one talks about interfaces).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.