1 / 28

Chapter 9 – Object-Oriented Programming: Inheritance

Chapter 9 – Object-Oriented Programming: Inheritance.

tara
Download Presentation

Chapter 9 – Object-Oriented Programming: Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9 – Object-Oriented Programming: Inheritance Outline9.1 Introduction9.2 Base Classes and Derived Classes9.3 protected Members9.4 Creating Base Classes and Derived Classes9.5 Constructors and Destructors in Derived Classes9.6 Software Engineering with Inheritance9.7 Case Study: Point, Circle, Cylinder

  2. 9.2 Base Classes and Derived Classes

  3. 9.2 Base Classes and Derived Classes CommunityMemeber Employee Student Alumnus Faculty Staff Administrator Teacher Fig. 9.2 Inheritance hierarchy for university CommunityMembers.

  4. 9.2 Base Classes and Derived Classes Shape TwoDimensionalShape ThreeDimensionalShape Circle Square Triangle Sphere Cube Cylinder Fig. 9.3 Portion of a Shape class hierarchy.

  5. 1 // Fig. 9.4: Point.cs 2 // Class Point maintains an X and Y coordinate. 3 4 using System; 5 6 // Point class definition 7 public class Point 8 { 9 // point coordinate 10 protected int xCoordinate, yCoordinate; 11 12 // default constructor 13 public Point() 14 { 15 // implicit call to base class constructor occurs here 16 X = 0; 17 Y = 0; 18 } 19 20 // constructor 21 public Point( int xValue, int yValue ) 22 { 23 // implicit call to base class constructor occurs here 24 X = xValue; 25 Y = yValue; 26 } 27 28 // property X 29 public int X 30 { 31 get 32 { 33 return xCoordinate; 34 } 35 Point.cs

  6. 36 set 37 { 38 xCoordinate = value; 39 } 40 } 41 42 // property Y 43 public int Y 44 { 45 get 46 { 47 return yCoordinate; 48 } 49 50 set 51 { 52 yCoordinate = value; 53 } 54 } 55 56 // return string representation of Point 57 public override string ToString() 58 { 59 return"[" + X + ", " + Y + "]"; 60 } 61 62 } // end class Point Point.cs Program Output

  7. 1 // Fig. 9.5: PointTest.cs 2 // Manipulating a Point object. 3 4 using System; 5 using System.Windows.Forms; 6 7 // PointTest class definition 8 class PointTest 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 Point point = new Point( 72, 115 ); 14 15 // append initial Point coordinates to output 16 string output = "X coordinate: " + point.X + 17 "\nY coordinate: " + point.Y; 18 19 // set new coordinates 20 point.X = 10; 21 point.Y = 10; 22 23 // append new Point coordinates to output 24 output += "\n\nNew location of point: " + 25 point.ToString() + "\nImplicit ToString call: " + 26 point; 27 28 MessageBox.Show( output, "Demonstrating Class Point" ); 29 30 } // end method Main 31 32 } // end class PointTest PointTest.cs

  8. 1 // Fig. 9.6: Circle.cs 2 // Class Circle maintains a radius and X and Y coordinates 3 // representing a circle's center. 4 5 using System; 6 7 // Circle class definition 8 public class Circle 9 { 10 // coordinates of circle center 11 protected int xCoordinate, yCoordinate; 12 13 // radius of circle 14 protected double radius; 15 16 // default constructor 17 public Circle() 18 { 19 // implicit call to base class constructor occurs here 20 X = 0; 21 Y = 0; 22 Radius = 0.0; 23 } 24 25 // constructor 26 public Circle( int xValue, int yValue, double radiusValue ) 27 { 28 // implicit call to base class constructor occurs here 29 X = xValue; 30 Y = yValue; 31 Radius = radiusValue; 32 } 33 Circle.cs

  9. 34 // property X 35 public int X 36 { 37 get 38 { 39 return xCoordinate; 40 } 41 42 set 43 { 44 xCoordinate = value; 45 } 46 } 47 48 // property Y 49 public int Y 50 { 51 get 52 { 53 return yCoordinate; 54 } 55 56 set 57 { 58 yCoordinate = value; 59 } 60 } 61 62 // property Radius 63 public double Radius 64 { 65 get 66 { 67 return radius; 68 } Circle.cs

  10. 69 70 set 71 { 72 radius = ( value >= 0.0 ? value : 0.0 ); 73 } 74 } 75 76 // return area of Circle 77 public double Area() 78 { 79 return Math.PI * radius * radius; 80 } 81 82 // return string representation of Circle 83 public override string ToString() 84 { 85 return"Center = [" + X + ", " + Y + 86 "]; Radius = " + radius; 87 } 88 89 } // end class Circle Circle.cs

  11. 1 // Fig. 9.7: CircleTest.cs 2 // Manipulating a Circle object. 3 4 using System; 5 using System.Windows.Forms; 6 7 // CircleTest class definition 8 class CircleTest 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 Circle circle = new Circle( 37, 43, 2.5 ); 14 15 // append Circle properties to output 16 string output = "X coordinate: " + circle.X + 17 "\nY coordinate: " + circle.Y + 18 "\nRadius: " + circle.Radius; 19 20 // set new coordinates and radius 21 circle.X = 10; 22 circle.Y = 10; 23 circle.Radius = 4.25; 24 25 // append new Point coordinates to output 26 output += "\n\nNew location and radius of circle: " + 27 circle.ToString() + 28 "\nImplicit ToString call: " + circle + "\nArea: " + 29 String.Format( "{0:F2}", circle.Area() ); 30 31 MessageBox.Show( output, "Demonstrating Class Circle" ); 32 33 } // end method Main 34 35 } // end class CircleTest CircleTest.cs

  12. CircleTest.cs Program Output

  13. 1 // Fig. 9.8: Circle.cs 2 // Class Circle inherits from Point. 3 4 using System; 5 6 // Circle class definition 7 public class Circle : Point 8 { 9 // radius of circle 10 protected double radius; 11 12 // default constructor 13 public Circle() 14 { 15 // implicit call to base class constructor occurs here 16 Radius = 0.0; 17 } 18 19 // constructor 20 public Circle( int xValue, int yValue, double radiusValue ) 21 : base( xValue, yValue ) 22 { 23 Radius = radiusValue; 24 } 25 26 // property Radius 27 public double Radius 28 { 29 get 30 { 31 return radius; 32 } 33 Circle.cs

  14. 34 set 35 { 36 radius = ( value >= 0.0 ? value : 0.0 ); 37 } 38 } 39 40 // return area of Circle 41 public double Area() 42 { 43 return Math.PI * radius * radius; 44 } 45 46 // return string representation of Circle 47 public override string ToString() 48 { 49 return"Center = [" + xCoordinate + ", " + yCoordinate + 50 "]; Radius = " + radius; 51 } 52 53 } // end class Circle Circle.cs

  15. 1 // Fig. 9.9: Point.cs 2 // Class Point maintains an X and Y coordinate. 3 4 using System; 5 6 // Point class definition 7 public class Point 8 { 9 // point coordinate 10 protected int xCoordinate, yCoordinate; 11 12 // default constructor 13 public Point() 14 { 15 // implicit call to base class constructor occurs here 16 X = 0; 17 Y = 0; 18 Console.WriteLine( "Point constructor: " + this ); 19 } 20 21 // constructor 22 public Point( int xValue, int yValue ) 23 { 24 // implicit call to base class constructor occurs here 25 X = xValue; 26 Y = yValue; 27 Console.WriteLine( "Point constructor: " + this ); 28 } 29 30 // destructor 31 ~Point() 32 { 33 Console.WriteLine( "Point destructor: " + this ); 34 } 35 Point.cs

  16. 36 // property X 37 public int X 38 { 39 get 40 { 41 return xCoordinate; 42 } 43 44 set 45 { 46 xCoordinate = value; 47 } 48 } 49 50 // property Y 51 public int Y 52 { 53 get 54 { 55 return yCoordinate; 56 } 57 58 set 59 { 60 yCoordinate = value; 61 } 62 } 63 64 // return string representation of Point 65 public override string ToString() 66 { 67 return"[" + X + ", " + Y + "]"; 68 } 69 70 } // end class Point Point.cs

  17. 1 // Fig. 9.10: Circle.cs 2 // Class Circle inherits from Point. 3 4 using System; 5 6 // Circle class definition 7 public class Circle : Point 8 { 9 // radius of circle 10 protecteddouble radius; 11 12 // default constructor 13 public Circle() 14 { 15 // implicit call to base class constructor occurs here 16 Radius = 0.0; 17 Console.WriteLine( "Circle constructor: " + this ); 18 } 19 20 // constructor 21 public Circle( int xValue, int yValue, double radiusValue ) 22 : base( xValue, yValue ) 23 { 24 Radius = radiusValue; 25 Console.WriteLine( "Circle constructor: " + this ); 26 } 27 28 // destructor 29 ~Circle() 30 { 31 Console.WriteLine( "Circle destructor: " + this ); 32 } 33 Circle.cs

  18. 34 // property Radius 35 public double Radius 36 { 37 get 38 { 39 return radius; 40 } 41 42 set 43 { 44 radius = ( value >= 0.0 ? value : 0.0 ); 45 } 46 } 47 48 // return string representation of Circle 49 public override string ToString() 50 { 51 return"Center = " + base.ToString() + 52 "; Radius = " + radius; 53 } 54 55 } // end class Circle Circle.cs

  19. 1 // Fig. 9.11: ConstructorAndDestructor.cs 2 // Demonstrating the order in which base-class and 3 // derived-class constructors and destructors are called. 4 5 using System; 6 7 // ConstructorAndDestructor class definition 8 class ConstructorAndDestructor 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 Circle circle1 = new Circle( 72, 29, 4.5 ); 14 15 Console.WriteLine(); 16 17 Circle circle2 = new Circle( 5, 5, 10 ); 18 19 Console.WriteLine(); 20 21 // remove references to Circle objects 22 circle1 = null; 23 circle2 = null; 24 25 System.GC.Collect(); 26 27 } // end method Main 28 29 } // end class ConstructorAndDestructor ConstructorAndDestructor.cs

  20. Point constructor: Center = [72, 29]; Radius = 0 Circle constructor: Center = [72, 29]; Radius = 4.5 Point constructor: Center = [5, 5]; Radius = 0 Circle constructor: Center = [5, 5]; Radius = 10 Circle destructor: Center = [5, 5]; Radius = 10 Point destructor: Center = [5, 5]; Radius = 10 Circle destructor: Center = [72, 29]; Radius = 4.5 Point destructor: Center = [72, 29]; Radius = 4.5 ConstructorAndDestructor.cs Program Output

  21. 1 // Fig. 9.12: Circle.cs 2 // Class Circle inherits from Point. 3 4 using System; 5 6 // Circle class definition 7 public class Circle : Point 8 { 9 // radius of circle 10 protected double radius; 11 12 // default constructor 13 public Circle() 14 { 15 // implicit call to base class constructor occurs here 16 Radius = 0.0; 17 } 18 19 // constructor 20 public Circle( int xValue, int yValue, double radiusValue ) 21 : base( xValue, yValue ) 22 { 23 Radius = radiusValue; 24 } 25 26 // property Radius 27 public double Radius 28 { 29 get 30 { 31 return radius; 32 } 33 Circle.cs

  22. 34 set 35 { 36 radius = ( value >= 0.0 ? value : 0.0 ); 37 } 38 } 39 40 // return area of Circle 41 public virtual double Area() 42 { 43 return Math.PI * radius * radius; 44 } 45 46 // return string representation of Circle 47 public override string ToString() 48 { 49 return"Center = " + base.ToString() + 50 "; Radius = " + radius; 51 } 52 53 } // end class Circle Circle.cs

  23. 1 // Fig. 9.13: CircleTest2.cs 2 // Manipulating a Circle object. 3 4 using System; 5 using System.Windows.Forms; 6 7 // CircleTest class definition 8 class CircleTest2 9 { 10 // main entry point for application 11 static void Main( string[] args ) 12 { 13 Circle circle = new Circle( 37, 43, 2.5 ); 14 15 // append Circle properties to output 16 string output = "X coordinate: " + circle.X + 17 "\nY coordinate: " + circle.Y + 18 "\nRadius: " + circle.Radius; 19 20 // set new coordinates and radius 21 circle.X = 10; 22 circle.Y = 10; 23 circle.Radius = 4.25; 24 25 // append new Point coordinates to output 26 output += "\n\nNew location and radius of circle: " + 27 circle.ToString() + 28 "\nImplicit ToString call: " + circle + "\nArea: " + 29 String.Format( "{0:F2}", circle.Area() ); 30 31 MessageBox.Show( output, "Demonstrating Class Circle" ); 32 33 } // end method Main 34 35 } // end class CircleTest2 CircleTest2.cs

  24. CircleTest2.cs Program Output

  25. 1 // Fig. 9.14: Cylinder.cs 2 // Class Cylinder inherits from Circle. 3 4 using System; 5 6 // Cylinder class definition 7 publicclass Cylinder : Circle 8 { 9 protected double height; 10 11 // default constructor 12 public Cylinder() 13 { 14 // implicit call to base class constructor occurs here 15 Height = 0.0; 16 } 17 18 // constructor 19 public Cylinder( int xValue, int yValue, 20 double radiusValue, double heightValue ) 21 : base( xValue, yValue, radiusValue ) 22 { 23 Height = heightValue; 24 } 25 26 // property Height 27 public double Height 28 { 29 get 30 { 31 return height; 32 } 33 Cylinder.cs

  26. 34 set 35 { 36 height = ( value >= 0.0 ? value : 0.0 ); 37 } 38 } 39 40 // return area of Cylinder 41 public override double Area() 42 { 43 return 2 * base.Area() + 2 * Math.PI * radius * radius; 44 } 45 46 // return volume of Cylinder 47 public double Volume() 48 { 49 returnbase.Area() * height; 50 } 51 52 // return string representation of Cylinder 53 public override string ToString() 54 { 55 returnbase.ToString() + "; Heigth = " + height; 56 } 57 58 } // end class Cylinder Cylinder.cs

  27. 1 // Fig. 9.15: CylinderTest.cs 2 // Manipulating a Cylinder object. 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 Cylinder cylinder = new Cylinder( 12, 23, 2.5, 5.7 ); 14 15 // append Cylinder properties to output 16 string output = "X coordinate: " + cylinder.X + 17 "\nY coordinate: " + cylinder.Y + 18 "\nRadius: " + cylinder.Radius + 19 "\nHeight: " + cylinder.Height; 20 21 // set new coordinates and radius 22 cylinder.X = 10; 23 cylinder.Y = 10; 24 cylinder.Radius = 10; 25 cylinder.Height = 4.25; 26 27 // append new Point coordinates to output 28 output += 29 "\n\nNew location, radius and height of cylinder: " + 30 cylinder.ToString() + "\nImplicit ToString call: " + 31 cylinder + "\nArea: " + 32 String.Format( "{0:F2}", cylinder.Area() ) + 33 "\nVolume: " + 34 String.Format( "{0:F2}", cylinder.Volume() ); 35 CylinderTest.cs

  28. 36 MessageBox.Show( output, "Demonstrating Class Cylinder" ); 37 38 } // end method Main 39 40 } // end class CylinderTest CylinderTest.cs Program Output

More Related