472,784 Members | 939 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Composite classes

I have a basic C++ question. Consider a composite class A containing
two data members B and C that are also class objects:

class A
{
private:
B objB; // B is a class
C objC; // C is class
public:
...etc...
}

Is there a way for objB to invoke a method defined in class C? For
instance,

void B::functionB(void)
{
objC.functionC(void);
}

What is the best way to "link" B and C?

Thanks.

Aug 5 '07 #1
6 3713
On Aug 5, 8:57 am, gccntn <gcc...@yahoo.comwrote:
I have a basic C++ question. Consider a composite class A containing
two data members B and C that are also class objects:

class A
{
private:
B objB; // B is a class
C objC; // C is class
public:
...etc...

}

Is there a way for objB to invoke a method defined in class C? For
instance,

void B::functionB(void)
{
objC.functionC(void);

}

What is the best way to "link" B and C?

Thanks.
Hi,

One idea is something like that:
void B::functionB(C& objC)
{
objC.functionC();
}
Anyway, you should pass an object or reference to an object of class
C. of course you can define a local C object inside functionB:
void B::functionB()
{
C objC;
objC.functionC();
}

It depends on your problem.

Regards,
Saeed

Aug 5 '07 #2
"gccntn" <gc****@yahoo.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
>I have a basic C++ question. Consider a composite class A containing
two data members B and C that are also class objects:

class A
{
private:
B objB; // B is a class
C objC; // C is class
public:
...etc...
}

Is there a way for objB to invoke a method defined in class C? For
instance,

void B::functionB(void)
{
objC.functionC(void);
}

What is the best way to "link" B and C?
It depends on what it is you are actually trying to achieve. If B needs to
invoke a method of C, then perhaps C should be a member of B. You have many
alternatives though.

( following is all untested code )
1. Have the constructor of B take a reference to C and store it.

class B
{
public:
B( C& c ): c(c) {}
void functionB();
private:
C& c;
}

void B::functionB()
{
c.functionC();
}

2. Have an initialization function for B take a pointer to C and store it.

class B
{
public:
B( ): c( NULL ) {}
void Init( C* cp ) { c = cp; }
void functionB();
private:
C* c;
}

void B::functionB()
{
if ( c )
c->functionC();
}

3. Best IMO, have B create it's own C

class B
{
public:
void functionB();
private:
C c;
}

void B::functionB()
{
c.functionC();
}

4. Have functionB accept a reference to C.

class B
{
public:
void functionB( C& c);
}

void B::functionB( C& c )
{
c.functionC();
}

5. Other less desirable methods (public instance of C, etc..)

The main quesiton is, WHY does B need to call a method of C?


Aug 5 '07 #3
On Aug 5, 3:26 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
"gccntn" <gcc...@yahoo.comwrote in message

news:11**********************@k79g2000hse.googlegr oups.com...


I have a basic C++ question. Consider a composite class A containing
two data members B and C that are also class objects:
class A
{
private:
B objB; // B is a class
C objC; // C is class
public:
...etc...
}
Is there a way for objB to invoke a method defined in class C? For
instance,
void B::functionB(void)
{
objC.functionC(void);
}
What is the best way to "link" B and C?

It depends on what it is you are actually trying to achieve. If B needs to
invoke a method of C, then perhaps C should be a member of B. You have many
alternatives though.

( following is all untested code )
1. Have the constructor of B take a reference to C and store it.

class B
{
public:
B( C& c ): c(c) {}
void functionB();
private:
C& c;

}

void B::functionB()
{
c.functionC();

}

2. Have an initialization function for B take a pointer to C and store it.

class B
{
public:
B( ): c( NULL ) {}
void Init( C* cp ) { c = cp; }
void functionB();
private:
C* c;

}

void B::functionB()
{
if ( c )
c->functionC();

}

3. Best IMO, have B create it's own C

class B
{
public:
void functionB();
private:
C c;

}

void B::functionB()
{
c.functionC();

}

4. Have functionB accept a reference to C.

class B
{
public:
void functionB( C& c);

}

void B::functionB( C& c )
{
c.functionC();

}

5. Other less desirable methods (public instance of C, etc..)

The main quesiton is, WHY does B need to call a method of C?- Hide quoted text -

- Show quoted text -

"The main quesiton is, WHY does B need to call a method of C?"

Good question. Say A (the composite class) is Car, B is Controls and C
is Engine. As I envision this, Controls must be able to tell Engine
when to Start, Accelerate, Stop and so on. However, please let me know
if anybody wants to suggest a different design. I'm open to
suggestions.

Thanks for all the good tips.

Aug 6 '07 #4
Hi!

gccntn schrieb:
"The main quesiton is, WHY does B need to call a method of C?"

Good question. Say A (the composite class) is Car, B is Controls and C
is Engine. As I envision this, Controls must be able to tell Engine
when to Start, Accelerate, Stop and so on. However, please let me know
if anybody wants to suggest a different design. I'm open to
suggestions.
Sound like the "have B have a pointer/reference to C" solution to me.

