473,385 Members | 1,409 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,385 software developers and data experts.

inheritance ideas

Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot.

What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB. Of course, I
can't, however the reason I'm looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.

So, while I do realize that this doesn't work, and
furthermore I also realize WHY this doesn't and shouldn't
work, I'm wondering if I can get around it without
COMPLETELY changing the design of what I have so far.

Thanks so much,
Mike
Jul 22 '05 #1
12 1366
"Mike Frayn" <re**********@hotmail.com> wrote...
Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot.

What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB.
So, why not have a pure virtual function in CBaseClass that would
"access" your derived class data member? Why do you need to access
it, anyway?
Of course, I
can't, however the reason I'm looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.
So, the ability to access data members specific to derived classes
has really nothing to do with the reasons for which you created your
hierarchy, right? Then why are you trying to use the base class for
something it wasn't created to do?
So, while I do realize that this doesn't work, and
furthermore I also realize WHY this doesn't and shouldn't
work, I'm wondering if I can get around it without
COMPLETELY changing the design of what I have so far.


There is no reason to completely change the design. However, there
is a strong need for you to realise that WRT accessing data members
you have _no_design_ whatsoever, so there is nothing to change. If
you uncover your secret why you need that data (and public too), we
could probably suggest a couple of solutions.

V
Jul 22 '05 #2
Mike Frayn wrote:
Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot. [snip] class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
}; [snip]
class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
}; [snip]

Thanks so much,
Mike


By the way, there is no reason to prefix all of
your classes with the letter 'C'. This is a
Microsoft naming convention.

See:
http://www.jelovic.com/articles/stupid_naming.htm
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #3
"Mike Frayn" <re**********@hotmail.com> wrote in message
news:40*********************@lovejoy.zen.co.uk...
[snip]

I would like to be able to access DataFieldB. Of course, I
can't, however the reason I'm looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.


You might have a look at boost::any at

www.boost.org

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #4
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote...
[...]
By the way, there is no reason to prefix all of
your classes with the letter 'C'. This is a
Microsoft naming convention.


Actually, it's not just Microsoft. You'd be surprised how many other
software companies have their own naming conventions that look similar
and must seem unnecessary. Of course, Microsoft gets bashed because
they published theirs...

V
Jul 22 '05 #5
In article <40*********************@lovejoy.zen.co.uk>,
"Mike Frayn" <re**********@hotmail.com> wrote:
Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot.

What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB. Of course, I
can't, however the reason I'm looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.

So, while I do realize that this doesn't work, and
furthermore I also realize WHY this doesn't and shouldn't
work, I'm wondering if I can get around it without
COMPLETELY changing the design of what I have so far.


void dealWithDataFieldB( CBaseClass* bc ) {
CInheritedClassA* ic = dynamic_cast< CInheritedClassA >( bc );
assert( ic );
ic->DataFieldB = 5;
}

Note the assert. I would only recommend using this in cases where you
know that the object in question is going to be a CInheritedClassA
object *every* time this piece of code is run. IMHO of course.
Jul 22 '05 #6

"Mike Frayn" <re**********@hotmail.com> wrote in message
news:40*********************@lovejoy.zen.co.uk...
Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured
that I would give it a shot.

What I want is to have a base class with particular member
data and a virtual function that will be overloaded by the
inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I
would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB. Of course, I
can't, however the reason I'm looking for such functionality
is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists
for separate types.

So, while I do realize that this doesn't work, and
furthermore I also realize WHY this doesn't and shouldn't
work, I'm wondering if I can get around it without
COMPLETELY changing the design of what I have so far.

Thanks so much,
Mike


Just define virtual functions which access the data members you want.

You shouldn't be defining public data anyway, that's rule 1 of OO design.

john
Jul 22 '05 #7
> By the way, there is no reason to prefix all of
your classes with the letter 'C'. This is a
Microsoft naming convention.


I disagree, I believe there is a reason and that's why I use
it.

Mike
Jul 22 '05 #8
Aha! Thanks Dan!

Mike

"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news1.west.e arthlink.net...
In article <40*********************@lovejoy.zen.co.uk>,
"Mike Frayn" <re**********@hotmail.com> wrote:
Hi there. I'm wondering how to achieve a particular
functionality using virtual functions and class
inheritance... I don't know if its possible, but I figured that I would give it a shot.

What I want is to have a base class with particular member data and a virtual function that will be overloaded by the inheriting class.

class CBaseClass
{
public:
virtual void OverLoadMe(void) {}
int DataFieldA;
};

In the inherited class, I will overload the function but I would like to introduce new member data:

