You are here:

# C++/class code translation

Question
Hi,

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

PJ, Thank you for your question.

If not....

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.

David Spector

Questioner's Rating
 Rating(1-10) Knowledgeability = 10 Clarity of Response = 10 Politeness = 10 Comment Very knowlegable and promt. A first rate guy.

C++

Volunteer

#### David Spector

##### Expertise

Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.

##### Experience

I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Publications
Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

Education/Credentials
ICCP Systems Programming Certification
Master's degree equivalent in Computer Science