Apache web server - 370 Object-Oriented Programming: Inheritance Chapter 9 Height to
370 Object-Oriented Programming: Inheritance Chapter 9 Height to obtain information about the Cylinder object, because CylinderTest cannot reference the privatedata of class Cylinderdirectly. Lines 22 25 use properties X, Y, Radiusand Heightto reset the Cylinder s x-y coordinates (we assume the cylinder s x-y coordinates specify its position on the x-y plane), radius and height. Class Cylindercan use class Point3 s Xand Yproperties, because class Cylinderinherits them indirectly from class Point3 Class Cylinder inherits properties X and Y directly from class Circle4, which inherited them directly from class Point3. Line 29 invokes method ToString implicitly to obtain the string representation of the Cylinder object. Lines 33 37 invoke methods Diameter and Circumference of the Cylinder object because class Cylinder inherits these methods from class Circle4 and cannot override them, these methods, exactly as listed in Circle4, are invoked. Lines 41 45 invoke methods Areaand Volume. Using the point-circle-cylinder example, we have shown the use and benefits of inheritance. We were able to develop classes Circle4and Cylinderusing inheritance much faster than if we had developed these classes from scratch. Inheritance avoids duplicating code and the associated code-maintenance problems. 1 // Fig. 9.16: CylinderTest.cs 2 // Tests class Cylinder. 3 4 using System; 5 using System.Windows.Forms; 6 7 // CylinderTest class definition 8 class CylinderTest 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 // instantiate object of class Cylinder 14 Cylinder cylinder = new Cylinder(12, 23, 2.5, 5.7); 15 16 // properties get initial x-y coordinate, radius and height 17 string output = “X coordinate is ” + cylinder.X + “n” + 18 “Y coordinate is ” + cylinder.Y + “nRadius is ” + 19 cylinder.Radius + “n” + “Height is ” + cylinder.Height; 20 21 // properties set new x-y coordinate, radius and height 22 cylinder.X = 2; 23 cylinder.Y = 2; 24 cylinder.Radius = 4.25; 25 cylinder.Height = 10; 26 27 // get new x-y coordinate and radius 28 output += “nnThe new location, radius and height of ” + 29 “cylinder aren” + cylinder + “nn”; 30 31 // display Cylinder’s Diameter 32 output += “Diameter is ” + 33 String.Format( “{0:F}”, cylinder.Diameter() ) + “n”; Fig. 9.16 Fig. 9.16Fig. 9.FiFi16g. 9.16g. 9.16Testing class Cylinder(Part 1 of 2.).
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.