C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-03-2006, 09:28 PM   #1
Registered User
 
Join Date: Jul 2006
Location: US
Posts: 14
Unhappy help on class coupling

Hi and thanks for reading.
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;
};
I wrote the various member definitions and all is fine and dandy.
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];
};
If I do this how are the two distinct class supposed to work together? Can the computeForceResults receive an instance of the Force class?
In other words, is it OK to do something like:
Code:
int computeForceResults(Force &force);
The compiler doesnt recognize that as valid.
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   Reply With Quote
Old 07-04-2006, 01:20 AM   #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   Reply With Quote
Old 07-05-2006, 12:01 PM   #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   Reply With Quote
Old 07-05-2006, 12:15 PM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,361
Quote:
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.
If you want to do it that way, then they should return a bool instead. However, in the case of the reading and printing functions, you may find it better to overload operator>> and operator<< with istream& and ostream& respectively. Your readForceData() member function looks like it should be a constructor and/or a setter function instead. Your printForce() member function should be const, but that wouldnt be a problem if you replaced it with an overloaded operator<<. You do not need to explicitly define a destructor since the compiler generated version will do fine.
__________________
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:33 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22