473,221 Members | 2,115 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,221 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 1700
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
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
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
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
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
by: Efi Merdler | last post by:
Hi, I have an xml file with the following structure: <Dictionary> .... <Nested> <Entry> .... </Entry> <Entry> ....
4
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
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
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
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
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.