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

T

Tony Young

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();
...
}
 
P

Phlip

Tony said:
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.
 
C

Cy Edmunds

Tony Young said:
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
 
D

Daniel T.

Tony Young said:
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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top