Chapter 3: Problem 7
(Data Members) Explain the purpose of a data member.
Short Answer
Step by step solution
Key Concepts
These are the key concepts you need to understand to accurately answer the question.
/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none}
Learning Materials
Features
Discover
Chapter 3: Problem 7
(Data Members) Explain the purpose of a data member.
These are the key concepts you need to understand to accurately answer the question.
All the tools & learning materials you need for study success - in one app.
Get started for free
(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.
(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.
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.
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.
(Header and Source-Code Files) What's a header? What's a source-code file? Discuss the purpose of each.
What do you think about this solution?
We value your feedback to improve our textbook solutions.