Chapter 10 Object-Oriented Programming: Polymorphism 387 method ToString (Web site directory)

Chapter 10 Object-Oriented Programming: Polymorphism 387 method ToString of point1 prints the object as a Point. Similarly, because circle1 is reference to a Circle object, method ToString of circle1 prints the object as a Circle. 1 // Fig. 10.3: PointCircleTest.cs 2 // Demonstrating inheritance and polymorphism. 3 4 using System; 5 using System.Windows.Forms; 6 7 // PointCircleTest class definition 8 class PointCircleTest 9 { 10 // main entry point for application. 11 static void Main( string[] args ) 12 { 13 Point point1 = new Point( 30, 50 ); 14 Circle circle1 = new Circle( 120, 89, 2.7 ); 15 16 string output = “Point point1: ” + point1.ToString() + 17 “nCircle circle1: ” + circle1.ToString(); 18 19 // use ‘is a’ relationship to assign 20 // Circle circle1 to Point reference 21 Point point2 = circle1; 22 23 output += “nnCCircle circle1 (via point2): ” + 24 point2.ToString(); 25 26 // downcast (cast base-class reference to derived-class 27 // data type) point2 to Circle circle2 28 Circle circle2 = ( Circle ) point2; 29 30 output += “nnCircle circle1 (via circle2): ” + 31 circle2.ToString(); 32 33 output += “nArea of circle1 (via circle2): ” + 34 circle2.Area().ToString( “F” ); 35 36 // attempt to assign point1 object to Circle reference 37 if ( point1 is Circle ) 38 { 39 circle2 = ( Circle ) point1; 40 output += “nncast successful”; 41 } 42 else 43 { 44 output += “nnpoint1 does not refer to a Circle”; 45 } 46 47 MessageBox.Show( output, 48 “Demonstrating the ‘is a’ relationship” ); Fig. 10.3 Fig. 10.FiFig. 10.3g. 10.3Assigning derived-class references to base-class references. (Part 1 of 2.) Fig. 10.3
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Leave a Reply