Thread: get and set ???

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    24

    get and set ???

    I have a good idea of what i need to put into the set but the get i am not sure about if anyone would help I would be greatfull
    Code:
    float Circle::getradius()
    {
    /////????
    }
    void Circle::setradius(float)
    {
    	printf("please enter the radius of the Circle");
    	scanf ("%f",&radius); 
    }
    thank you for your time

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Your code is all wrong. A class is an object. The object itself does not ask for the information. That's the job of the function that uses the object.
    Since this is a circle, the circle must have a radius. But the class itself is the circle. That is the essence of objects. Thus, you can manipulate the circle by setting its radius, and likewise, you cal also get or retrieve the radius later.
    (A circle can't speak, so it can't ask for information.)
    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.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    this is in the .cpp file i am using this so i am trying to set the raduis so i dnt put anything in the get part ??

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I already told you. It doesn't make any sense for get/set because you got the essence of an class all wrong.
    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.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    so i pass the information do the variable like i would in a function ???

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, set takes an argument that specifies the radius and the function the uses the object asks the user and then calls the member function.
    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.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You have the class declaration somewhere and this class has a float member for radius? The get and set function should allow the outside code to retrieve and set the value of this member variable.

    I think you should be able to see what getters and setters do in any beginner's tutorial on classes.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    ok not 100% sure what was ment by that lmao I simple create a new function and pass the info into the variable in Circle.h ??? but do i creat this new function within Cicle.cpp ??

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Could you post the contents of Circle.h?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How about you go around looking on a class tutorial?
    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.

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    cheers for the help has helped me out alot

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    I have looked up a tutorial i would just like to know if this is where u put the code
    Code:
    Circle::Circle(void)
    {
    printf ("please enter the radius of the circle");
    scanf ("%f",&radius);
    }
    thank you for your time

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

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

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    24
    so in the constructor do u just call the variables that u declared using the main ??

Popular pages Recent additions subscribe to a feed