Initializing Objects with Constructors
As mentioned in Section 4.5, when an object of class GradeBook (Fig. 4.7) is created, its instance variable courseName is initialized to null by default. What if you want to provide a course name when you create a GradeBook object? Each class you declare can provide a constructor that can be used to initialize an object of a class when the object is created. In fact, C# requires a constructor call for every object that is created. The new operator calls the class's constructor to perform the initialization. The constructor call is indicated by the class name, followed by parentheses. For example, line 11 of Fig. 4.8 first uses new to create a GradeBook object. The empty parentheses after "new GradeBook" indicate a call without arguments to the class's constructor. By default, the compiler provides a default constructor with no parameters in any class that does not explicitly include a constructor, so every class has a constructor.
When you declare a class, you can provide your own constructor to specify custom initialization for objects of your class. For example, you might want to specify a course name for a GradeBook object when the object is created, as in
GradeBook myGradeBook = new GradeBook( "CS101 Introduction to C# Programming" );
In this case, the argument "CS101 Introduction to C# Programming" is passed to the GradeBook object's constructor and used to initialize the courseName. Each time you create a different GradeBook object, you can provide a different course name. The preceding statement requires that the class provide a constructor with a string parameter. Figure 4.12 contains a modified GradeBook class with such a constructor.
Figure 4.12. GradeBook class with a constructor to initialize the course name.
1 // Fig. 4.12: GradeBook.cs 2 // GradeBook class with a constructor to initialize the course name. 3 using System; 4 5 public class GradeBook 6 { 7 private string courseName; // course name for this GradeBook 8 9 // constructor initializes courseName with string supplied as argument 10 public GradeBook( string name ) 11 { 12 CourseName = name; // initialize courseName using property 13 } // end constructor 14 15 // property to get and set the course name 16 public string CourseName 17 { 18 get 19 { 20 return courseName; 21 } // end get 22 set 23 { 24 courseName = value; 25 } // end set 26 } // end property CourseName 27 28 // display a welcome message to the GradeBook user 29 public void DisplayMessage() 30 { 31 // use property CourseName to get the 32 // name of the course that this GradeBook represents 33 Console.WriteLine( "Welcome to the grade book for {0}!", 34 CourseName ); 35 } // end method DisplayMessage 36 } // end class GradeBook |
Lines 1013 declare the constructor for class GradeBook. A constructor must have the same name as its class. Like a method, a constructor specifies in its parameter list the data it requires to perform its task. Unlike a method, a constructor doesn't specify a return type. When you create a new object (with new), you place this data in the parentheses that follow the class name. Line 10 indicates that class GradeBook's constructor has a parameter called name of type string. In line 12, the name passed to the constructor is assigned to instance variable courseName via the CourseName property's set accessor.
Figure 4.13 demonstrates initializing GradeBook objects using this constructor. Lines 1213 create and initialize a GradeBook object. The constructor of class GradeBook is called with the argument "CS101 Introduction to C# Programming" to initialize the course name. The object creation expression to the right of = in lines 1213 returns a reference to the new object, which is assigned to variable gradeBook1. Lines 1415 repeat this process for another GradeBook object, this time passing the argument "CS102 Data Structures in C#" to initialize the course name for gradeBook2. Lines 1821 use each object's CourseName property to obtain the course names and show that they were indeed initialized when the objects were created. In the introduction to Section 4.5, you learned that each instance (i.e., object) of a class contains its own copy of the class's instance variables. The output confirms that each GradeBook maintains its own copy of instance variable courseName.
Figure 4.13. GradeBook constructor used to specify the course name at the time each GradeBook object is created.
1 // Fig. 4.13: GradeBookTest.cs 2 // GradeBook constructor used to specify the course name at the 3 // time each GradeBook object is created. 4 using System; 5 6 public class GradeBookTest 7 { 8 // Main method begins program execution 9 public static void Main( string[] args ) 10 { 11 // create GradeBook object 12 GradeBook gradeBook1 = new GradeBook( // invokes constructor 13 "CS101 Introduction to C# Programming" ); 14 GradeBook gradeBook2 = new GradeBook( // invokes constructor 15 "CS102 Data Structures in C#" ); 16 17 // display initial value of courseName for each GradeBook 18 Console.WriteLine( "gradeBook1 course name is: {0}", 19 gradeBook1.CourseName ); 20 Console.WriteLine( "gradeBook2 course name is: {0}", 21 gradeBook2.CourseName ); 22 } // end Main 23 } // end class GradeBookTest
|
Like methods, constructors also can take arguments. However, an important difference between constructors and methods is that constructors cannot return valuesin fact, they cannot specify a return type (not even void). Normally, constructors are declared public. If a class does not include a constructor, the class's instance variables are initialized to their default values. If you declare any constructors for a class, C# will not create a default constructor for that class.
Adding the Constructor to Class GradeBook's UML Class Diagram
The UML class diagram of Fig. 4.14 models class GradeBook of Fig. 4.12, which has a constructor that has a courseName parameter of type string. Like operations, the UML models constructors in the third compartment of a class in a class diagram. To distinguish a constructor from a class's operations, the UML places the word "constructor" between guillemets (« and ») before the constructor's name. It is customary to list constructors before other operations in the third compartment.
Figure 4.14. UML class diagram indicating that class GradeBook has a constructor with a name parameter of type string.