Chapter 9 Object-Oriented Programming: Inheritance 351 16 // (Web site templates)
Wednesday, December 12th, 2007Chapter 9 Object-Oriented Programming: Inheritance 351 16 // display point coordinates via X and Y properties 17 string output = “X coordinate is ” + point.X + 18 “n” + “Y coordinate is ” + point.Y; 19 20 point.X = 10; // set x-coordinate via X property 21 point.Y = 10; // set y-coordinate via Y property 22 23 // display new point value 24 output += “nnThe new location of point is ” + point; 25 26 MessageBox.Show( output, “Demonstrating Class Point” ); 27 28 } // end method Main 29 30 } // end class PointTest Fig. 9.5 Fig. 9.5Fig. 9.FiFi5g. 9.5g. 9.5PointTestclass demonstrates class Pointfunctionality. (Part 2 of 2.) We now discuss the second part of our introduction to inheritance by creating and testing (a completely new) class Circle (Fig. 9.6), which directly inherits from class System.Object and represents an x-y coordinate pair (representing the center of the circle) and a radius. Lines 9 10 declare the instance variables x, yand radius as private data. The public services of class Circle include two Circle constructors (lines 13 25), properties X, Yand Radius(lines 28 71), methods Diameter(lines 74 77), Circumference(lines 80 83), Area(lines 86 89) and ToString(lines 92 96). These properties and methods encapsulate all necessary features (i.e., the analytic geometry ) of a circle; in the next section, we show how this encapsulation enables us to reuse and extend this class. 1 // Fig. 9.6: Circle.cs 2 // Circle class contains x-y coordinate pair and radius. 3 4 using System; 5 6 // Circle class definition implicitly inherits from Object 7 public class Circle 8 { 9 private int x, y; // coordinates of Circle’s center 10 private double radius; // Circle’s radius 11 Fig. 9.6 Fig. 9.6Fig. 9.FiFi6g. 9.6g. 9.6Circleclass contains an x-y coordinate and a radius. (Part 1 of 3.)
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.