Help with sphere member functions

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Help with sphere member functions

 
0
  #1
May 1st, 2008
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.

  1.  
  2. sphere(); //default constructor
  3.  
  4. sphere(int inradius); //parameterized constructor or function for inputting radius
  5.  
  6. void setradius(int newradius); //new sphere object
  7.  
  8. 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)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,659
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Help with sphere member functions

 
0
  #2
May 1st, 2008
Here is an example
  1. // create instance and use default constructor
  2. sphere obj;
  3. // create another instance but specifiy the radius
  4. int radius = 20;
  5. sphere obj1(radius);
  6.  
  7. // now change the radius
  8. radius = 21;
  9. obj.setradius(radius);
Last edited by Ancient Dragon; May 1st, 2008 at 10:37 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Help with sphere member functions

 
0
  #3
May 1st, 2008
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?
Last edited by henpecked1; May 1st, 2008 at 10:48 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,659
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Help with sphere member functions

 
0
  #4
May 1st, 2008
>>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.
Last edited by Ancient Dragon; May 1st, 2008 at 10:51 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Help with sphere member functions

 
0
  #5
May 1st, 2008
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

  1.  
  2. sphere::sphere() //default constructor
  3. {
  4. int Radius=0;
  5. }
  6.  
  7. void sphere::setradius(int newRadius)
  8. {
  9. radius = newRadius <= 0 ? 1 : newRadius;
  10. } //to avoid the "0" issue and set a new radius
  11.  
  12. int sphere::getradius()
  13. {
  14. while (radius <0)
  15. {
  16. cout << "Radius must be a positive value." << endl;
  17. }
  18. return (radius);
  19. // the getter with another means to avoid a 0 radius
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Help with sphere member functions

 
0
  #6
May 2nd, 2008
well nm then I suppose, I'll just ask about it in class tomorrow. It's probably the safer bet anyway
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,659
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1500
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Help with sphere member functions

 
0
  #7
May 2nd, 2008
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.
  1. sphere sp;
  2. for(int i = 20; i < 31; i++)
  3. {
  4. sp.setradius(i);
  5. sp.print();
  6. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Help with sphere member functions

 
1
  #8
May 2nd, 2008
Originally Posted by henpecked1 View Post
  1. sphere::sphere() //default constructor
  2. {
  3. int Radius=0;
  4. }
wow.

what did you expect that code to do?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 600 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC