360 Object-Oriented Programming: Inheritance Chapter 9 49 //
360 Object-Oriented Programming: Inheritance Chapter 9 49 // calculate circumference 50 public double Circumference() 51 { 52 return Math.PI * Diameter(); 53 } 54 55 // calculate Circle area 56 public virtual double Area() 57 { 58 return Math.PI * Math.Pow( radius, 2 ); 59 } 60 61 // return string representation of Circle3 62 public override string ToString() 63 { 64 return “Center = [” + x + “, ” + y + “]” + 65 “; Radius = ” + radius; 66 } 67 68 } // end class Circle3 Fig. 9.10 Fig. 9.10Fig. 9.FiFi10g. 9.10g. 9.10Circle3class that inherits from class Point2. (Part 2 of 2.) Class CircleTest3(Fig. 9.11) performs identical tests on class Circle3as class CircleTest (Fig. 9.7) performed on class Circle (Fig. 9.6). Note that the outputs of the two programs are identical. We created class Circle without using inheritance and created class Circle3 using inheritance; however, both classes provide the same functionality. However, observe that the code listing for class Circle3, which is 68 lines, is considerably shorter than the code listing for class Circle, which is 98 lines, because class Circle3 absorbs part of its functionality from Point2, whereas class Circle does not. Also, there is now only one copy of the point functionality. 1 / Fig. 9.11: CircleTest3.cs 2 // Testing class Circle3. 3 4 using System; 5 using System.Windows.Forms; 6 7 // CircleTest3 class definition 8 class CircleTest3 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 // instantiate Circle3 14 Circle3 circle = new Circle3( 37, 43, 2.5 ); 15 16 // get Circle3’s initial x-y coordinates and radius 17 string output = “X coordinate is ” + circle.X + “n” + 18 “Y coordinate is ” + circle.Y + “nRadius is ” + 19 circle.Radius; Fig. 9.11 Fig. 9.11Fig. 9.FiFi11g. 9.11g. 9.11CircleTest3demonstrates class Circle3functionality. (Part 1 of 2.)
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.