I've been given an assignment to write code for a sphere class. Now the point of this lesson is really formatting I/O, so they've given some member functions for the class. The problem is I don't know what they are all for. I've included them below and what I think they are supposed to do, so clue me in if I'm wrong.

sphere();  //default constructor

sphere(int inradius); //parameterized constructor or function for inputting radius

void setradius(int newradius); //new sphere object

int getradius(); // getter for the radius private data member

and then the rest are just calculation member functions for volume, diameter, and such.

If I'm right, I'm not sure how I call the first object in main and then replace it with a new instance of the object with a different radius value (The values are supposed to be 20 through 30 without creating an array of sphere objects)

Here is an example

// create instance and use default constructor
sphere obj; 
// create another instance but specifiy the radius
int radius = 20;
sphere obj1(radius);

// now change the radius
radius = 21;
obj.setradius(radius);

I take it I'm right about what the functions are for then.

So after I call the default to create an instance of the object, for the next one, can I set the radius within the function or will it be cleaner to define radius in main and then call it?

Further, when I set the new radius, can it be done in a loop inside that function, rather than calling it individually for 21, 22, 23, 24 etc or should it be done the same way as when I set the first radius?

>>Further, when I set the new radius, can it be done in a loop inside that function, rather
>>than calling it individually for 21, 22, 23, 24 etc?
I don't know. You probably should post the exact problem.

Warning: be cautious of doing that because some professors may consider it cheating.

declare and define free functions that will take a float/int and output it to the screen in decimal, hex, binary and scientific notation. The values passed will be values returned from member functions of a user defined sphere class.

given an object of a sphere class with a single radius data member. Produce member functions that compute and return values for circumference and volume.

using this class and the free functions print out the data in columns for spheres that have a radii of 20 to 30 in increments of 1.

Then the instructor gives the member functions posted above (I've already written the calculation functions) and says onlyh one sphere object need exist at a time. DO NOT declare an array of sphere objects.

That is the problem as given. I was just confused as to what some of the member functions of the class would be, and given those functions, how would they be implemented.

I made an attempt at a couple, but the confusion stopped me because I also began to wonder how I was going to limit the objects to 20 through 30 given the member functions that he wrote if that's not confusing enough

sphere::sphere()        //default constructor
{
	int Radius=0;
}

void sphere::setradius(int newRadius)
{
	radius = newRadius <= 0 ? 1 : newRadius;
}    //to avoid the "0" issue and set a new radius

int sphere::getradius()
{
	while (radius <0)
	{
		cout << "Radius must be a positive value." << endl;
	}
	return (radius);
// the getter with another means to avoid a 0 radius

well nm then I suppose, I'll just ask about it in class tomorrow. It's probably the safer bet anyway

in the constructor the default radius should be 1, not 0 because that's what you make it in setradius()

you don't need the loop in getradius(). Just return the radius.

To answer your question: The instructions aren't very clear about the 20-30 thing but I suppose a loop might be the best way.

sphere sp;
for(int i = 20; i < 31; i++)
{
    sp.setradius(i);
    sp.print();
}
This question has already been answered. Start a new discussion instead.