chapter 9 – object-oriented programming: inheritance

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

Upload: tara

Post on 14-Jan-2016

38 views

Category:

Documents


0 download

DESCRIPTION

Chapter 9 – Object-Oriented Programming: Inheritance. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall. All rights reserved.

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

Page 2: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall. All rights reserved.

2

9.2 Base Classes and Derived Classes

Base class Derived classes Student GraduateStudent

UndergraduateStudent

Shape Circle Triangle Rectangle

Loan CarLoan HomeImprovementLoan MortgageLoan

Employee FacultyMember StaffMember

Account CheckingAccount SavingsAccount

Fig. 9.1 Inheritance examples.

Page 3: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall. All rights reserved.

3

9.2 Base Classes and Derived Classes

Fig. 9.2 Inheritance hierarchy for university CommunityMembers.

CommunityMemeber

Employee Student Alumnus

Faculty Staff

Administrator Teacher

Page 4: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall. All rights reserved.

4

9.2 Base Classes and Derived Classes

Fig. 9.3 Portion of a Shape class hierarchy.

Shape

TwoDimensionalShape ThreeDimensionalShape

Sphere Cube CylinderTriangleSquareCircle

Page 5: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline5

Point.cs

1 // Fig. 9.4: Point.cs2 // Class Point maintains an X and Y coordinate.3 4 using System;5 6 // Point class definition7 public class Point 8 {9 // point coordinate10 protected int xCoordinate, yCoordinate;11 12 // default constructor13 public Point()14 {15 // implicit call to base class constructor occurs here16 X = 0;17 Y = 0;18 }19 20 // constructor21 public Point( int xValue, int yValue )22 {23 // implicit call to base class constructor occurs here24 X = xValue;25 Y = yValue;26 }27 28 // property X29 public int X30 {31 get32 {33 return xCoordinate;34 }35

Page 6: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline6

Point.cs

Program Output

36 set 37 {38 xCoordinate = value;39 }40 }41 42 // property Y43 public int Y44 {45 get46 {47 return yCoordinate;48 }49 50 set 51 {52 yCoordinate = value;53 }54 }55 56 // return string representation of Point57 public override string ToString()58 {59 return "[" + X + ", " + Y + "]";60 }61 62 } // end class Point

Page 7: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline7

PointTest.cs

1 // Fig. 9.5: PointTest.cs2 // Manipulating a Point object.3 4 using System;5 using System.Windows.Forms;6 7 // PointTest class definition8 class PointTest9 {10 // main entry point for application11 static void Main( string[] args )12 {13 Point point = new Point( 72, 115 );14 15 // append initial Point coordinates to output16 string output = "X coordinate: " + point.X +17 "\nY coordinate: " + point.Y;18 19 // set new coordinates20 point.X = 10;21 point.Y = 10;22 23 // append new Point coordinates to output24 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 Main31 32 } // end class PointTest

Page 8: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline8

Circle.cs

1 // Fig. 9.6: Circle.cs2 // Class Circle maintains a radius and X and Y coordinates 3 // representing a circle's center.4 5 using System;6 7 // Circle class definition8 public class Circle 9 {10 // coordinates of circle center11 protected int xCoordinate, yCoordinate;12 13 // radius of circle14 protected double radius;15 16 // default constructor17 public Circle()18 {19 // implicit call to base class constructor occurs here20 X = 0;21 Y = 0;22 Radius = 0.0;23 }24 25 // constructor26 public Circle( int xValue, int yValue, double radiusValue )27 {28 // implicit call to base class constructor occurs here29 X = xValue;30 Y = yValue;31 Radius = radiusValue;32 }33

Page 9: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline9

Circle.cs

34 // property X35 public int X36 {37 get38 {39 return xCoordinate;40 }41 42 set 43 {44 xCoordinate = value;45 }46 }47 48 // property Y49 public int Y50 {51 get52 {53 return yCoordinate;54 }55 56 set 57 {58 yCoordinate = value;59 }60 }61 62 // property Radius63 public double Radius64 {65 get66 {67 return radius;68 }

Page 10: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline10

Circle.cs

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

Page 11: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline11

CircleTest.cs

1 // Fig. 9.7: CircleTest.cs2 // Manipulating a Circle object.3 4 using System;5 using System.Windows.Forms;6 7 // CircleTest class definition8 class CircleTest9 {10 // main entry point for application11 static void Main( string[] args )12 {13 Circle circle = new Circle( 37, 43, 2.5 );14 15 // append Circle properties to output16 string output = "X coordinate: " + circle.X +17 "\nY coordinate: " + circle.Y + 18 "\nRadius: " + circle.Radius;19 20 // set new coordinates and radius21 circle.X = 10;22 circle.Y = 10;23 circle.Radius = 4.25;24 25 // append new Point coordinates to output26 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 Main34 35 } // end class CircleTest

Page 12: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline12

CircleTest.cs Program Output

Page 13: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline13

Circle.cs

1 // Fig. 9.8: Circle.cs2 // Class Circle inherits from Point.3 4 using System;5 6 // Circle class definition7 public class Circle : Point8 {9 // radius of circle10 protected double radius;11 12 // default constructor13 public Circle()14 {15 // implicit call to base class constructor occurs here16 Radius = 0.0;17 }18 19 // constructor20 public Circle( int xValue, int yValue, double radiusValue )21 : base( xValue, yValue )22 {23 Radius = radiusValue;24 }25 26 // property Radius27 public double Radius28 {29 get30 {31 return radius;32 }33

Page 14: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline14

Circle.cs

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

Page 15: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline15

Point.cs