Frank
Aug 16 '07 #5
"gccntn" <gc****@yahoo.comwrote in message
news:11**********************@b79g2000hse.googlegr oups.com...
On Aug 5, 3:26 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>"gccntn" <gcc...@yahoo.comwrote in message

news:11**********************@k79g2000hse.googleg roups.com...


>I have a basic C++ question. Consider a composite class A containing
two data members B and C that are also class objects:
class A
{
private:
B objB; // B is a class
C objC; // C is class
public:
...etc...
}
Is there a way for objB to invoke a method defined in class C? For
instance,
void B::functionB(void)
{
objC.functionC(void);
}
What is the best way to "link" B and C?

It depends on what it is you are actually trying to achieve. If B needs
to
invoke a method of C, then perhaps C should be a member of B. You have
many
alternatives though.

( following is all untested code )
1. Have the constructor of B take a reference to C and store it.

class B
{
public:
B( C& c ): c(c) {}
void functionB();
private:
C& c;

}

void B::functionB()
{
c.functionC();

}

2. Have an initialization function for B take a pointer to C and store
it.

class B
{
public:
B( ): c( NULL ) {}
void Init( C* cp ) { c = cp; }
void functionB();
private:
C* c;

}

void B::functionB()
{
if ( c )
c->functionC();

}

3. Best IMO, have B create it's own C

class B
{
public:
void functionB();
private:
C c;

}

void B::functionB()
{
c.functionC();

}

4. Have functionB accept a reference to C.

class B
{
public:
void functionB( C& c);

}

void B::functionB( C& c )
{
c.functionC();

}

5. Other less desirable methods (public instance of C, etc..)

The main quesiton is, WHY does B need to call a method of C?- Hide quoted
text -

- Show quoted text -


"The main quesiton is, WHY does B need to call a method of C?"

Good question. Say A (the composite class) is Car, B is Controls and C
is Engine. As I envision this, Controls must be able to tell Engine
when to Start, Accelerate, Stop and so on. However, please let me know
if anybody wants to suggest a different design. I'm open to
suggestions.

Thanks for all the good tips.
A car HAS controls. Being such, C should be inside B.

I would go with method 3. B has it's own C.

class Car
{
public:
void Acceleratre( int Speed ) { C.Accelerate( Speed ); }
private:
Controls control;
}

Aug 16 '07 #6
Jim Langston wrote:
>
A car HAS controls. Being such, C should be inside B.

I would go with method 3. B has it's own C.
Hmmmm, at this level, I'm not sure I agree. A car has an engine,
windows, trunk, radio, etc. Now the engine has it's controls (starter,
throttle), a window has it's controls (crank or button), and the radio
has it's own controls (tuner, volume, etc). These controls are all
accessible from the car, but they are not the car's controls. That is,
they don't control the car, but rather a component of the car. I
probably would not model the controls as a separate object at all. Why
not just tell the engine to turn on, accelerate, etc directly? If you
try to get too close to reality things get much more complex without a
gain to offset it.

Having said all that, if you still wanted the car to have controls, then
I might consider using the observer pattern with the engine observing
the controls.

joe
Aug 16 '07 #7

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

Similar topics

4
by: Matan Nassau | last post by:
This question is a little embarrassing... I have a boolean expressions interpreter with its composite (see Go4's "Design Patterns" book). here is a simple leaf of the composite: class Constant :...
0
by: Michael Andersson | last post by:
Given a set of classes class A { enum [ ID = 0x0001} }; class B { enum [ ID = 0x0002} }; class B { enum [ ID = 0x0004} }; I wish to generate a composite class, perhaps using something like...
0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
0
by: AndrewF | last post by:
Hi there. I am royally stuck with this so any help would be greatly appreciated. Basically I have a set of classes that create composite controls for the user. One of these is an upload object...
0
by: multiformity | last post by:
Ok, so I have gone off and documented the lifecycle of a page with a custom composite control on it. You can find that document here: http://www.ats-engineers.com/lifecycle.htm Now, I am...
3
by: Beavis | last post by:
I hate to repost a message, but I am still at the same point where I was when I originally posted, and hopefully someone else will see this one... Ok, so I have gone off and documented the...
2
by: pkpatil | last post by:
Hi, Can a private composite object in a class access private or protected members of base class? For e.g. class composite { void memberFunction(); };
2
by: lwhitb1 | last post by:
Does anyone have any input on setting up my CAB application so that the application is thread safe, and cached appropiately? I read that this can be managed through Services, and dynamic injection....
6
by: shapper | last post by:
Hello, I am working in a class library with various custom controls. In which cases should a control inherit Control, WebControl and CompositeControl classes? And when should a custom...
4
by: Peter Morris | last post by:
I am not sure what you are asking. You seem to be asking how to implement a plain IEnumerable on a composite structure, but then your example shows a flat structure using "yield". Your subject...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.