0
votes

I have several objects of a Polygon type that just define shapes and I want to store them in a vector that holds Polygons. Polygon has several general methods like getArea() and such, but it is a purely abstract class.

If I have a child of Polygon called Circle, with the method getRadius(), do I need to create a virtual method in Polygon to access that method if it is stored in a vector?

If I expand to more and more shapes, wouldn't creating all those virtual methods in the Polygon declaration (with the associating child method in the child's declaration) be redundant and a waste of space?

Is there another way to inform the compiler that the object I created (of child type) in the Polygon vector of the available child methods without using virtual methods? Or was that kind of the whole point of virtual methods?

I understand how to use virtual methods that the child and parent share, I'm just asking for situations where the parent does not have the method I want to access from the child.

3
If the parent class needs to share a method from potential child classes it should be virtual. If a child class just needs to use a method from the base class this one doesn't need to be virtual in the base class.πάντα ῥεῖ
If you find yourself wanting to create a virtual method that only makes sense for one child class, then your design is probably wrong (i.e. you're probably abusing polymorphism).Oliver Charlesworth
i'm sure someone more knowledgeable than I will come along with an answer soon, but in my mind, if you have code that needs to operate on Circles, it should only accept Circle objects. If it can deal with any Polygon, it shouldn't be using subclass specific functionalityim so confused
Also, "I have a child of Polygon called Circle" - what? Sounds like your parent class should be called Shape.Oliver Charlesworth
You are correct about naming, Oil Charlesworth. Definitely should be Shape.user3908267

3 Answers

2
votes

The point of virtual methods is to allow methods which are implemented differently by different children to be called at runtime with the correct implementation.

If you have a method that is specific to one child class only, then the parent should not have a virtual method for it.

If you are trying to call methods which are specific to Circle then you should be using Circle* instead of Polygon*.

0
votes

If the not-shared method does not make sense for the parent class, then you do not want to put it in the parent class at all. In fact, in that case, polymorphism does not apply, because you cannot have different runtime instances behaving differently on that method: that method just does not make sense to the instances of some child classes, while it does for some others. Therefore, you may want to create a new class, which (1) will become a child of the previously mentioned parent class, (2) will have a virtual method that is shared among the other classes, (3) will be extended by those classes.

In general, if you want to invoke a method specific for a particular class, you need to cast your object to that class or to one of its ancestor declaring that method (virtual or not).

0
votes

I suppose you want to make a single getRadius call to a vector which will call individual polygons respectively. Then a base class obviously makes sense.

Just implement the individual GetRadius Method for all the childclasses and have one default virtual function to ensure consistency.

Since, you're saying, there are multiple shapes, it implies there are multiple classes aka multiple GetRadius implementations.

A virtual function will simplify your calling process.