943,817 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 899
  • C++ RSS
May 1st, 2008
0

Help with sphere member functions

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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)
Similar Threads
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
May 1st, 2008
0

Re: Help with sphere member functions

Here is an example
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
May 1st, 2008
0

Re: Help with sphere member functions

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.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
May 1st, 2008
0

Re: Help with sphere member functions

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
May 1st, 2008
0

Re: Help with sphere member functions

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
May 2nd, 2008
0

Re: Help with sphere member functions

well nm then I suppose, I'll just ask about it in class tomorrow. It's probably the safer bet anyway
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
May 2nd, 2008
0

Re: Help with sphere member functions

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.
C++ Syntax (Toggle Plain Text)
  1. sphere sp;
  2. for(int i = 20; i < 31; i++)
  3. {
  4. sp.setradius(i);
  5. sp.print();
  6. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
May 2nd, 2008
1

Re: Help with sphere member functions

Click to Expand / Collapse  Quote originally posted by henpecked1 ...
C++ Syntax (Toggle Plain Text)
  1. sphere::sphere() //default constructor
  2. {
  3. int Radius=0;
  4. }
wow.

what did you expect that code to do?
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: please help with code >> expected primary-expression?
Next Thread in C++ Forum Timeline: Count newline character





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC