Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.

Similar presentations


Presentation on theme: " 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes."— Presentation transcript:

1  2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes 9.3 protected Members 9.4Creating Base Classes and Derived Classes 9.5Constructors and Destructors in Derived Classes 9.6Software Engineering with Inheritance 9.7Case Study: Point, Circle, Cylinder

2  2002 Prentice Hall. All rights reserved. 2 9.2 Base Classes and Derived Classes

3  2002 Prentice Hall. All rights reserved. 3 9.2 Base Classes and Derived Classes Fig. 9.2Inheritance hierarchy for university CommunityMember s. CommunityMemeber EmployeeStudentAlumnus FacultyStaff AdministratorTeacher

4  2002 Prentice Hall. All rights reserved. 4 9.2 Base Classes and Derived Classes Fig. 9.3Portion of a Shape class hierarchy. Shape TwoDimensionalShapeThreeDimensionalShape SphereCubeCylinderTriangleSquareCircle

5  2002 Prentice Hall. All rights reserved. Outline 5 Point.cs 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

6  2002 Prentice Hall. All rights reserved. Outline 6 Point.cs Program Output 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

7  2002 Prentice Hall. All rights reserved. Outline 7 PointTest.cs 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

8  2002 Prentice Hall. All rights reserved. Outline 8 Circle.cs 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

9  2002 Prentice Hall. All rights reserved. Outline 9 Circle.cs 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 }

10  2002 Prentice Hall. All rights reserved. Outline 10 Circle.cs 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

11  2002 Prentice Hall. All rights reserved. Outline 11 CircleTest.cs 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

12  2002 Prentice Hall. All rights reserved. Outline 12 CircleTest.cs Program Output

13  2002 Prentice Hall. All rights reserved. Outline 13 Circle.cs 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

14  2002 Prentice Hall. All rights reserved. Outline 14 Circle.cs 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

15  2002 Prentice Hall. All rights reserved. Outline 15 Point.cs 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

16  2002 Prentice Hall. All rights reserved. Outline 16 Point.cs 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

17  2002 Prentice Hall. All rights reserved. Outline 17 Circle.cs 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 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 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

18  2002 Prentice Hall. All rights reserved. Outline 18 Circle.cs 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

19  2002 Prentice Hall. All rights reserved. Outline 19 ConstructorAndDe structor.cs 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

20  2002 Prentice Hall. All rights reserved. Outline 20 ConstructorAndDe structor.cs Program Output 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

21  2002 Prentice Hall. All rights reserved. Outline 21 Circle.cs 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

22  2002 Prentice Hall. All rights reserved. Outline 22 Circle.cs 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

23  2002 Prentice Hall. All rights reserved. Outline 23 CircleTest2.cs 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

24  2002 Prentice Hall. All rights reserved. Outline 24 CircleTest2.cs Program Output

25  2002 Prentice Hall. All rights reserved. Outline 25 Cylinder.cs 1 // Fig. 9.14: Cylinder.cs 2 // Class Cylinder inherits from Circle. 3 4 using System; 5 6 // Cylinder class definition 7 public class 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

26  2002 Prentice Hall. All rights reserved. Outline 26 Cylinder.cs 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 return base.Area() * height; 50 } 51 52 // return string representation of Cylinder 53 public override string ToString() 54 { 55 return base.ToString() + "; Heigth = " + height; 56 } 57 58 } // end class Cylinder

27  2002 Prentice Hall. All rights reserved. Outline 27 CylinderTest.cs 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

28  2002 Prentice Hall. All rights reserved. Outline 28 CylinderTest.cs Program Output 36 MessageBox.Show( output, "Demonstrating Class Cylinder" ); 37 38 } // end method Main 39 40 } // end class CylinderTest


Download ppt " 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes."

Similar presentations


Ads by Google