Você está na página 1de 3

Proposed Exercises Chapter 8

In this document you can find exercises related to the content of C++ Programming Language (Fourth Edition).
Solution is provided. However, if you follow the book carefully, you will be able to solve the proposed problems
easily.
Introduction material is very dense and thats why no exercise material is elaborated.
Dont get panic If you have difficulties trying to solve these exercises. The material provided in the Introduction
section (Part I) and chapter eight, allow you to PROPOSE some solution. Some recommendations are:
1. Try to mimic the Programming Technique found in the chapter.
2. Make the program work.
3. Order struct(class) members for readability and sort them by size.
4. Declaration implies definition (initialization).
5. Test your code in main().
6. Especial attention to UML struct description.
7. Smile, you are becoming part of the C++ community.

A. struct
An struct defines by default all member public. This is why all members appears with an open lock in the UML
figure.The following UML describe the Address struct (page 202 in book). This is done with the help of metauml
(latex package).
1. Declare an Address object and print all his member elements.
2. Declare a pointer to Address object and print all his member elements.
3. In the book, we found that objects must be correctly initialize. How do you guarantee that Address object is
initialized properly ?
Address
name: const char *
street: const char *
town: const char *
zip: const char *
state: char[2]
number: int

Figure 1: UML Address struct


4. Implement the Person struct, described in the next UML figure.
Person
age: int
sex: char
name: string
last name: string

Figure 2: UML Person struct

5. Declare a Person object and print all his member elements.


6. Declare a pointer to Person object and print all his member elements.
7. Did you free memory in exercise 2 and 6 ?
8. Add a constructor to Person object and draw the UML description.
9. (*) How do you free memory from Person object ? For example, if you Declares a pointer to Person object,
soon or later you need to use delete. Add a destructor and test if you need to use delete.
10. Implement the Rectangle struct, described in the next UML figure.
Rectangle
Base: const double
Height: const double
Area(): double

Figure 3: UML Rectangle struct


11. Implement the Employee struct, described in the next UML figure.
Employee
Name: string
Age: int
Hourly Salary: double
Monthly Salary():void

Figure 4: UML Employee struct


Additional information to Employee struct:
(a) Monthly Salary() should prompt a message:"Monthly salary for name is XX". XX is equal to Hourly
Salary * 8 *20;
(b) Prompt a message in case of negative age and negative hourly salary. (Hint: Because there is no
constructor, implement a function that return a bool and test for negative age and salary.)
12. (*) The purpose of this exercise is to increase complexity (not functionality) of Person structure (Exercise
4). Lets say that:
(a) Sex can be enumerate in: male, female, gay, lesbian, transsexual.
(b) Marital status can be: single, married, divorced, widow.
(c) Print() prompt a message saying: "Person last name is sex and has age years old. Marital status of
name is marital status.
With this in mind Implement the Person struct described in the next UML figure.
Person
age: int
sex: enum class
marital status: enum class
name: string
last name: string
print(): void

Figure 5: UML Person struc

Define two person and use print().

Você também pode gostar