0

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.

10
  • 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. Aug 4, 2014 at 22:09
  • 2
    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). Aug 4, 2014 at 22:09
  • 1
    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 functionality Aug 4, 2014 at 22:09
  • 2
    Also, "I have a child of Polygon called Circle" - what? Sounds like your parent class should be called Shape. Aug 4, 2014 at 22:10
  • You are correct about naming, Oil Charlesworth. Definitely should be Shape. Aug 4, 2014 at 22:20

3 Answers 3

2

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*.

5
  • Ok, the theory I was working with was that I wanted to create a vector to contain all of my Polygons, with some shared methods for all of the Polygons and some methods specific to each child class. Should I try to abandon this design? I am noticing that my need for specific methods in the child classes is increasing. I want to be able to iterate through the vector, check which kind of polygon it is (i.e. Circle, Rectangle, Triangle) and apply effects to them. Is there a better method for this, as I want to be able to create Polygons of different styles dynamically. Aug 4, 2014 at 22:16
  • @user3908267, One possibility is that you can create a virtual method called applyEffects, which each child will implement differently by calling child-specific methods. Then, when you iterate over the vector, you can just call applyEffects on each polygon, and the object itself will know the correct behavior to perform.
    – merlin2011
    Aug 4, 2014 at 22:18
  • Oh ok! That is a good idea. Ya I really didn't think a bunch of hanging virtual methods in the parent were in any way a good thing. Aug 4, 2014 at 22:21
  • Thanks a bunch, I'm just refreshing myself on object-oriented programming. I program in Assembly all day, so I'm a little rusty. Aug 4, 2014 at 22:24
  • @user3908267, No worries. I'm glad you found this discussion useful. :)
    – merlin2011
    Aug 4, 2014 at 22:25
0

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).

1
  • Thank you! I agree that I really wasn't using polymorphism correctly. Thanks for the post. Aug 4, 2014 at 22:25
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.