473,385 Members | 1,333 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.

Designing a class hierarchy with parents and children

Hello!
I want to design a class hierarchy with one base abstract class, let's
say CBase. I have a predicate that every object in the class hierarchy
must have a parent object of type from this class hierarchy but only
objects of some of these types can be a parent.

Example:

class CFruit : public CBase { };
class CBranch : public CBase { };

Class CBranch can be a parent for another object (CBranch or CFruit)
but CFruit cannot be a parent for any object.
It is tempting to put a CBase pointer in CBase class to fulfill
predicate that every object must have a parent but I want to force
proper object type for parent pointer by design structure and not the
runtime check (it may happend by accident the CFruit was made a parent
by upcast).

It is possible to put forward CBranch class declaration (which is a
base for other parent-type classes) in CBase class header file and put
a pointer to it in a base class but I do not want to show off any
derived classes in a base class header file.

It is possible to use an object factory which may try to guard proper
object creation for parent object but this don't convince me. Parent-
type objects may have different constructors than child-only-type
objects but this is hard to maintain.

Is there any design pattern for that problem?

May 12 '07 #1
3 3309
On May 12, 10:51 am, krzysztof.kono...@gmail.com wrote:
Hello!
I want to design a class hierarchy with one base abstract class, let's
say CBase. I have a predicate that every object in the class hierarchy
must have a parent object of type from this class hierarchy but only
objects of some of these types can be a parent.

Example:

class CFruit : public CBase { };
class CBranch : public CBase { };
What exactly are you trying to model? If you can't use your concrete
classes through an ABC pointer without casting, inheritance may be the
wrong approach.

What does your ABC interface look like?

May 12 '07 #2
On 12 Maj, 22:10, dave_mikes...@fastmail.fm wrote:
On May 12, 10:51 am, krzysztof.kono...@gmail.com wrote:
Hello!
I want to design a class hierarchy with one base abstract class, let's
say CBase. I have a predicate that every object in the class hierarchy
must have a parent object of type from this class hierarchy but only
objects of some of these types can be a parent.
Example:
class CFruit : public CBase { };
class CBranch : public CBase { };

What exactly are you trying to model? If you can't use your concrete
classes through an ABC pointer without casting, inheritance may be the
wrong approach.

What does your ABC interface look like?
I am building some hierarchy tree. I have a sort of objects: main
containers (scopes), internal containers (scopes), regular objects
(leaves).
Main contaitainers (scopes) can contain main containers and internal
containers and all regular objects.
Internal containers can contain internal containers and some subset of
regular objects.

I need to build a tree (analyze the hierarchy). I do not know at which
point of the hierarchy I start (main or intelnal container or regular
object).
I receive some starting point and its type in some representation
which I can dispatch with abstract factory.
I want to say to this starting point (object) : "build hierarchy!" and
it should know what it contains (if it can) and it can call its
children for building hierarchy (all objects can gather proper
information for building the hierarchy through some interface which is
already available in the design).

At the end I want to traverse all regular objects (leaves of the tree)
and make some action with them ( they need to ask their parents for
some informations which will be available only after building a tree
(at least the part ending with the current leaf)).

May 12 '07 #3
On May 12, 11:26 am, krzysztof.kono...@gmail.com wrote:
I am building some hierarchy tree. I have a sort of objects: main
containers (scopes), internal containers (scopes), regular objects
(leaves).
Main contaitainers (scopes) can contain main containers and internal
containers and all regular objects.
Internal containers can contain internal containers and some subset of
regular objects.
I think the Composite Design Pattern may be helpful. Here is a link
to a C++ report article that uses Composite and Visitor for a tree.
Not sure if it's the exact solution you need for your problem, but
maybe it will set you on the right track.

http://members.home.nl/r.f.pels/arti...Knowledge.html

May 12 '07 #4

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

Similar topics

4
by: james | last post by:
I have a custom UserControl, which can have many sub class levels derived from it. I want to be able to discover all the components at Load time, but the only components I can see from the base...
6
by: Ray Schumacher | last post by:
What is the feeling on using "parent" in a class definition that class methods can refer to, vs. some other organization ? Should all relevant objects/vars just be passed into the method as needed?...
6
by: Sashi | last post by:
class parent{ Parent(){}; ~Parent(){}; } Child: public Parent{ Child(){}; ~Child(){};
9
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
61
by: Sanders Kaufman | last post by:
I'm wondering if I'm doing this right, as far as using another class object as a PHP class property. class my_baseclass { var $Database; var $ErrorMessage; var $TableName; var $RecordSet;...
1
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
3
by: Andrew Meador | last post by:
I have searched and found where this has been discussed, but the suggestions I have found have all ended up tuning into debated about whether it needs to be done - but - as far as I can tell - I...
36
by: Roedy Green | last post by:
The only browser I have encountered that supports <colgroup><col class="behold"></colgroup> to apply a CSS style to a whole column, is Microsoft Internet Explorer. I have been told it SHOULD NOT...
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: 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:
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.