PART A & PART B

PART A
Use the example on p. 355 of the text to create your own structure related to an overtime pay calculator and define the variables needed within your structure to perform those calculations.
Define and create a base class using the same variables that may serve the same function as the structure.
EXAMPLE FROM PAGE 355 OF THE TEXT
Initializing a struct
The fi rst way to get data into the members of a struct is to defi ne initial values in the declaration.
Suppose you wanted to initialize the variable Novel to contain the data for one of your favorite
books, Paneless Programming , published in 1981 by the Gutter Press. This is a story of a guy
performing heroic code development while living in an igloo, and, as you probably know, inspired
the famous Hollywood box offi ce success, Gone with the Window . It was written by I.C. Fingers,
who is also the author of that seminal three – volume work, The Connoisseur ’ s Guide to the Paper
Clip . With this wealth of information, you can write the declaration for the variable Novel as:

BOOK Novel =
{
“Paneless Programming”, // Initial value for Title
“I.C. Fingers”, // Initial value for Author
“Gutter Press”, // Initial value for Publisher
1981 // Initial value for Year
};

The initializing values appear between braces, separated by commas, in much the same way that you
defi ned initial values for members of an array. As with arrays, the sequence of initial values obviously
needs to be the same as the sequence of the members of the struct in its defi nition. Each member of
the structure Novel has the corresponding value assigned to it, as indicated in the comments.

Accessing the Members of a struct
To access individual members of a struct , you can use the member selection operator , which is a
period; this is sometimes referred to as the member access operator . To refer to a particular member,
you write the struct variable name, followed by a period, followed by the name of the member
that you want to access. To change the Year member of the Novel structure, you could write:
Novel.Year = 1988;
This would set the value of the Year member to 1988. You can use a member of a structure in
exactly the same way as any other variable of the same type as the member. To increment the
member Year by two, for example, you can write:
Novel.Year += 2;
This increments the value of the Year member of the struct , just like any other variable

PART B
Use the program on pp. 368 of the text as a starting point for this assignment.
Modify the program in the following ways:
• Add a third box to the calculation.
• Calculate length (L), width (W), and height (H) for the third box.
• Calculate the total volume for all three boxes in question.
Answer the following questions in your own words after making the required modifications to the program:
• Under what circumstances must you use classes?
• Why do you use a class instead of a structure? Are they interchangeable? Explain your answer.
• Are classes more useful than structures? Why?
PROGRAM FROM PAGE 368
Your First Use of Classes
Verify that you can use your class in the same way as the structure. Try it out in the following console
application: // Ex7_02.cpp
// Creating and using boxes
#include
using std::cout;
using std::endl;
class CBox // Class definition at global scope
{
public:
double m_Length; // Length of a box in inches
double m_Width; // Width of a box in inches
double m_Height; // Height of a box in inches
};
int main()
{
CBox box1; // Declare box1 of type CBox
CBox box2; // Declare box2 of type CBox
double boxVolume(0.0); // Stores the volume of a box
box1.m_Height = 18.0; // Define the values
box1.m_Length = 78.0; // of the members of
box1.m_Width = 24.0; // the object box1
box2.m_Height = box1.m_Height – 10; // Define box2
box2.m_Length = box1.m_Length/2.0; // members in
box2.m_Width = 0.25*box1.m_Length; // terms of box1
// Calculate volume of box1
boxVolume = box1.m_Height*box1.m_Length*box1.m_Width;
cout < < endl
< < “Volume of box1 = ” < < boxVolume;
cout < < endl
< < “box2 has sides which total ”
< < box2.m_Height+ box2.m_Length+ box2.m_Width
< < ” inches.”;
cout < < endl // Display the size of a box in memory
< < “A CBox object occupies ”
< < sizeof box1 < < ” bytes.”;
cout < < endl;
return 0;
}
code snippet Ex7_02.cpp
Available for
download on
Wrox.com