473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

inheritance and overridden functions

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

Oct 21 '05 #1
3 1370

Ben wrote:
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<BALL*>.
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.

Oct 21 '05 #2

Ben wrote:
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<BALL*>.
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.

Oct 21 '05 #3
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

Oct 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the...
4
by: Busin | last post by:
When a child class inherits from a base class, will the child class inherits everything of the base class, including all member variables and functions? Or is such inheritance "selective", like not...
20
by: Steve Jorgensen | last post by:
A while back, I started boning up on Software Engineering best practices and learning about Agile programming. In the process, I've become much more committed to removing duplication in code at a...
3
by: vineoff | last post by:
When to do like this: class A { ... }; class B : private A { ... }; Thanks.
7
by: shintu | last post by:
Hi, For the following code snippet, the compiler complains for "mi *mi1=new mi;" statement //******************************************************* #include <iostream> using namespace std;...
18
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
5
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
3
by: djboyse | last post by:
Hey, I'm kind of new to C++ and having some problems with inheritance. I've tried looking everywhere for some help, but I'm not too sure what to ask so please any input will be greatly...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.