| 1: || |
What is meant by the term class properties ?
| A1: || |
In Chapter 2, "The Basics of Object-Oriented Programming," you learned that encapsulated within an object are member variables that are used to describe the object. These member variables are the properties associated with the object.
| 2: || |
Why are class properties important?
| A2: || |
Class properties are important for two reasons. First, the properties are viewed as attributes that can be used to describe or define an object. Second, by knowing the exact values for each property, you know its current state. For example, you might have a class property named mTerminal that is a Boolean variable that is used to describe a patient's severity of illness . By examining the value of mTerminal , you can learn about one aspect of the state of a patient.
| 3: || |
Suppose you start using the COven class described in this chapter but notice that it doesn't model your oven very well. In particular, your oven has a temperature probe that you can insert into a roast to read the roast's internal temperature directly. What modifications would you have to consider in order to modify the COven class?
| A3: || |
You may have begun your answer by saying that you need to add a new variable, perhaps named mProbeTemp , to the COven class. That's a good start, but you might want to back up a bit before going any further. The first question you should always ask yourself before adding new properties to an existing class is whether you want to modify the existing class or create a new class. This type of decision is based on an understanding of inheritance, which is the topic of discussion in Chapter 17, "Inheritance." As a general rule, you can make single-property modifications to a class without deriving a new class. However, anytime you want to extend an existing class, you should think about whether to graft the new variable onto the existing class or whether a new derived class should be developed.
The extension you are contemplating in this case is sufficiently easy that you can simply add a new property (for example, mProbeTemp ) to the class. You will, of course, need to write a new property Get procedure for mProbeTemp . You can probably omit the property Set procedure because it makes no sense to be able to set the probe's temperature; it is a read-only attribute.
| 4: || |
If you have created a class, is it really necessary to document each property?
| A4: || |
Yes. You should always document your code. It is amazing how quickly you will forget what you write, even over short periods of time. Although meaningful variable names help, it is always good to document what the class properties are and what they are designed to do. If you cannot read the code for a property accessor method and understand what it is doing, you have not done a good job documenting it.
| 5: || |
The code for the COven class is so simple, why do you bother using symbolic constants for BROILTEMP and CLEANTEMP ?
| A5: || |
Although the code is very simple and the symbolic constants are not required, when you read the code, do you think the use of the symbolic constants makes it easier to understand what the code is doing? If you answered no, methinks you jest.
| 6: || |
What is the purpose of the ReadOnly and WriteOnly keywords?
| A6: || |
When you use the ReadOnly keyword, you are denying users of the class from modifying the property. You use the ReadOnly keyword for any property that you do not want changed by users of the class. The WriteOnly property allows the user to change the value of the property, but not to read its current value. For example, you might be writing code for a network utility that records the network ID of printer users. Your class would record the activity but not make it available outside the class.
| 7: || |
What part do properties play in the interface of a class?
| A7: || |
The property Get and Set accessor methods define the way in which the user of the class must interact with the properties to change the state of the object modeled by the class.
| 8: || |
What's wrong with the following class code fragment?
Dim mAccountBalance As Double Dim mAccountStatus As Integer Dim mDateOpened As Date Dim mAccountFirstName As String Dim mAccountLastName As String Dim mAccountSSN As String Public Property DateOpened() As Date ' Purpose: This property reads and writes the date the account was ' opened. Get Return mDateOpened End Get Set(ByVal Value As Date) MDateOpened = Value End Set End Property Public Property Balance() As Integer ' Purpose: This property reads and writes the account balance Get Return mAccountBalance End Get Set(ByVal Value As Integer) MAccountBalance = Value End Set End Property
| A8: || |
Several things might need to be fixed in this fragment. First, the Balance() property methods should have the Double type specifier , not Integer . Also, because this property deals with monetary units, the Decimal data type might be a better choice. Second, there probably should not be a Set property method for Balance() . The class should include methods for processing deposits and withdrawals to the account that should set the balance. That would probably be a safer approach than to let the user of the class set the account balance directly.
| 9: || |
Write an accounting system for General Ledger, Accounts Payable, Accounts Receivable, Payroll, and Inventory by using classes to model all the appropriate objects found in a small business accounting system.
| A9: || |