Thread: some questions on classes

  1. #1
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916

    some questions on classes

    okay, first of all...I've read that in a well designed class, none of the data members should be public, all data should be accessed through member functions. However, I think I'm about to be designing some classes with a lot of data members that will need to be both read and changed from outside the class. I'm a demon for speed in my code, and if I can have small code that is also fast, that's just great. My questions are as follows: First, is there an advantage to having a member function in the class to return the value of a data member, and another to change it, over simply having the data member public? Also, does the overhead involved in calling a function apply to member functions in classes? I assume it does, and if so, wouldn't it be slower to have functions to access the data than to just make the data public?
    Away.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    well the thing that people usually don't get about this topic is that SetVariable and GetVariable functions doesn't usually need to be excessively used. The fact is a class should handle its variables internally for the most part. If you've designed it such that your code will look like this:
    Code:
    ClassName obj;
    obj.SetA(1);
    obj.SetB("blah");
    .
    .
    .
    obj.SetN(57);
    well, quite honestly you've designed it poorly.

    If a class needs a certain number of variables EVERY time before it is valid then perhaps you want them to be parameters on the constructor. If you are repetitively setting, perhaps you should stop looking at your class as a "state machine" but as a "thing" with abilities.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    well, a bunch of classes are going to be interacting with each other, that's why there is going to be a lot of data reading...what would you suggest?
    Away.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    impossible to suggest an architecture when the problem is not known.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by FillYourBrain
    If a class needs a certain number of variables EVERY time before it is valid then perhaps you want them to be parameters on the constructor. If you are repetitively setting, perhaps you should stop looking at your class as a "state machine" but as a "thing" with abilities.
    With this in mind, how many arguments would you consider is too many in the constructor?
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    well if there's more than a couple I would say the class probably needs to be broken up. Really if there's 10 or 20 variables that have to be set on a class before it works it's kinda like one of those "God classes". These are what people do when they're just trying to do OOP without being structured. I wouldn't do more than 2 or three probably. But that's not a rule so much.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    theoretical game development...
    suppose you have a plant class, which needs to find out from the ground class what type of ground it is and how much water is in it to find out if the plant can grow...and say a seed class has to find out from the plant class what kind of plant it is (have it reference the plant class with pointers or something...) That example is over simplified, we are thinking about designing an entire game where every object interacts completely with the environment, and with this architecture it would be easy to add new types of objects to the world which would interact with all the rest...but as you can see, it requires a lot of data transfers on a lot of different variables in the classes...some will obviously be private, but would it be better just to make the ones that are in high demand be public?
    Away.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The reasons for making members private does not really apply to your predicament. The overhead for the function calls and all those get/set methods would just add confusion as well. Ignore the naysayers and do it your way
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Sebastiani
    Ignore the naysayers and do it your way
    Is this for real? What naysayers? Jeez! Read the thread first. Nobody wants to use a mess of sets and gets!
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by blackrat364
    theoretical game development...
    suppose you have a plant class, which needs to find out from the ground class what type of ground it is and how much water is in it to find out if the plant can grow...and say a seed class has to find out from the plant class what kind of plant it is (have it reference the plant class with pointers or something...) That example is over simplified, we are thinking about designing an entire game where every object interacts completely with the environment, and with this architecture it would be easy to add new types of objects to the world which would interact with all the rest...but as you can see, it requires a lot of data transfers on a lot of different variables in the classes...some will obviously be private, but would it be better just to make the ones that are in high demand be public?
    Bingo! That's the problem! You're Gets and such that you are doing are strictly for the purpose of finding out if the plant can grow!

    But....

    The work that a class does should be internal. You should tell the plant class "Grow(-- ground parameters -- time parameters -- etc --)" and the class itself should check to see if it can or how much. That's the problem. Overuse of sets and gets is never really necessary.

    I assume the ground class will contain some number of plant classes.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    All data should be hidden within its class, otherwise it becomes difficult to determine which portion of the system's functionality is dependant on that data. This is one of the major Heuristics of object oriented programming. Think of what behavior you want a class to perform and the data that it requires, but each class should only capture one key abstraction.

  12. #12
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Originally posted by FillYourBrain
    Bingo! That's the problem! You're Gets and such that you are doing are strictly for the purpose of finding out if the plant can grow!

    But....

    The work that a class does should be internal. You should tell the plant class "Grow(-- ground parameters -- time parameters -- etc --)" and the class itself should check to see if it can or how much. That's the problem. Overuse of sets and gets is never really necessary.

    I assume the ground class will contain some number of plant classes.
    This is more or less correct. A class should only use operations in the public interface of another class or have nothing to do with that class. Do not create implied dependencies between the implementations of two classes.

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    TK, how come every once in a blue moon you give useful input? I don't get it. You almost seem like a regular guy right now.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Also, for Get & Set functions, you will benefit if you can implement the functions as inline. That will save on the overhead of a function call if you are worried about the speed.

    Other than that, read what FillYourBrain has written

  15. #15
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I wouldn't promote Gets and Sets public accessors, however I can't quite remember if this is correct, but Gets and Sets is reasonable under some circumstances if they are made protected, and used by iherited objects. There might be a reason why you would want to support that behavior. I haven't used OOP enough though to remember the details.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions about classes(implementation files)
    By Link_26 in forum C++ Programming
    Replies: 6
    Last Post: 06-22-2006, 09:48 AM
  2. Classes being able to use other classes functions
    By rainmanddw in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2006, 11:19 AM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM
  5. 2 questions - classes and variables
    By phantom in forum C++ Programming
    Replies: 8
    Last Post: 09-25-2001, 09:22 PM