Ok so I am making a class "Box" with private variables and I have made public member functions to be able to populate the private variables right? So my question is can i use the "::" scope operator for my public member functions outside of my class to populate my private variables if i have prototyped the member functions inside of class Box?
in the section Member initialization in constructors
One should only need set functions if the value of member variables need to change after the object is created. Generally there should be some checking to see that the new values or the parameters supplied make sense, or that making changes are authorised, as in setBankBalance.
If the class consists of just public get and set functions along with corresponding private variables and no other functions, then it might not need to be a class at all. It could be a C style struct (public by default) which in turn could be a private member of a class that needs it.
Thanks. I have it set up like you do fun2code but my problem is i am having the user populate the box variables, so what would be the code to get the user to populate it. For example would i use say,
void Box::setWidth( int Width )
{
width = Width;// width being private
}
Box b;
int boxwidth;
cout << "what do you want the width of the box to be?";
cin >> boxwidth;
b.setWidth(boxwidth);
would this work to have the user to populate the width value?
You could get all the info first, then create the object with the appropriate constructor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
unsignedint boxwidth = 0;
std::cout << "what do you want the width of the box to be?";
std::cin >> boxwidth;
// make sure to handle input errors & validate the input
unsignedint boxheight = 0;
std::cout << "what do you want the height of the box to be?";
std::cin >> boxheight;
// make sure to handle input errors & validate the input
// if input is OK and validated, create the object
Box b(boxwidth, boxheight);
std::cout << "box width now = " << b.getWidth() << "\n";
std::cout << "box height now = " << b.getHeight() << "\n";
Of course if you want to change the values again, then use the set functions
thanks guys. good stuff. Got it working now. Also, can I create all the objects at once after getting a bunch of info like this? Or should i do it after getting each bit of info? (this is for a car inventory where i'm populating public member functions for private variables by the way)