December 30th, 2007
370 Object-Oriented Programming: Inheritance Chapter 9 Height to obtain information about the Cylinder object, because CylinderTest cannot reference the privatedata of class Cylinderdirectly. Lines 22 25 use properties X, Y, Radiusand Heightto reset the Cylinder s x-y coordinates (we assume the cylinder s x-y coordinates specify its position on the x-y plane), radius and height. Class Cylindercan use class Point3 s Xand Yproperties, because class Cylinderinherits them indirectly from class Point3 Class Cylinder inherits properties X and Y directly from class Circle4, which inherited them directly from class Point3. Line 29 invokes method ToString implicitly to obtain the string representation of the Cylinder object. Lines 33 37 invoke methods Diameter and Circumference of the Cylinder object because class Cylinder inherits these methods from class Circle4 and cannot override them, these methods, exactly as listed in Circle4, are invoked. Lines 41 45 invoke methods Areaand Volume. Using the point-circle-cylinder example, we have shown the use and benefits of inheritance. We were able to develop classes Circle4and Cylinderusing inheritance much faster than if we had developed these classes from scratch. Inheritance avoids duplicating code and the associated code-maintenance problems. 1 // Fig. 9.16: CylinderTest.cs 2 // Tests class Cylinder. 3 4 using System; 5 using System.Windows.Forms; 6 7 // CylinderTest class definition 8 class CylinderTest 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 // instantiate object of class Cylinder 14 Cylinder cylinder = new Cylinder(12, 23, 2.5, 5.7); 15 16 // properties get initial x-y coordinate, radius and height 17 string output = “X coordinate is ” + cylinder.X + “n” + 18 “Y coordinate is ” + cylinder.Y + “nRadius is ” + 19 cylinder.Radius + “n” + “Height is ” + cylinder.Height; 20 21 // properties set new x-y coordinate, radius and height 22 cylinder.X = 2; 23 cylinder.Y = 2; 24 cylinder.Radius = 4.25; 25 cylinder.Height = 10; 26 27 // get new x-y coordinate and radius 28 output += “nnThe new location, radius and height of ” + 29 “cylinder aren” + cylinder + “nn”; 30 31 // display Cylinder’s Diameter 32 output += “Diameter is ” + 33 String.Format( “{0:F}”, cylinder.Diameter() ) + “n”; Fig. 9.16 Fig. 9.16Fig. 9.FiFi16g. 9.16g. 9.16Testing class Cylinder(Part 1 of 2.).
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.
Posted in MySQL5 | No Comments »
December 29th, 2007
Chapter 9 Object-Oriented Programming: Inheritance 369 11 // default constructor 12 public Cylinder() 13 { 14 // implicit call to Circle4 constructor occurs here 15 } 16 17 // four-argument constructor 18 public Cylinder( int xValue, int yValue, double radiusValue, 19 double heightValue ) : base( xValue, yValue, radiusValue ) 20 { 21 Height = heightValue; // set Cylinder height 22 } 23 24 // property Height 25 public double Height 26 { 27 get 28 { 29 return height; 30 } 31 32 set 33 { 34 if ( value >= 0 ) // validate height 35 height = value; 36 } 37 38 } // end property Height 39 40 // override Circle4 method Area to calculate Cylinder area 41 public override double Area() 42 { 43 return 2 * base.Area() + base.Circumference() * Height; 44 } 45 46 // calculate Cylinder volume 47 public double Volume() 48 { 49 return base.Area() * Height; 50 } 51 52 // convert Cylinder to string 53 public override string ToString() 54 { 55 return base.ToString() + “; Height = ” + Height; 56 } 57 58 } // end class Cylinder Fig. 9.15 Fig. 9.15Fig. 9.FiFi15g. 9.15g. 9.15Cylinderclass inherits from class Circle4and overrides method Area. (Part 2 of 2.) Figure 9.16 is a CylinderTestapplication that tests the Cylinderclass. Line 14 instantiates an object of class Cylinder. Lines 17 19 use properties X, Y, Radius and
In case you need quality webspace to host and run your web applications, try our personal web hosting services.
Posted in MySQL5 | No Comments »
December 28th, 2007
368 Object-Oriented Programming: Inheritance Chapter 9 9.5 Case Study: Three-Level Inheritance Hierarchy Let us consider a more substantial inheritance example involving a three-level point-circlecylinder hierarchy. In Section 9.4, we developed classes Point3 (Fig. 9.12) and Circle4(Fig. 9.13). Now, we present an example in which we derive class Cylinder from class Circle4. The first class that we use in our case study is class Point3(Fig. 9.12). We declared Point3 s instance variables as private. Class Point3also contains properties Xand Y for accessing x and y and method ToString (which Point3 overrides from class Object) for obtaining a string representation of the x-y coordinate pair. We also created class Circle4 (Fig. 9.13), which inherits from class Point3. Class Circle4 contains the Point3 functionality, in addition to providing property Radius, which ensures that the radiusmember variable cannot hold a negative value, and methods Diameter, Circumference, Area and ToString. Recall that method Area was declared virtual(line 53). As we discussed in Section 9.4, this keyword enables derived classes to override a base-class method. Derived classes of class Circle4 (such as class Cylinder, which we introduce momentarily) can override these methods and provide specific implementations. A circle has an area that is calculated by the formula, pr2, in which r represents the circle s radius. However, a cylinder has a surface area that is calculated by the formula, (2pr2) + (2prh), in which r represents the cylinder s radius and h represents the cylinder s height. Therefore, class Cylindermust override method Areato include this calculation, so we declared class Circle4 s method Areaas virtual. Figure 9.15 presents class Cylinder, which inherits from class Circle4 (line 7). Class Cylinder s public services include the inherited Circle4 methods Diameter, Circumference, Area and ToString; the inherited Circle4 property Radius; the indirectly inherited Point3properties Xand Y; the Cylinderconstructor, property Height and method Volume. Method Area (lines 41 44) overrides method Area of class Circle4. Note that, if class Cylinder were to attempt to override Circle4 s methods Diameter and Circumference, syntax errors would occur, because class Circle4 did not declare these methods virtual. Method ToString (lines 53 56) overrides method ToStringof class Circle4to obtain a stringrepresentation for the cylinder. Class Cylinder also includes method Volume (lines 47 50) to calculate the cylinder s volume. Because we do not declare method Volume as virtual, no derived class of class Cylindercan override this method. 1 // Fig. 9.15: Cylinder.cs 2 // Cylinder class inherits from class Circle4. 3 4 using System; 5 6 // Cylinder class definition inherits from Circle4 7 public class Cylinder : Circle4 8 { 9 private double height; 10 Fig. 9.15 Fig. 9.15Fig. 9.FiFi15g. 9.15g. 9.15Cylinderclass inherits from class Circle4and overrides method Area. (Part 1 of 2.)
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.
Posted in MySQL5 | No Comments »
December 27th, 2007
Chapter 9 Object-Oriented Programming: Inheritance 367 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 // instantiate Circle4 14 Circle4 circle = new Circle4( 37, 43, 2.5 ); 15 16 // get Circle4’s initial x-y coordinates and radius 17 string output = “X coordinate is ” + circle.X + “n” + 18 “Y coordinate is ” + circle.Y + “n” + 19 “Radius is ” + circle.Radius; 20 21 // set Circle4’s x-y coordinates and radius to new values 22 circle.X = 2; 23 circle.Y = 2; 24 circle.Radius = 4.25; 25 26 // display Circle4’s string representation 27 output += “nn” + 28 “The new location and radius of circle are ” + 29 “n” + circle + “n”; 30 31 // display Circle4’s Diameter 32 output += “Diameter is ” + 33 String.Format( “{0:F}”, circle.Diameter() ) + “n”; 34 35 // display Circle4’s Circumference 36 output += “Circumference is ” + 37 String.Format( “{0:F}”, circle.Circumference() ) + “n”; 38 39 // display Circle4’s Area 40 output += “Area is ” + 41 String.Format( “{0:F}”, circle.Area() ); 42 43 MessageBox.Show( output, “Demonstrating Class Circle4″ ); 44 45 } // end method Main 46 47 } // end class CircleTest4 Fig. 9.14 Fig. 9.14Fig. 9.FiFi14g. 9.14g. 9.14CircleTest4demonstrates class Circle4functionality. (Part 2 of 2.)
If you are in need for chaep and reliable webhost to host your website, our recommendation is http web server services.
Posted in MySQL5 | No Comments »
December 26th, 2007
366 Object-Oriented Programming: Inheritance Chapter 9 Point3 s base class) is declared virtual. Method ToStringof class Circle4displays the privateinstance variables xand yof class Point3by calling the base class s ToStringmethod (in this case, Point3 s ToStringmethod). The call is made in line 62 via the expression base.ToString() and causes the values of x and y to become part of the Circle4 s string representation. Using this approach is a good software engineering practice: Recall that Software Engineering Observation 8.11 stated that, if an object s method performs the actions needed by another object, call that method rather than duplicating its code body. Duplicate code creates code-maintenance problems. By having Circle4 s ToString method use the formatting provided by Point3 s ToString method, we avoid duplicating code. Also, Point3 s ToStringmethod performs part of the task of Circle4 s ToString method, so we call Point3 s ToString method from class Circle4with the expression base.ToString(). Common Programming Error 9.3 When a base-class method is overridden in a derived class, the derived-class version often calls the base-class version to do additional work. Failure to use the base reference when referencing the base class s method causes infinite recursion, because the derived-class method would then call itself. Common Programming Error 9.4 The use of chained base references to refer to a member (a method, property or variable) several levels up the hierarchy (as in base.base.mX) is a syntax error. Software Engineering Observation 9.8 A redefinition in a derived class of a base-class method that uses a different signature than that of the base-class method is method overloading rather than method overriding. Software Engineering Observation 9.9 Although method ToString certainly could be overridden to perform arbitrary actions, the general understanding in the C# .NET community is that method ToString should be overridden to obtain an object s string representation. Class CircleTest4 (Fig. 9.14) performs identical manipulations on class Circle4 as did classes CircleTest (Fig. 9.7) and CircleTest3 (Fig. 9.11). Note that the outputs of all three modules are identical. Therefore, although each circle class appears to behave identically, class Circle4 is the most properly engineered. Using inheritance, we have efficiently and effectively constructed a well-engineered class. 1 // Fig. 9.14: CircleTest4.cs 2 // Testing class Circle4. 3 4 using System; 5 using System.Windows.Forms; 6 7 // CircleTest4 class definition 8 class CircleTest4 9 { Fig. 9.14 Fig. 9.14Fig. 9.FiFi14g. 9.14g. 9.14CircleTest4demonstrates class Circle4functionality. (Part 1 of 2.)
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.
Posted in MySQL5 | No Comments »
December 25th, 2007
Chapter 9 Object-Oriented Programming: Inheritance 365 52 // calculate Circle area 53 public virtual double Area() 54 { 55 return Math.PI * Math.Pow( Radius, 2 ); // use property 56 } 57 58 // return string representation of Circle4 59 public override string ToString() 60 { 61 // use base reference to return Point string representation 62 return “Center= ” + base.ToString() + 63 “; Radius = ” + Radius; // use property Radius 64 } 65 66 } // end class Circle4 Fig. 9.13 Fig. 9.13Fig. 9.FiFi13g. 9.13g. 9.13Circle4class that inherits from class Point3, which does not provide protecteddata (Part 2 of 2.). Software Engineering Observation 9.7 When possible, use properties to alter and obtain the values of member variables, even if those values can be modified directly. A property s set accessor can prevent attempts to assign an inappropriate value to that the value, and a property s get accessor can help control the presentation of the data to clients. Performance Tip 9.1 Using a property to access a variable s value is slightly slower than accessing the data directly. However, attempting to optimize programs by referencing data directly often is unnecessary, because the compiler optimizes the programs implicitly. [Today s so-called optimizing compilers are carefully designed to perform many optimizations implicitly, even if the programmer does not write what appears to be the most optimal code. A good rule is, Do not second-guess the compiler. For the purpose of this example, to demonstrate both explicit and implicit calls to base- class constructors, we include a second constructor that calls the base-class constructor explicitly. Lines 18 22 declare the Circle4constructor that invokes the second Point3 constructor explicitly (line 19) using the base-class constructor-call syntax (i.e., reference base followed by a set of parentheses containing the arguments to the base-class constructor). In this case, xValue and yValue are passed to initialize the private base- class members xand y. The colon symbol (:) followed by the basekeyword accesses the base-class version of that method explicitly (line 19). By making this call, we can initialize xand yin the base class to specific values, rather than to 0. Common Programming Error 9.2 It is a syntax error if a derived class uses base to call its base-class constructor with arguments that do not match exactly the number and types of parameters specified in one of the base-class constructor definitions. Class Circle4 s ToString method (line 59 64) overrides class Point3 s ToString method (lines 57 60 of Fig. 9.12). As we discussed earlier, overriding this method is possible, because method ToString of class System.Object (class
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.
Posted in MySQL5 | No Comments »
December 24th, 2007
364 Object-Oriented Programming: Inheritance Chapter 9 1 // Fig. 9.13: Circle4.cs 2 // Circle4 class that inherits from class Point3. 3 4 using System; 6 // Circle4 class definition inherits from Point3 7 public class Circle4 : Point3 8 { 9 private double radius; 11 // default constructor 12 public Circle4() 13 { 14 // implicit call to Point constructor occurs here } 16 17 // constructor 18 public Circle4( int xValue, int yValue, double radiusValue ) 19 : base( xValue, yValue ) { 21 Radius = radiusValue; 22 } 23 24 // property Radius public double Radius 26 { 27 get 28 { 29 return radius; } 31 32 set 33 { 34 if ( value >= 0 ) // validation needed radius = value; 36 } 37 38 } // end property Radius 39 // calculate Circle diameter 41 public double Diameter() 42 { 43 return Radius * 2; // use property Radius 44 } 46 // calculate Circle circumference 47 public double Circumference() 48 { 49 return Math.PI * Diameter(); } 51 Fig. 9.13 Fig. 9.13Fig. 9.FiFi13g. 9.13g. 9.13Circle4class that inherits from class Point3, which does not provide protecteddata (Part 1 of 2.).
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.
Posted in MySQL5 | No Comments »
December 23rd, 2007
Chapter 9 Object-Oriented Programming: Inheritance 363 12 // default constructor 13 public Point3() 14 { 15 // implicit call to Object constructor occurs here 16 } 17 18 // constructor 19 public Point3( int xValue, int yValue ) 20 { 21 // implicit call to Object constructor occurs here 22 X = xValue; // use property X 23 Y = yValue; // use property Y 24 } 25 26 // property X 27 public int X 28 { 29 get 30 { 31 return x; 32 } 33 34 set 35 { 36 x = value; // no need for validation 37 } 38 39 } // end property X 40 41 // property Y 42 public int Y 43 { 44 get 45 { 46 return y; 47 } 48 49 set 50 { 51 y = value; // no need for validation 52 } 53 54 } // end property Y 55 56 // return string representation of Point3 57 public override string ToString() 58 { 59 return “[” + X + “, ” + Y + “]”; 60 } 61 62 } // end class Point3 Fig. 9.12 Fig. 9.12FiFFg. 9.12ig. 9.12ig. 9.12Point3class uses properties to manipulate its privatedata. (Part 2 of 2.)
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.
Posted in MySQL5 | No Comments »
December 22nd, 2007
362 Object-Oriented Programming: Inheritance Chapter 9 data, thus leaving the object in an inconsistent state. For example, if we were to declare Circle3 s variable radius as protected, a derived-class object (e.g., Cylinder), could then assign a negative value to radius. The second problem with using protected data is that derived-class methods are more likely to be written to depend on base-class implementation. In practice, derived classes should depend only on the base-class services (i.e., non-private methods and properties) and not on base-class implementation. With protected data in the base class, if the base-class implementation changes, we may need to modify all derived classes of that base class. For example, if for some reason we were to change the names of variables x and y to xCoordinate and yCoordinate, then we would have to do so for all occurrences in which a derived class references these base-class variables directly. In such a case, the software is said to be fragile or brittle. The programmer should be able to change the base-class implementation freely, while still providing the same services to derived classes. (Of course, if the base class services change, we must reimplement our derived classes, but good object-oriented design attempts to prevent this.) Software Engineering Observation 9.5 The most appropriate time to use the protected access modifier is when a base class should provide a service only to its derived classes (i.e., the base class should not provide the service to other clients). Software Engineering Observation 9.6 Declaring base-class instance variables private (as opposed to declaring them protected) enables programmers to change base-class implementation without having to change derived-class implementation. Testing and Debugging Tip 9.1 When possible, avoid including protected data in a base class. Rather, include non- private properties and methods that access private data, ensuring that the object maintains a consistent state. We reexamine our point-circle hierarchy example once more; this time, attempting to use the best software engineering. We use Point3(Fig. 9.12), which declares variables x and yas privateand uses properties in method ToStringto access these values. We show how derived class Circle4 (Fig. 9.13) can invoke non-private base-class methods and properties to manipulate these variables. 1 // Fig. 9.12: Point3.cs 2 // Point3 class represents an x-y coordinate pair. 3 4 using System; 5 6 // Point3 class definition implicitly inherits from Object 7 public class Point3 8 { 9 // point coordinate 10 private int x, y; 11 Fig. 9.12 Fig. 9.12FiFFg. 9.12ig. 9.12ig. 9.12Point3class uses properties to manipulate its privatedata. (Part 1 of 2.)
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.
Posted in MySQL5 | No Comments »
December 21st, 2007
Chapter 9 Object-Oriented Programming: Inheritance 361 20 21 // set Circle3’s x-y coordinates and radius to new values 22 circle.X = 2; 23 circle.Y = 2; 24 circle.Radius = 4.25; 25 26 // display Circle3’s string representation 27 output += “nn” + 28 “The new location and radius of circle are ” + 29 “n” + circle + “n”; 30 31 // display Circle3’s Diameter 32 output += “Diameter is ” + 33 String.Format( “{0:F}”, circle.Diameter() ) + “n”; 34 35 // display Circle3’s Circumference 36 output += “Circumference is ” + 37 String.Format( “{0:F}”, circle.Circumference() ) + “n”; 38 39 // display Circle3’s Area 40 output += “Area is ” + 41 String.Format( “{0:F}”, circle.Area() ); 42 43 MessageBox.Show( output, “Demonstrating Class Circle3″ ); 44 45 } // end method Main 46 47 } // end class CircleTest3 Fig. 9.11 Fig. 9.11Fig. 9.FiFi11g. 9.11g. 9.11CircleTest3demonstrates class Circle3functionality. (Part 2 of 2.) In the previous example, we declared the base-class instance variables as protected, so that a derived class could modify their values directly. The use of protectedvariables allows for a slight increase in performance, because we avoid incurring the overhead of a method call to a property s set or getaccessor. However, in most C# applications, in which user interaction comprises a large part of the execution time, the optimization offered through the use of protectedvariables is negligible. Using protected instance variables creates two major problems. First, the derived- class object does not have to use a property to set the value of the base-class s protected data. Therefore, a derived-class object can easily assign an illegal value to the protected
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.
Posted in MySQL5 | No Comments »