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