inheritance and overridden functions

B

Ben

Please help:

I have a class called BALL
and I have some classes that inherit from BALL: BASKETBALL, BASEBALL,
SOCCERBALL, MINI_SOCCERBALL, etc.

I want to be able to have a single object that keeps track of every
ball, so I have a vector of type BALL*, where I keep track of all of
the different balls no matter what inherited class they are.

But when I call a function, I want the overridden functions to be used.
So, if I call Rotate() on one of the objects in my vector and it is a
BASKETBALL then it should use the Rotate() function from the BASKETBALL
class.

What is the best way to do this?

Here is what I think my options are:

1. Add a member variable called BallType to BALL that keeps track of
which class it is and use that to determine which inherited class is
used when calling member functions.

2. Make the functions into function pointers and set the pointers in
the constructors.

3. Or maybe I am doing something completely wrong and shouldn't even
have a vector of BALLs that are actually varioud inherited classes.


Here is why I don't know which method to use:

Problem with #1: This seems to go against the object oriented software
development techniques which I have been taught, because future
inherited objects would not be able to be created without changing the
parent object.

Problem with #2: This goes against common programming practices,
including OOD. I believe the only way to do this is to use use global
functions that would only be used by single classes, but they wouldn't
be members of the class. Only the function pointer would.

Problem with #3: I might have to start over from scratch, and I don't
know what it is that I could be doing wrong.



Unless someone can give me some new insight, then I am gonna use #2.

Thanks,
Ben
 
M

Maxim Yegorushkin

Ben said:
Please help:

I have a class called BALL
and I have some classes that inherit from BALL: BASKETBALL, BASEBALL,
SOCCERBALL, MINI_SOCCERBALL, etc.

I want to be able to have a single object that keeps track of every
ball, so I have a vector of type BALL*, where I keep track of all of
the different balls no matter what inherited class they are.

Or better use std::map<BALL*>. This container inserts unique balls and
finds them much faster than an unsorted std::vector said:
But when I call a function, I want the overridden functions to be used.
So, if I call Rotate() on one of the objects in my vector and it is a
BASKETBALL then it should use the Rotate() function from the BASKETBALL
class.

Learn how to use virtual functions. Simply put, you make Rotate() a
virtual function in the base class and override it in the descendants.
A call to Rotate() through a pointer/reference to the base class
resolves to the overridden descendant's function.
 
M

Maxim Yegorushkin

Ben said:
Please help:

I have a class called BALL
and I have some classes that inherit from BALL: BASKETBALL, BASEBALL,
SOCCERBALL, MINI_SOCCERBALL, etc.

I want to be able to have a single object that keeps track of every
ball, so I have a vector of type BALL*, where I keep track of all of
the different balls no matter what inherited class they are.

Or better use std::set<BALL*>. This container inserts unique balls and
finds them much faster than an unsorted std::vector said:
But when I call a function, I want the overridden functions to be used.
So, if I call Rotate() on one of the objects in my vector and it is a
BASKETBALL then it should use the Rotate() function from the BASKETBALL
class.

Learn how to use virtual functions. Simply put, you make Rotate() a
virtual function in the base class and override it in the descendants.
A call to Rotate() through a pointer/reference to the base class
resolves to the overridden descendant's function.
 
B

Ben

Thank you very much for your response!

I use virtual functions, but I didn't know that virtual functions could
still have a default function. So, after reading your post, I decided
to try a virtual function dropping the "= 0" and adding a member
function to the parent class. It worked exactly how I needed.

Thanks again,
Ben Dacko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top