![]() |
| | #1 |
| Registered User Join Date: Jul 2006 Location: US
Posts: 14
| I have a question about class design, this is my first effort in C++ so please be forgiving! The problem: I have to design a class that represent a mechanical force element. Let's call this Force. Each Force has 6 components and some methods, to initialize and read some data, filling up the properties of the Force. So my class looks something like this: Code: class Force
{
public:
Force(); // default constructor
~Force(); // default destructor
int initializeForce();
int printForce();
int readForceData(int, int, int, int, int, int, int);
private:
ForceComponent m_x;
ForceComponent m_y;
ForceComponent m_z;
ForceComponent m_rx;
ForceComponent m_ry;
ForceComponent m_rz;
};
In other words I'm able to initialize the Force object that I instantiate from this class, and I can verify that it contains all the data as read from file. This force object also should be evaluated: each component should receive a numerical value and, based on the data for that particular component, it should return a value, something like F = k x where: - k is part of the data that I read - x is the input to the component and - F is the output value. Seems pretty simple. The problem is that I do not know if the results (6 values, one for each direction) should be part of my Force class. My intuition tells me that they should not private members and that they should go in a separate class alltogether. Code: class ForceResults
{
public:
int initializeForceResults();
int computeForceResults( ???? );
private:
float m_x[6];
float m_f[6];
};
In other words, is it OK to do something like: Code: int computeForceResults(Force &force); Essentially I'm trying to understand the concept of class coupling and how to manage the separation between a class that contains the DATA and one that contains the RESULTS that are computed using that DATA. Any help much appreciated!!!! Thanks & regards, Andrea. |
| andrea72 is offline | |
| | #2 |
| The larch Join Date: May 2006
Posts: 3,222
| I'm not sure why you are trying to separate DATA and RESULTS. If the results are characteristic to the force, their natural place would be in the Force class, I think. Doesn't ForceResults already have the data (m_x) anyway, so you're just copying it around? So, I would add private data members for results in the Force class. Add a method to compute results (why would it return an int, I think it should only modify the private data of the class object and return void). Add a method to output the results, or change the printForce method to output them (who needs to see the initial data?) By the way, if k is part of the data from the file (gravity constant?) then you might need a place to store that too. Also, I think the initialize... methods might be doing what the constructor should do. And why do they return int, what are going to do with the return value? By the way, if you want to go with two classes, ForceResults methods (computeForceResult()) cannot access private data of the Force object, unless you make it public, or declare Force as a friend of ForceResults. (I wouldn't do it that way, it's much more confusing than needs be.) |
| anon is offline | |
| | #3 |
| Registered User Join Date: Jul 2006 Location: US
Posts: 14
| Well my idea was to have all the function members returning "int" to signify the success of the operation. Then I would check the return value from the member function call. For example the member function called readForceData should return a 0 for success and a 1 for failure. As for the k constant yes that is part of the private members of the class, it just doesn't appear becuase I was making an example, but they are part of the various ForceComponent classes. Thanks for your suggestions, I now agree that the Force results can be part of the Force class. Andrea. |
| andrea72 is offline | |
| | #4 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,361
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with FIFO QUEUE | jackfraust | C++ Programming | 23 | 04-03-2009 08:17 AM |
| Class design problem | h3ro | C++ Programming | 10 | 12-19-2008 09:10 AM |
| Defining derivated class problem | mikahell | C++ Programming | 9 | 08-22-2007 02:46 PM |
| matrix class | shuo | C++ Programming | 2 | 07-13-2007 01:03 AM |
| Abstract class problem | VanJay011379 | C++ Programming | 9 | 07-31-2002 01:30 PM |