Web page design - 384 Object-Oriented Programming: Polymorphism Chapter 10 references that

384 Object-Oriented Programming: Polymorphism Chapter 10 references that refer to objects of many derived-class types. This is allowed despite the fact that the derived-class objects are of different data types. However, the reverse is not true a base-class object is not an object of any of its derived classes. For example, a Point is not a Circle in the hierarchy defined in Chapter 9. If a base-class reference refers to a derived-class object, it is possible to convert the base-class reference to the object s actual data type and manipulate the object as that type. The example in Fig. 10.1 Fig. 10.3 demonstrates assigning derived-class objects to base-class references and casting base-class references to derived-class references. Class Point (Fig. 10.1), which we discussed in Chapter 9, represents an x-y coordinate pair. Class Circle (Fig. 10.2), which we also discussed in Chapter 9, represents a circle and inherits from class Point. Each Circleobject is a Pointand also has a radius (represented via property Radius). We declare method Areaas virtual, so that a derived class (such as class Cylinder) can override method Areato calculate the derived-class object s area. Class PointCircleTest (Fig. 10.3) demonstrates the assignment and cast operations. 1 // Fig. 10.1: Point.cs 2 // Point class represents an x-y coordinate pair. 3 4 using System; 5 6 // Point class definition implicitly inherits from Object 7 public class Point 8 { 9 // point coordinate 10 private int x, y; 11 12 // default constructor 13 public Point() 14 { 15 // implicit call to Object constructor occurs here 16 } 17 18 // constructor 19 public Point( int xValue, int yValue ) 20 { 21 // implicit call to Object constructor occurs here 22 X = xValue; 23 Y = yValue; 24 } 25 26 // property X 27 public int X 28 { 29 get 30 { 31 return x; 32 } 33 Fig. 10.1 Fig. 10.1Fig. 10FiFi.1g. 10.1g. 10.1Pointclass represents an x-y coordinate pair. (Part 1 of 2.)
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Leave a Reply