471,613 Members | 2,587 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1592
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by William Krick | last post: by
9 posts views Thread by Sid Price | last post: by
4 posts views Thread by Efi Merdler | last post: by
4 posts views Thread by =?iso-8859-1?B?UmVu6SBKZW5zZW4=?= | last post: by
5 posts views Thread by cbmeeks | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.