Thread: get and set ???

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, the circle constructor is not where you put "ask user questions" code, in general.

    I would put the question in main() or some other "not part of the class" function.

    And strictly speaking, if you use C++, you should formulate your question using "cout" and "cin".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    what you'd want to do is something like this:

    Code:
    class circle
    {
            private:
            float fArea;
            float fRadius;
            float fDiameter;
            float fCircumference;
            public:
            __property void set_radius(float r)
            {
                    fRadius = r;
                    fArea = PI*r*r;
                    fDiameter = 2*r;
                    fCircumference = 2*PI*r;
            }
            __property float get_radius()
            {
                    return fRadius;
            }
            //related getters and setters for other private data members...
    };
    getters and setters are useful for automatically updating related members, not for actually getting and setting the variable itself, that's what the = operator is for

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by m37h0d View Post
    what you'd want to do is something like this:

    Code:
    class circle
    {
            private:
            float fArea;
            float fRadius;
            float fDiameter;
            float fCircumference;
            public:
            __property void set_radius(float r)
            {
                    fRadius = r;
                    fArea = PI*r*r;
                    fDiameter = 2*r;
                    fCircumference = 2*PI*r;
            }
            __property float get_radius()
            {
                    return fRadius;
            }
            //related getters and setters for other private data members...
    };
    getters and setters are useful for automatically updating related members, not for actually getting and setting the variable itself, that's what the = operator is for
    But if you are keeping LOTS of circles, storing all the area, diameter and circumference is a bit excessive, as they can all be calculated quite easily when needed. All you have to STORE in the function is the radius.

    Using getter/setter functions will allow you to CONTROL what values are set, and to hide the internal representation.

    For example, if we produce a class like you have above, and then decide that we can actually find the right circle much faster by simply storing the radius [because it takes up 1/4 of the space, and using less memory means we can walk through more of them in the same amount of time, approximately 4 times faster in fact], and calculating the other components, then we have to change ALL the code that uses area, circumference and diameter. If on the other hand, you have a function getArea() that returns the area, we can implement the area calculation either as part of the setRadius() or as part of the getArea() function - more freedom, less dependancy on the class data structure itself.

    And of course, if fArea is private, you can't use = to set it anyways.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by matsp View Post
    But if you are keeping LOTS of circles, storing all the area, diameter and circumference is a bit excessive, as they can all be calculated quite easily when needed. All you have to STORE in the function is the radius.

    Using getter/setter functions will allow you to CONTROL what values are set, and to hide the internal representation.

    For example, if we produce a class like you have above, and then decide that we can actually find the right circle much faster by simply storing the radius [because it takes up 1/4 of the space, and using less memory means we can walk through more of them in the same amount of time, approximately 4 times faster in fact], and calculating the other components, then we have to change ALL the code that uses area, circumference and diameter. If on the other hand, you have a function getArea() that returns the area, we can implement the area calculation either as part of the setRadius() or as part of the getArea() function - more freedom, less dependancy on the class data structure itself.

    And of course, if fArea is private, you can't use = to set it anyways.

    --
    Mats
    all depends on whether RAM or CPU is more precious to you, wouldn't it? i definitely see that in some situations, that might be preferable.

    i don't usually deal with situations like the one you describe. i usually have RAM to burn, and would prefer to execute a loop with less computational overhead.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by m37h0d View Post
    all depends on whether RAM or CPU is more precious to you, wouldn't it? i definitely see that in some situations, that might be preferable.

    i don't usually deal with situations like the one you describe. i usually have RAM to burn, and would prefer to execute a loop with less computational overhead.
    You obviously haven't tried to get a memory benchmark to run faster... :-)

    Anyways, my point isn't that you HAVE to do it either way - you should not make assumptions about which is better until you have measured one or the other. Both you and I have no idea what this circle class is going to be used as. But if you have getter functions that give you the area, then you CAN change the way the area is calculated [early or late]. This is much harder if the code is directly accessing the member variables in the code. If you are interfacing to an application that is already compiled (e.g. a customer application), you can not change the internal data at all in the class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed