Abstract Base Class - Can't use derived functions cause compiler can't find member function in base class

Hi folks,

I'm learning about Abstract base classes. I have a base, Shape, and derived, Triangle, Rectangle and Circle. I'm trying to set the float base in Triangle, but I get an error that setBase is not a member function of Shape. Right, it isn't. I want to use the Triangle function. So do I have to call Triangle's function specifically instead of using my pointer? It doesn't make sense for me to make a base variable and setBase function in Shape because some shapes don't have a base value.

Is there some way I can use my pointer to Triangle to call triangle's function instead of it trying to call Shape's? Or do I have to use triangle's function specifically?

Let me know if you want to see some code.
You can't use the pointer if the function doesn't exist in the Base class. The reason is that when you use the pointer, you try to call the Base class's method, which is redirected to method of the actual class you are pointing to. That wouldn't work if Base doesn't have that method defined.

Basically, you can't call derived class-specific methods that way. What if you didn't happen to be pointing to a Triangle with you Base*, but instead a Circle?
Code please.
If you know the shape is a triangle you can cast it. Or you can just put the function in the base class and throw some error on other shapes
If you doing shapes, it sounds like you've just covered polymorphism!

Do you have a specific problem you're trying to solve?

Usually you have access to the derived type at the point of construction, to you should have no problem setting the initial value of the base. So is it that you need to change the base value later? If so, have you covered casting?
Last edited on
Yep, just covered polymorphism. And I understand what I read, but the example I read only used a method that all derived classes could use. So I'm trying to figure out how best to handle methods that are specific to derived classes. The instructor's texts focus on pointers, so I figured I had to use them for the isolated methods as well as the base methods.

This is an online class, which is bad enough, cause it's a 16 week course in 8 weeks. But there's no real interaction with the instructor unless you email him. Otherwise we just look over this weeks notes and them do the project. To ask questions, we email him and wait a day for the response. It's way less efficient to email the instructor and wait for a response from one person than to ask a forum. So thanks for the replies, everyone! I appreciate the input!

Yes, the base value for the triangle will be set by the user in the program. I have a rectangle with width and height, a Triangle with base and height, and a Circle with radius and a constant, pi. I could change the triangle's base to width, but I still have unique variables in circle, so why bother.

The user creates multiple different shapes and sets the values at creation. The shapes go into an STL.

So is it best to just use the derived class's methods directly? Or is there a more pointer-y way to do this more efficiently?

Code:

http://pastebin.com/AG1mWMqi
Last edited on
The whole point of polymorphism is to have a function common to all derived classes, say Perimeter() or Area() that would have different internals for each of the types (Triangle, Square, etc.). Then you can have a function that takes a Shape*, and does something with the perimeter or area without caring about what kind of Shape you are dealing with. Using a method specific to one of the derived classes kind of defeats the point of dealing with a more general class.
Ok then. Since it doesn't make sense to treat a circle the same as a rectangle in every way, he must just want us to demonstrate the ability to use polymorphism with area() and reference the derived classes for other functions.

I'm going to email him now. Thanks for clearing that up, though!
If you need to get at methods specific to a derived class, you can use a cast. Either a dynamic_cast, or a static_cast.

The dynamic_cast is safer, but requires RTTI to be enabled (it generally is, but under some circumstances might be unavailable). If you do a dynamic_cast on a base class pointer, it will return NULL if the cast is invalid.

The static_cast relies on you doing a check (you'll need to implement a getType() which returns triangle, square, etc.) then do the cast if it's safe to do so.

Andy

P.S. a dymanic_cast on a reference results in a bad_cast exception if the cast is invalid.
Just had a look at your pastebin page...

You need to move your member variable to the right derived class.

area, base, width, height, radius, pi;

Of course, pi shouldn't be a member of any class. It's a universal constant!

Topic archived. No new replies allowed.