class CInheritedClassA : public CBaseClass
{
void OverLoadMe(void) { //happy now? }
float DataFieldB;
};

Now if I defined a variable like so:

CBaseClass* MyClass = new CInheritedClassA;

I would like to be able to access DataFieldB. Of course, I can't, however the reason I'm looking for such functionality is so that I can house a group of inheritedclassA-Z
instances in one list without having to have separate lists for separate types.

So, while I do realize that this doesn't work, and
furthermore I also realize WHY this doesn't and shouldn't work, I'm wondering if I can get around it without
COMPLETELY changing the design of what I have so far.
void dealWithDataFieldB( CBaseClass* bc ) {
CInheritedClassA* ic = dynamic_cast< CInheritedClassA
( bc );
assert( ic );
ic->DataFieldB = 5;
}

Note the assert. I would only recommend using this in

cases where you know that the object in question is going to be a CInheritedClassA object *every* time this piece of code is run. IMHO of

course.
Jul 22 '05 #9
> Just define virtual functions which access the data
members you want.

Hmmm, but then I may as well have placed all the data
members in the base class. No? Seems to me that they'd
both look equally unattractive.
You shouldn't be defining public data anyway, that's rule

1 of OO design.

I should have figured I'd receive grief for that. It was
irrelevant to my example so I didn't bother.

Mike
Jul 22 '05 #10

"Mike Frayn" <re**********@hotmail.com> wrote in message
news:40*********************@lovejoy.zen.co.uk...
Just define virtual functions which access the data

members you want.

Hmmm, but then I may as well have placed all the data
members in the base class. No? Seems to me that they'd
both look equally unattractive.


If all derived classes (now and in the future) share the data members, then
put them into the base class. If they don't then don't. I don't really see
the problem.

There's nothing to stop you putting an error catching virtual method in the
base class.

class Base
{
public:
virtual int getA() const { throw "no A exists in Base"; }
};

class Derived : public Base
{
public:
virtual int getA() const { return a; }
private:
int a;
};

john
Jul 22 '05 #11
Okay John, you seem to be approaching this as if I already
understand what the right answer is. What's the problem?
The problem is that I don't understand :)

Uhm, my point was simply that if I'm going to add a virtual
function for every data member that is added in a derived
class, then I'll have a bunch of virtual functions in the
base class, some of which get used in derivations from it
and some that don't. That seems to be the same, to me, as
simply defining all the member variables in the base class
and then only using some of them in each particular derived
class.

Mike

"John Harrison" <jo*************@hotmail.com> wrote in
message news:c2*************@ID-196037.news.uni-berlin.de...

"Mike Frayn" <re**********@hotmail.com> wrote in message
news:40*********************@lovejoy.zen.co.uk...
Just define virtual functions which access the data members you want.

Hmmm, but then I may as well have placed all the data
members in the base class. No? Seems to me that they'd
both look equally unattractive.


If all derived classes (now and in the future) share the

data members, then put them into the base class. If they don't then don't. I don't really see the problem.

There's nothing to stop you putting an error catching virtual method in the base class.

class Base
{
public:
virtual int getA() const { throw "no A exists in Base"; } };

class Derived : public Base
{
public:
virtual int getA() const { return a; }
private:
int a;
};

john

Jul 22 '05 #12

"Mike Frayn" <re**********@hotmail.com> wrote in message
news:40*********************@lovejoy.zen.co.uk...
Okay John, you seem to be approaching this as if I already
understand what the right answer is. What's the problem?
The problem is that I don't understand :)

Uhm, my point was simply that if I'm going to add a virtual
function for every data member that is added in a derived
class, then I'll have a bunch of virtual functions in the
base class, some of which get used in derivations from it
and some that don't. That seems to be the same, to me, as
simply defining all the member variables in the base class
and then only using some of them in each particular derived
class.

Mike


OK well now I understand. Two advantages of using virtual functions are

1) You get error checking, if a derived class tries to access a variable it
doesn't have then the base class virtual function can detect this (as in my
last post)
2) You save space, only the derived classes that have member variables will
actually have them, instead of all derived classes having them by virtue of
there being in the base class.

I think your approach to the design of a inheritance tree is a little
backward. You say 'if I'm going to add a virtual for every data member that
is added in a derived class' but really when you are designing this you
should be thinking of *all* the functionality you need in the base class.
Derived classes do not add functionality to this, they implement the base
class functionality in different specialised ways. At least that what the
books say, real world is sometimes a little more complex.

john
Jul 22 '05 #13

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

Similar topics

5
by: Tobias Windeln | last post by:
Hi! I'm looking for suggestions on object-based inheritance in Python. Automatic forwarding (often called delegation) in Python is easy: def __getattr__(self, attr): return...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
0
by: apb18 | last post by:
A bit of query plan strangeness. Suppose you have an inheritance tree such that the columns 'ID' and 'field' appear in the top level table, call that table XXX. tables YYY and ZZZ both inherit...
3
by: Darren | last post by:
I have a Person object and from that I'm inheriting other objects including Client, Carer, Doctor (and others). The Person object has a large number of properties and methods that I want available...
3
by: Hazz | last post by:
I am just beginning to design a Treeview display (winforms) for wine regions. Problem. Some wine growing regions belong to two counties. Eg. Carneros is in both Napa and Sonoma Counties. Although...
0
by: Mariano | last post by:
Hi, I have posted a bug of the forms designer that overwrites always the Icon, thus preventing Icon inheritance when you derive from a form. I am trying to overcome this by adding an ImageList in...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
4
by: Joe | last post by:
We are working on a project that requires 3rd parties (ie, consultants) to use our ASP.net webforms in a very reusable manner. The big catch is that we are not allowed to give them source. There...
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.