Chapter 9 Object-Oriented (Best web site) Programming: Inheritance 365 52 //
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.