Archived

This topic is now archived and is closed to further replies.

AndreTheGiant

purpose of Abstract keyword (c++)

Recommended Posts

Its my understanding that when you have a class, and that class has one or more pure virtual functions, then the class is abstract, and cannot be instantiated. The class is only good for deriving other classes from, and is called an interface. Is that all correct so far? So what is the point of using the abstract keyword? If defining pure virtual functions makes the class abstract, WHEN and WHY would you ever explicitly declare a class abstract, like this: abstract class Student { .... Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There''s not "abstract" keyword in C++. You''re confused. That keyword belongs to Java and possibly C#.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
also in java for example, you might not want all the methods to be declared abstract, so using the abstract on the method implies that the method itself needs redeclaring, whereas declaring it on the class makes ALL the methods abstract. Does the same rule apply to C++?

Share this post


Link to post
Share on other sites
quote:
Original post by AndreTheGiant
Its my understanding that when you have a class, and that class has one or more pure virtual functions, then the class is abstract, and cannot be instantiated. The class is only good for deriving other classes from, and is called an interface.

Is that all correct so far?


Close, to be an interface it must _only_ have pure virtual methods. If you mix, it''s just an abstract base class.

And as others have mentioned, there''s no abstract keyword in C++.

Share this post


Link to post
Share on other sites
Is it just a conceptual difference then?

Because wether it has one or all purre virtual functions, it still cant be instantiated, and must be derived from. Why must it have ALL pure virtuals to earn the name ''interface'' ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by AndreTheGiant
Why must it have ALL pure virtuals to earn the name ''interface'' ?

Because that''s how the word "interface" is defined.

Interfaces and abstract base classes are useful in different situations. One example when interfaces are useful is in the Proxy pattern. The idea is to have a "placeholder"(proxy) class that acts as a stand-in for the "real" object. When you call a function in the proxy, all it does is to call the same function in the real object. For this to work, these two classes must have the same set of functions, so they both implement the same interface (a third class). It wouldn''t make sense to have any implementation in the common interface class, so the pattern specifies an interface, not an abstract base class.

Abstract classes are useful when the base class has some functionality that can be shared among subclasses. In the Template Method pattern, the idea is to let subclasses implement a part of an algorithm, while the base class takes care of another part. The variable part of the algorithm can be declared as a pure virtual function in the base class, and the subclasses have to implement it to complete the algorithm.

Hope that makes sense. The point is that there''s more to it than wether the class can be instantiated or not.

Share this post


Link to post
Share on other sites