-
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
-
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.)
-
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 ??
-
I already told you. It doesn't make any sense for get/set because you got the essence of an class all wrong.
-
so i pass the information do the variable like i would in a function ???
-
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.
-
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.
-
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 ??
-
Could you post the contents of Circle.h?
-
How about you go around looking on a class tutorial?
-
cheers for the help has helped me out alot
-
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
-
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
-
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 ;)
-
so in the constructor do u just call the variables that u declared using the main ??