Web servers - Chapter 8 Object-Based Programming 287 After defining the

Chapter 8 Object-Based Programming 287 After defining the class, we can use it as a type in declarations such as Time1 sunset; // reference to a Time1 object The class name (Time1) is a type name. A class can yield many objects, just as a primitive data type, such as int, can yield many variables. Programmers can create class types as needed; this is one reason why C# is known as an extensible language. Class TimeTest1 (Fig. 8.2) uses an instance of class Time1. Method Main (lines 11 40) declares and initializes Time1instance time(line 13). When the object is instantiated, operator new allocates the memory in which the Time1object will be stored, then calls the Time1constructor (lines 15 18 of Fig. 8.1) to initialize the instance variables of the Time1 object. As mentioned before, this constructor invokes method SetTime of class Time1 to initialize each privateinstance variable to 0. Operator new(line 13 of Fig. 8.2) then returns a reference to the newly created object; this reference is assigned to time. 1 // Fig. 8.2: TimeTest1.cs 2 // Demonstrating class Time1. 3 4 using System; 5 using System.Windows.Forms; 6 7 // TimeTest1 uses creates and uses a Time1 object 8 class TimeTest1 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 Time1 time = new Time1(); // calls Time1 constructor 14 string output; 15 16 // assign string representation of time to output 17 output = “Initial universal time is: ” + 18 time.ToUniversalString() + 19 “nInitial standard time is: ” + 20 time.ToStandardString(); 21 22 // attempt valid time settings 23 time.SetTime( 13, 27, 6 ); 24 25 // append new string representations of time to output 26 output += “nnUniversal time after SetTime is: ” + 27 time.ToUniversalString() + 28 “nStandard time after SetTime is: ” + 29 time.ToStandardString(); 30 31 // attempt invalid time settings 32 time.SetTime( 99, 99, 99 ); 33 Fig. 8.2 Fig. 8.Fig.Fi 8.2g. 8.22Using an abstract data type. (Part 1 of 2.) Fig. 8.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Leave a Reply