3.5 (Default Constructor) What’s a default constructor? How are an object’s data members initialized if a class has only a default constructor defined by the compiler?
3.6 (Data Members) Explain the purpose of a data member.
3.7 (Using a Class Without a using
Directive) Explain how a program could use class string
without inserting a using
directive.
3.8 (Set and Get Functions) Explain why a class might provide a set function and a get function for a data member.
3.9 (Modified Account
Class) Modify class Account
(Fig. 3.8) to provide a member function called withdraw
that withdraws money from an Account
. Ensure that the withdrawal amount does not exceed the Account
’s balance. If it does, the balance should be left unchanged and the member function should display a message indicating "Withdrawal
amount
exceeded
account
balance."
Modify class AccountTest
(Fig. 3.9) to test member function withdraw
.
3.10 (Invoice
Class) Create a class called Invoice
that a hardware store might use to represent an invoice for an item sold at the store. An Invoice
should include four data members—a part number (type string
), a part description (type string
), a quantity of the item being purchased (type int
) and a price per item (type int
). Your class should have a constructor that initializes the four data members. Provide a set and a get function for each data member. In addition, provide a member function named getInvoiceAmount
that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as an int
value. If the quantity is not positive, it should be set to 0
. If the price per item is not positive, it should be set to 0
. Write a test program that demonstrates class Invoice
’s capabilities.
3.11 (Employee
Class) Create a class called Employee
that includes three pieces of information as data members—a first name (type string
), a last name (type string
) and a monthly salary (type int
). Your class should have a constructor that initializes the three data members. Provide a set and a get function for each data member. If the monthly salary is not positive, set it to 0
. Write a test program that demonstrates class Employee
’s capabilities. Create two Employee
objects and display each object’s yearly salary. Then give each Employee
a 10 percent raise and display each Employee
’s yearly salary again.
3.12 (Date
Class) Create a class called Date
that includes three pieces of information as data members—a month (type int
), a day (type int
) and a year (type int
). Your class should have a constructor with three parameters that uses the parameters to initialize the three data members. For the purpose of this exercise, assume that the values provided for the year and day are correct, but ensure that the month value is in the range 1–12; if it isn’t, set the month to 1. Provide a set and a get function for each data member. Provide a member function displayDate
that displays the month, day and year separated by forward slashes (/
). Write a test program that demonstrates class Date
’s capabilities.
3.13 (Removing Duplicated Code in the main
Function) In Fig. 3.9, the main
function contains six statements (lines 14–15, 16–17, 26–27, 28–29, 37–38 and 39–40) that each display an Account
object’s name
and balance
. Study these statements and you’ll notice that they differ only in the Account
object being manipulated—account1
or account2
. In this exercise, you’ll define a new displayAccount
function that contains one copy of that output statement. The member function’s parameter will be an Account
object and the member function will output the object’s name
and balance
. You’ll then replace the six duplicated statements in main
with calls to displayAccount
, passing as an argument the specific Account
object to output.
Modify Fig. 3.9 to define the following displayAccount
function after the using
directive and before main
:
void displayAccount(Account accountToDisplay) {
// place the statement that displays
// accountToDisplay’s name and balance here
}
Replace the comment in the member function’s body with a statement that displays accountToDisplay
’s name
and balance
.
Once you’ve completed displayAccount
’s declaration, modify main
to replace the statements that display each Account
’s name
and balance
with calls to displayAccount
of the form:
displayAccount(nameOfAccountObject);
In each call, the argument should be the account1
or account2
object, as appropriate. Then, test the updated program to ensure that it produces the same output as shown in Fig. 3.9.
3.14 (C++11 List Initializers) Write a statement that uses list initialization to initialize an object of class Account
which provides a constructor that receives an unsigned
int
, two string
s and a double
to initialize the accountNumber
, firstName
, lastName
and balance
data members of a new object of the class.