1 // Fig. 9.9: Point.cs2 // Class Point maintains an X and Y coordinate.3 4 using System;5 6 // Point class definition7 public class Point 8 {9 // point coordinate10 protected int xCoordinate, yCoordinate;11 12 // default constructor13 public Point()14 {15 // implicit call to base class constructor occurs here16 X = 0;17 Y = 0;18 Console.WriteLine( "Point constructor: " + this );19 }20 21 // constructor22 public Point( int xValue, int yValue )23 {24 // implicit call to base class constructor occurs here25 X = xValue;26 Y = yValue;27 Console.WriteLine( "Point constructor: " + this );28 }29 30 // destructor31 ~Point()32 { 33 Console.WriteLine( "Point destructor: " + this );34 }35

Page 16: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline16

Point.cs

36 // property X37 public int X38 {39 get40 {41 return xCoordinate;42 }43 44 set 45 {46 xCoordinate = value;47 }48 }49 50 // property Y51 public int Y52 {53 get54 {55 return yCoordinate;56 }57 58 set 59 {60 yCoordinate = value;61 }62 }63 64 // return string representation of Point65 public override string ToString()66 {67 return "[" + X + ", " + Y + "]";68 }69 70 } // end class Point

Page 17: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline17

Circle.cs

1 // Fig. 9.10: Circle.cs2 // Class Circle inherits from Point.3 4 using System;5 6 // Circle class definition7 public class Circle : Point8 {9 // radius of circle10 protected double radius;11 12 // default constructor13 public Circle()14 {15 // implicit call to base class constructor occurs here16 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 // destructor29 ~Circle()30 {31 Console.WriteLine( "Circle destructor: " + this );32 }33

Page 18: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline18

Circle.cs

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

Page 19: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline19

ConstructorAndDestructor.cs

1 // Fig. 9.11: ConstructorAndDestructor.cs2 // Demonstrating the order in which base-class and 3 // derived-class constructors and destructors are called.4 5 using System;6 7 // ConstructorAndDestructor class definition8 class ConstructorAndDestructor9 {10 // main entry point for application11 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 objects22 circle1 = null;23 circle2 = null;24 25 System.GC.Collect();26 27 } // end method Main28 29 } // end class ConstructorAndDestructor

Page 20: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline20

ConstructorAndDestructor.cs Program Output

Point constructor: Center = [72, 29]; Radius = 0Circle constructor: Center = [72, 29]; Radius = 4.5

Point constructor: Center = [5, 5]; Radius = 0Circle constructor: Center = [5, 5]; Radius = 10

Circle destructor: Center = [5, 5]; Radius = 10Point destructor: Center = [5, 5]; Radius = 10Circle destructor: Center = [72, 29]; Radius = 4.5Point destructor: Center = [72, 29]; Radius = 4.5

Page 21: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline21

Circle.cs

1 // Fig. 9.12: Circle.cs2 // Class Circle inherits from Point.3 4 using System;5 6 // Circle class definition7 public class Circle : Point8 {9 // radius of circle10 protected double radius;11 12 // default constructor13 public Circle()14 {15 // implicit call to base class constructor occurs here16 Radius = 0.0;17 }18 19 // constructor20 public Circle( int xValue, int yValue, double radiusValue )21 : base( xValue, yValue )22 {23 Radius = radiusValue;24 }25 26 // property Radius27 public double Radius28 {29 get30 {31 return radius;32 }33

Page 22: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline22

Circle.cs

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

Page 23: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline23

CircleTest2.cs

1 // Fig. 9.13: CircleTest2.cs2 // Manipulating a Circle object.3 4 using System;5 using System.Windows.Forms;6 7 // CircleTest class definition8 class CircleTest29 {10 // main entry point for application11 static void Main( string[] args )12 {13 Circle circle = new Circle( 37, 43, 2.5 );14 15 // append Circle properties to output16 string output = "X coordinate: " + circle.X +17 "\nY coordinate: " + circle.Y + 18 "\nRadius: " + circle.Radius;19 20 // set new coordinates and radius21 circle.X = 10;22 circle.Y = 10;23 circle.Radius = 4.25;24 25 // append new Point coordinates to output26 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 Main34 35 } // end class CircleTest2

Page 24: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline24

CircleTest2.cs Program Output

Page 25: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline25

Cylinder.cs

1 // Fig. 9.14: Cylinder.cs2 // Class Cylinder inherits from Circle.3 4 using System;5 6 // Cylinder class definition7 public class Cylinder : Circle8 {9 protected double height;10 11 // default constructor12 public Cylinder()13 {14 // implicit call to base class constructor occurs here15 Height = 0.0;16 }17 18 // constructor19 public Cylinder( int xValue, int yValue, 20 double radiusValue, double heightValue )21 : base( xValue, yValue, radiusValue )22 {23 Height = heightValue;24 }25 26 // property Height27 public double Height28 {29 get30 {31 return height;32 }33

Page 26: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline26

Cylinder.cs

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

Page 27: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline27

CylinderTest.cs

1 // Fig. 9.15: CylinderTest.cs2 // Manipulating a Cylinder object.3 4 using System;5 using System.Windows.Forms;6 7 // CylinderTest class definition8 class CylinderTest9 {10 // main entry point for application11 static void Main( string[] args )12 {13 Cylinder cylinder = new Cylinder( 12, 23, 2.5, 5.7 );14 15 // append Cylinder properties to output16 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 radius22 cylinder.X = 10;23 cylinder.Y = 10;24 cylinder.Radius = 10;25 cylinder.Height = 4.25;26 27 // append new Point coordinates to output28 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

Page 28: Chapter 9 – Object-Oriented Programming: Inheritance

2002 Prentice Hall.All rights reserved.

Outline28

CylinderTest.cs

Program Output

36 MessageBox.Show( output, "Demonstrating Class Cylinder" );37 38 } // end method Main39 40 } // end class CylinderTest