| | |
Help with sphere member functions
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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.
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)
C++ Syntax (Toggle Plain Text)
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
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
C++ Syntax (Toggle Plain Text)
// 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);
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.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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?
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.
>>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.
>>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.
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
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
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)
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
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.
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)
sphere sp; for(int i = 20; i < 31; i++) { sp.setradius(i); sp.print(); }
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.
•
•
Join Date: Nov 2006
Posts: 224
Reputation:
Solved Threads: 31
wow.
what did you expect that code to do?
what did you expect that code to do?
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: please help with code >> expected primary-expression?
- Next Thread: Count newline character
Views: 600 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






