This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Solution to Prelab Exercises Chapter 4 1. Constructors are special methods included in class definitions. a. What is a constructor used for? A constructor initializes an object when the object is instantiated. b. How do constructors differ from other methods in a class? The name of the constructor must be the same as the name of the class and a constructor has no return type. 2. Both methods and variables in a class are declared as either private or public . Describe the difference between private and public and indicate how a programmer decides which parts of a class should be private and which public. Public members of a class can be directly referenced from outside of the class but private members can be directly referenced only inside the class. Generally instance variables should be private to protect the variables from direct changes by programs using the class. All changes to the variables should be done by public methods provided by the class. Hence methods provided for client programs should be done by public methods provided by the class....
View Full Document
This note was uploaded on 03/22/2010 for the course CS 202 taught by Professor Fong during the Spring '10 term at SIU Carbondale.
- Spring '10
- Computer Science