Chapter 3
C++ How to Program · 14 exercises
Problem 1
Fill in the blanks in each of the following: a) Every class definition contains the keyword __ followed immediately by the class's name. b) A class definition is typically stored in a file with the __ filename extension. c) Each parameter in a function header specifies both a(n) __ and \(a(n)\) __. d) When each object of a class maintains its own copy of an attribute, the variable that represents the attribute is also known as a(n) __. e) Keyword public is a(n) __. f) Return type __ indicates that a function will perform a task but will not return any information when it completes its task. g) Function __ from the \(<\) string \(>\) library reads characters until a newline character is encountered, then copies those characters into the specified string. h) When a member function is defined outside the class definition, the function header must include the class name and the __ followed by the function name to "tie" the member function to the class definition. i) The source-code file and any other files that use a class can include the class's header via \(a(n)\) __ preprocessor directive.
9 step solution
Problem 2
State whether each of the following is true or false. If false, explain why. a) By convention, function names begin with a capital letter and all subsequent words in the name begin with a capital letter. b) Empty parentheses following a function name in a function prototype indicate that the function does not require any parameters to perform its task. c) Data members or member functions declared with access specifier private are accessible to member functions of the class in which they're declared. d) Variables declared in the body of a particular member function are known as data members and can be used in all member functions of the class. e) Every function's body is delimited by left and right braces ( \(\\{\text { and }\\}\) ). f) Any source-code file that contains int main () can be used to execute a program. g) The types of arguments in a function call must be consistent with the types of the corresponding parameters in the function prototype's parameter list.
7 step solution
Problem 3
What is the difference between a local variable and a data member?
3 step solution
Problem 4
Explain the purpose of a function parameter. What's the difference between a parameter and an argument?
2 step solution
Problem 5
(Function Prototypes and Definitions) Explain the difference between a function prototype and a function definition.
3 step solution
Problem 6
(Default Constructor) What's a default constructor? How are an object's data members initialized if a class has only an implicitly defined default constructor?
3 step solution
Problem 7
(Data Members) Explain the purpose of a data member.
3 step solution
Problem 8
(Header and Source-Code Files) What's a header? What's a source-code file? Discuss the purpose of each.
3 step solution
Problem 9
(Using a Class Without a us ing Directive) Explain how a program could use class string without inserting a using directive.
3 step solution
Problem 10
(Set and Get Functions) Explain why a class might provide a set function and a get function for a data member.
3 step solution
Problem 12
( Account Class ) Create an Account class that a bank might use to represent customers bank accounts. Include a data member of type int to represent the account balance. [Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., 2.75 ) - called floating-point values to represent dollar amounts.] Provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it's greater than or equal to \(0 .\) If not, set the balance to 0 and display an error message indicating that the initial balance was invalid. Provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account's balance. If it does, the balance should be left unchanged and the function should print a message indicating "Debit amount exceed ed account balance." Member function getBalance should return the current balance. Create a program that creates two Account objects and tests the member functions of class Account.
6 step solution
Problem 13
(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). [Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., \(2.75)\) - called floating-point values - to represent dollar amounts.] 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.
6 step solution
Problem 14
(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). \([\text {Note: In subsequent chapters, we'll use numbers that contain decimal points (e.g., } 2.75)-\) called floating-point values - to represent dollar amounts.] 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 yean \(y\) salary. Then give each Employee a 10 percent raise and display each Employee's yearly salary again.
6 step solution
Problem 15
(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.
5 step solution