473,503 Members | 479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parent first or child first? (a class structure question)

Hi,

I am very interested to know which one of the two functions below is
used more often by people. The 1st way intends to let B prepare the
data and A perform the action. The 2nd way intends to let A initialize
data in a general way and B specialize the data. My question may be
dumb because it depends on needs. But I am preparing a huge class
structure without knowing every small details now. I wonder if I can
formalize a rule in this regard to make the future development more
robust. I appreciate any advise. Thanks for your time. Tony

void B::foo() // B is a child of A
{
...
A::foo();
}

void B::foo()
{
A::foo();
...
}
Mar 25 '06 #1
3 1710
Tony Young wrote:
But I am preparing a huge class structure without knowing every small
details now.


You are asking "I need to design a class setup so that I will never ever
change the design, so I must not make any mistakes now."

The purpose of OO is to be changed, but this leads to bugs. So write unit
tests for every detail of your program, and only write enough features to
satisfy the most important business needs. Get the program ready, then
deliver it (either to a real user or a proxy).

Then add the next features, refactoring the design as you go. If you need to
invert the relationship between A and B, you can move individual statements
between them, and you can run all the unit tests after each tiny step. If
they fail, you don't need to debug. You just Undo the change away, and then
try again.

Put another way, unit tests are always more important than up-front design,
and they reduce the risk that you get stuck with an early design mistake.

Download CppUnit, or CppUnitLite, and use its example programs to practice
writing tests.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Mar 25 '06 #2

"Tony Young" <jd*******@yahoo.com> wrote in message
news:z1******************@newssvr29.news.prodigy.n et...
Hi,

I am very interested to know which one of the two functions below is used
more often by people. The 1st way intends to let B prepare the data and A
perform the action. The 2nd way intends to let A initialize data in a
general way and B specialize the data. My question may be dumb because it
depends on needs. But I am preparing a huge class structure without
knowing every small details now. I wonder if I can formalize a rule in
this regard to make the future development more robust. I appreciate any
advise. Thanks for your time. Tony

void B::foo() // B is a child of A
{
...
A::foo();
}

void B::foo()
{
A::foo();
...
}


I never use either technique. Initializing data is what constructors are
for:

class A
{
public:
A(some_type thing);
...
};

class B : public A:
{
public:
B(some_type thing, other_type other_thing) : A(some_type thing), ...{}
...
};

This simple paradigm seems to always work for abstract or concrete data
types.

Cy
Mar 25 '06 #3
In article <z1******************@newssvr29.news.prodigy.net >,
Tony Young <jd*******@yahoo.com> wrote:
Hi,

I am very interested to know which one of the two functions below is
used more often by people. The 1st way intends to let B prepare the
data and A perform the action. The 2nd way intends to let A initialize
data in a general way and B specialize the data. My question may be
dumb because it depends on needs. But I am preparing a huge class
structure without knowing every small details now. I wonder if I can
formalize a rule in this regard to make the future development more
robust. I appreciate any advise. Thanks for your time. Tony

void B::foo() // B is a child of A
{
...
A::foo();
}

void B::foo()
{
A::foo();
...
}


What is used most often would be something like this I expect:

class Base
{
public:
void foo();
protected:
virtual void do_foo() = 0;
};

void Base::foo() {
// do something
do_foo();
// do something else
}
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 26 '06 #4

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

Similar topics

6
16596
by: Veerle | last post by:
Hi, Somewhere in my html page, I have the following code: <div id="main-body"> <h2>Header text</h2> <p>Some other text ...</p> ..... <h2>Another header text</h2> <p>Some more other text...
3
19738
by: maricel | last post by:
Is there a way to list (using db2 command or catalogs) to list hierarchy of table parent & child relationship: 1) A list that shows which table should be deleted first,second,third... 2) A list...
3
2218
by: William Krick | last post by:
Given this XML <parent> <child name="billy"> <child name="sue"> </parent> I can use this to process each child... <xsl:for-each select="child">
9
1001
by: Sid Price | last post by:
Hello, I have a problem trying to figure out the following design issue. I have a base class and a number of classes derived from that base. A method in the base class covers 90% of the...
4
3500
by: Efi Merdler | last post by:
Hi, I have an xml file with the following structure: <Dictionary> .... <Nested> <Entry> .... </Entry> <Entry> ....
4
1026
by: =?iso-8859-1?B?UmVu6SBKZW5zZW4=?= | last post by:
Hello I got a problem with a class i'm trying to create. the problem is that i would like to use the dot when i access some parametes in the class like. Connect()
2
6410
by: ChaoticDALO | last post by:
Hello, I am wondering if this is possible. If I have a parent class and a child that inherits the parent, the child can access a parents member function right? Well let's say we are in the parent...
5
2721
by: cbmeeks | last post by:
Hello all. I have a project that I am working on and I need some suggestions. First, I have a class that contains a value and a reference to a parent class. For example: public class Data
2
1492
by: K Viltersten | last post by:
Suppose there is the following class structure. class S {...} class A : S {...} class B : S {...} class A1 : A {void m(){...}} class B1 : B {void m(){...}} class B2 : B {...} Just to clarify...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.