C++/class code translation
I'm using MS Visual C++ 6.0 on WIN XP professional. I do not understand the idea of a class, mainly how it is written. I have an assignment that goes like this:
""An open box is to be made from a piece of rectangle cardboard by cutting out squares of equal size from the four corners and bending up the sides. What size should the squares be to obtain a box with largest possible volume?
Design a class to define a specific object to solve the above problem.
The program is worth 20 points and the due date is March 12, 2004 at noon.""
I got the math done, it's just calculus, but going from the algorithm to code is hard, unless I cheated with simple algebra like this:
vol = (w - 2 * c) * c * (l - 2 * c);
Can you give me a template to work with, I figure someone with an input and small calculation like addition, would work. The text does not help with simple examples.
could you help me out?
PJ, Thank you for your question.
Since your deadline has passed, I assume you have already solved this problem.
I would design a class that represents an open box. It would have a member variable that represents the size of the cutout squares. It would have a function 'calc' to calculate the box's volume.
I would then apply a standard numerical method for evaluating the maximum volume. One simple algorithm would be to evaluate the volume when the square size is equal to 1/2 of the original square size. The volume in this case should be zero. Next, do the same evaluation for square size equal the last square size divided by two and repeat this step until the maximum volume is found. The direction of modifying the square size depends on whether the volume increases or decreases from the previous step.
Obviously, the use of a class in this case confers no advantage. The teacher is probably just trying hard (but failing) to create a real-world example.