0

Possible Duplicate:
What is the difference between a concrete class and an abstract class?

I was coding something in Visual C++ 2008 doing an exercise in a book I am reading when I held my cursor over one of the classes and it told me it was an abstract class. Now I know it is an abstract class as that is what this exercise is about but I was curious as to what lets the Intelisense thing know that it is an abstract class.

I did a little homework and found that it may have been the fact that I have two virtual functions in this class and one of them is a pure virtual.

Is the pure virtual a dead giveaway or are there other things that would tell you you are dealing with or looking at an abstract class?

Community
  • 1
  • 1
Afrika
  • 3
  • 3
  • 1
    Similar to a bunch of other questions on abstract classes in C++, for instance http://stackoverflow.com/questions/1298093/c-abstract-class – AAT May 25 '11 at 20:44
  • @Kev You seem to know nothing about C++, so how can you judge if this is a dupe? –  May 25 '11 at 21:23
  • @neil - it's going over the same ground. Check the accepted answer in http://stackoverflow.com/questions/2149207 - tells him exactly what an abstract class is – Kev May 25 '11 at 21:28
  • @kev The top voted answer there happens to be wrong, and would exhibit undefined behaviour if used. –  May 25 '11 at 21:31

3 Answers3

4

Is the pure virtual a dead giveaway

In C++, yes. Since it has no abstract keyword or equivalent, the common idiom to make a class abstract is to declare a pure virtual function in it (which prevents instantiation).*

For this same reason, in C++ there is not much difference between an interface and an abstract class - a C++ interface is simply a class containing only pure virtual functions.

*Update: Another way to prevent instantiation of a class is to declare its constructor(s) protected or private. The latter means it can't be subclassed either, but the former doesn't prevent subclassing, so in theory a protected constructor could also be a sign of an abstract class a way to force subclassing. However, I have never seen this in practice. I believe this is because an abstract class is designed to be extended, which in practice almost always means it has virtual functions. And the reason why we want it to be abstract is because some part of its implementation is not yet known, hence it is to be defined in its subclass(es). Which is exactly what a pure virtual function is meant for.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • What if I had an abstract class with no pure virtual functions but some regular virtual functions? Would it still be considered abstract? – Afrika May 25 '11 at 20:41
  • @Afrika, if it has no pure virtual functions, it can be instantiated (unless its constructor is declared non-public, that is), thus it is not abstract. – Péter Török May 25 '11 at 20:44
  • @Afrika: That's like asking "What if I had an even integer that was odd? Would it still be even?" The only possible answer is "Yes and no". – TonyK May 25 '11 at 20:45
  • Actually, "abstract" has a specific meaning in C++, vide section 10.4 of the C++ standard. The fact you cannot instantiate a class does not make it abstract. –  May 25 '11 at 21:05
  • You got one part wrong. Abstract classes might have methods other than pure virtuals (which subclasses can call later). Interfaces don't. That makes the difference. – Rápli András Dec 10 '15 at 17:20
1

The only thing that makes a class abstract is if it has one or more unimplemented pure virtual functions, possibly derived ones.

struct A {                    // abstract
    virtual void f() = 0;
};

struct B : public A {        // abstract
};

struct C : public B {        // not abstract
    void f() {}
};

Note that A should also have a (probably not pure) virtual destructor.

It is also possible to implement pure virtual functions, but this is pretty rare:

struct D {
    virtual void f() = 0;
};

The function has to be defined outside the class:

void D :: f() {}

but the class remains abstract.

  • -1. Wrong answer. Even if pure virtual function has implementation, it can be abstract. – Nawaz May 25 '11 at 21:04
  • @Neil. Alright. Removed the downvote. Sorry . – Nawaz May 25 '11 at 21:08
  • +1. Upvoted for its completeness and conciseness. – Nawaz May 25 '11 at 21:13
  • @Neil: If I see an answer with an egregious error, I downvote it and post a comment on the error. If the error gets corrected, I remove my downvote. But that's just me. – TonyK May 25 '11 at 21:17
  • Well, yes for an "egregious error", or at least for a dangerous one (which this isn't) I do the same, but in this case we are talking about the rarely used feature of implementing a pure function, which I omitted for pedagogical purposes. –  May 25 '11 at 21:22
  • @Neil: But that's what your post was about! "The only thing that makes a class abstract...". You simply forgot about the exception, and now here you are pretending you knew about it all along. "Omitted for pedagogical purposes", indeed! -1 for dissimulation. – TonyK May 31 '11 at 20:12
  • @TonyK I'm pretending nothing - and I don't like being called a liar. –  May 31 '11 at 20:21
  • @Neil: Well then, your answer was simply wrong. But it's difficult to follow this thread, after you deleted all those comments. – TonyK May 31 '11 at 20:33
0

I wouldn't say I'm an expert in C++ but I would say you've nailed it. The presence of a pure virtual method means that the class cannot be instantiated and is therefore abstract. If all of your methods are pure virtual (i.e. there is no implementation) it would be an interface and in this case you only need the header file.

Gyan aka Gary Buyn
  • 12,242
  • 2
  • 23
  • 26