Chapter 8 Object-Based Programming 295 ment constructor that (Web hosting comparison)
Chapter 8 Object-Based Programming 295 ment constructor that sets the time to midnight. Lines 22 25 define a Time2constructor that receives a single intargument representing the hourand sets the time using the specified hour value and zero for the minute and second. Lines 29 32 define a Time2 constructor that receives two intarguments representing the hourand minuteand sets the time using those values and zero for the second. Lines 35 38 define a Time2 constructor that receives three intarguments representing the hour, minuteand second and uses those values to set the time. Lines 41 44 define a Time2constructor that receives a reference to another Time2object. When this last constructor is called, the values from the Time2argument are used to initialize the hour, minuteand secondvalues of the new Time2 object. Even though class Time2declares hour, minuteand second as private(lines 9 11), the Time2constructor can access these values in its Time2argument directly using the expressions time.hour, time.minuteand time.second. Software Engineering Observation 8.13 When one object of a class has a reference to another object of the same class, the first object can access all the second object s data and methods (including those that are private). Notice that the second, third and fourth constructors (lines 22, 29 and 35) have some arguments in common and that those arguments are kept in the same order. For instance, the constructor that begins on line 29 has as its two arguments an integer representing the hour and an integer representing the minute. The constructor on line 35 has these same two arguments in the same order, followed by its last argument (an integer representing the second). Good Programming Practice 8.5 When defining overloaded constructors, keep the order of arguments as similar as possible; this makes client programming easier. Constructors do not specify return types; doing so results in syntax errors. Also, notice that each constructor receives a different number or different types of arguments. Even though only two of the constructors receive values for the hour, minute and second, each constructor calls SetTime with values for hour, minute and second and uses zeros for the missing values to satisfy SetTime s requirement of three arguments. Class TimeTest2(Fig. 8.5) starts the application that demonstrates the use of overloaded constructors (Fig. 8.4). Lines 15 20 create six Time2 objects that invoke various constructors of the class. Line 15 invokes the no-argument constructor by placing an empty set of parentheses after the class name. Lines 16 20 invoke the Time2 constructors that receive arguments. To invoke the appropriate constructor, pass the proper number, types and order of arguments (specified by the constructor s definition) to that constructor. For example, line 16 invokes the constructor that is defined in lines 22 25 of Fig. 8.4. Lines 22 47 invoke methods ToUniversalString and ToStandardString for each Time2object to demonstrate that the constructors initialize the objects correctly. 1 // Fig. 8.5: TimeTest2.cs 2 // Using overloaded constructors. 3 Fig. 8.5 Fig. 8.Fig.Fi 8.5g. 8.55Overloaded constructor demonstration. (Part 1 of 3.) Fig. 8.
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.