user569322
user569322

Reputation:

Preventing the instantiation of a class via pure virtual functions?

I'm looking at the following code in my book:

class Shape
{
    public:
        Shape(){}
        ~Shape(){}
        virtual long getArea() = 0; // Pure virtual function
        virtual long getPerim() = 0;
        virtual void draw() = 0;
};

Now it says that these virtual functions make the class abstract (which I understand from Java), so the class cannot be instantiated.

However, it says: "a class is in abstract data type by including one or more virtual functions in the class declaration."

Would this mean if I declared a class with ONE pure virtual function:

class Shape
{
    public:
        Shape(){}
        ~Shape(){}
        virtual long getArea() = 0; // Only pure virtual function
        virtual long getPerim(){}
        virtual void draw(){}
};

does the whole class become abstract? Because if a class has 100+ methods, it'll be tedious to write =0 for every method if I decide to make it abstract later.

Upvotes: 7

Views: 5998

Answers (4)

Steve Jessop
Steve Jessop

Reputation: 279255

Marking a function pure doesn't just make the class abstract, it makes any derived class that doesn't override the function also abstract.

So, if you want to force derived classes to implement all those functions, then make them all pure. If it makes sense for a derived class to implement only getArea and not the other two, then only mark getArea pure. In this example I suspect that it doesn't make sense, since if all the derived class has added is a way to compute the area, there's still no way that the base class can compute the perimeter.

Upvotes: 1

CashCow
CashCow

Reputation: 31445

You are correct in that one pure virtual method is enough to make the class abstract. That means you must implement the method in a derived class to instantiate an instance (of the derived class).

I have issues with your "class with 100+ methods". Usually this is a sign of very poor design.

I also have issues with your "tedious to write =0 for every method" attitude. Firstly I do not see why it is any more tedious to write =0 than it is to write {} as a default no-op implementation (and if the method returns anything, like getPerim() does, you risk undefined behaviour by not returning something). Primarily though, business logic dictates whether or not there is a default behaviour, and not the effort of writing.

Remember the Liskov substitution principle: Although one cannot have an instance of your base class, someone will have a pointer or reference to one, and call virtual methods on it without having to know what class they really have. (Incidentally Liskov was female, first name Barbara, and stated this principle sometime around 1983).

Your abstract base class should almost certainly have a virtual destructor, by the way.

Methods that will not change the state of the class should be declared const.

Upvotes: 2

npclaudiu
npclaudiu

Reputation: 2441

Declaring a method as pure virtual means that classes that inherit your class will have to implement that method or leave it pure virtual, in which case the derived class will also be an abstract class that cannot be instantiated.

Also, as Luchian said previously, you should always declare the destructor as virtual for every class that will be inherited.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

Yes, a single pure virtual method is enough to make the class abstract.

Moreover, you can always make the destructor pure virtual if no other methods are suited to be pure.

Also, your destructor should probably be virtual, since you're obviously going to inherit from this class. This is mandatory if you plan on deleting objects of a derived type through a pointer to a base type.

Upvotes: 12

Related Questions