Thread: get and set ???

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A constructor is the initialization phase. It does whatever is necessary to use your object. Initialization code.
    Typically what your car does when you turn on the engine.
    Does it ask the radius or if you want to put on your stereo? No? Then nor should your class.
    There are member functions for that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    no. you create an instance of your class first, then set members.

    Code:
    float r;
    
    cout >> "Enter radius: ";
    cin << r;
    circle *c = new circle();
    c->radius = r;

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Better to do it this way:
    Code:
    float r;
    cout >> "Enter radius: ";
    cin << r;
    circle c;
    c.setradius(r);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    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.

  5. #20
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    thank you !!!! it has helped alot

  6. #21
    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.

  7. #22
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by Elysia View Post
    Better to do it this way:
    Code:
    float r;
    cout >> "Enter radius: ";
    cin << r;
    circle c;
    c.setradius(r);
    are __property declarations not standard? why call the setter as opposed to setting the property variable?

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> are __property declarations not standard?
    No, they are not part of standard C++. You also shouldn't be using new unless necessary, and the << and >> are backwards in the code example.

  9. #24
    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.

  10. #25
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    is this the right place to ask for the radius its in the circle.ccp file
    Code:
    void Circle::setradius(float)
    {
    	printf("please enter the radius of the Circle");
    	scanf ("&#37;f",&radius); 
    }
    thanks for your time

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think we are going round in circles (pun intended) here. You are back to the same "setradius" function that you suggested in your first post, even though the follow-up posts to that should have made it fairly clear that it's NOT the right thing to do.

    You have plenty of examples in the previous posts. Please take a few moments to read those posts, then post questions if it's still not clear.

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

  12. #27
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by gda2004 View Post
    is this the right place to ask for the radius its in the circle.ccp file
    Code:
    void Circle::setradius(float)
    {
    	printf("please enter the radius of the Circle");
    	scanf ("&#37;f",&radius); 
    }
    thanks for your time
    circle.cpp is not the right place to ask for it. main, or some user-interaction class or routine is the right place to ask for it.

  13. #28
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    y can i not have a function declaring the variable radius with int the header named Circle::Circle() ??? i have now watch a video http://www.youtube.com/watch?v=TDRoQcGGH1Y i am getting conflicting messages

    thank you for your time

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Start listening to what people are telling you. Your car does not ask if you want to enable your stereo when you start the engine. So likewise you shouldn't ask for information when the class is being constructed.
    The class is an object! It does not ask for information. It's your program's work to do that. Then the program manipulates the object in such a fashion that everything works to satisfaction.

    Your class isn't a class, it's meant to be a circle. A real circle. And a real circle doesn't ask for its radius. You, who draw the circle sets its radius.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i am getting conflicting messages
    There is no absolute rule on this issue. I would agree with the people in this thread that better practice is to separate the requesting of the information from the user out of the class. However, you can still do it the other way like in your video.

    To answer your original question, a simple return radius; should work.

Popular pages Recent additions subscribe to a feed