473,605 Members | 2,590 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 1718
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*******@yaho o.com> wrote in message
news:z1******** **********@news svr29.news.prod igy.net...
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************ ******@newssvr2 9.news.prodigy. net>,
Tony Young <jd*******@yaho o.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
16604
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 ...</p>
3
19774
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 that shows which table should loaded first, second,third... Your help would be highly appreciated. maricel.
3
2227
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
1013
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 functionality required for all classes, however I need to have that 10% functionality that is left in the derived classes. What I would like to do is have the method in the base class be able to call the method in the derived classes, it should do this...
4
3512
by: Efi Merdler | last post by:
Hi, I have an xml file with the following structure: <Dictionary> .... <Nested> <Entry> .... </Entry> <Entry> ....
4
1033
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
6420
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 member function that has just been accessed by a child. The program goes through some condition statements, comes true to a situation where it's true, and now needs to tell the child to do child member function x. Is that possible? class parent{ ...
5
2724
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
1500
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 the issue. A1 and B1 share a method (void m() ) AND B2 doesn't have that
0
8004
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8418
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8288
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5886
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3958
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2438
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 we have to send another system
1
1541
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1271
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.