- 4.1 Introduction
- 4.2 Classes, Objects, Methods, Properties and Instance Variables
- 4.3 Declaring a Class with a Method and Instantiating an Object of a Class
- 4.4 Declaring a Method with a Parameter
- 4.5 Instance Variables and Properties
- 4.6 UML Class Diagram with a Property
- 4.7 Software Engineering with Properties and set and get Accessors
- 4.8 Auto-Implemented Properties
- 4.9 Value Types vs. Reference Types
- 4.10 Initializing Objects with Constructors
- 4.11 Floating-Point Numbers and Type decimal
- 4.12 Wrap-Up
- Self-Review Exercises
- Answers to Self-Review Exercises
- Making a Difference Exercises
Answers to Self-Review Exercises
4.1 a) object. b) class. c) new. d) type, name. e) global namespace. f) instance variable. g) float, double, decimal. h) double-precision. i) ToDecimal. j) access modifier. k) void. l) ReadLine. m) using directive. n) single-precision. o) C. p) value, reference. q) auto-implemented property.
4.2 a) False. By convention, method names begin with an uppercase first letter and all subsequent words in the name begin with an uppercase first letter. b) False. A property's get accessor enables a client to retrieve the value of the instance variable associated with the property. A property's set accessor enables a client to modify the value of the instance variable associated with the property. c) True. d) True. e) False. After defining a property, you can use it the same way you use a variable. f) True. g) False. Such variables are called local variables and can be used only in the method in which they're declared. h) False. A property declaration can contain a get accessor, a set accessor or both. i) True. j) False. Instance variables are initialized by default. k) True. l) True. m) True. n) False. Such literals are of type double by default.
4.3 A local variable is declared in the body of a method and can be used only in the method in which it's declared. An instance variable is declared in a class, but not in the body of any of the class's methods. Every object (instance) of a class has a separate copy of the class's instance variables. Also, instance variables are accessible to all methods of the class. (We'll see an exception to this in Chapter 10, Classes and Objects: A Deeper Look.)
4.4 A parameter represents additional information that a method requires to perform its task. Each parameter required by a method is specified in the method's declaration. An argument is the actual value that's passed to a method parameter when a method is called.