help on class coupling

This is a discussion on help on class coupling within the C++ Programming forums, part of the General Programming Boards category; Hi and thanks for reading. I have a question about class design, this is my first effort in C++ so ...

  1. #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.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.)

  3. #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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,625
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    1
    C++ Coupling: OK, what are the options in using multi-class programs. At least some of classes could be made resusable -- GUI, Input, Output, etc, and then there are specific classes for specific problems. How best to communicate variables and messages?

    Thanks in advance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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