1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

Outline 1 Introduction 2 Base Classes and Derived Classes 3 protected Members 4 Relationship between Base Classes and Derived Classes 5 Case Study: Three-Level.
Chapter 19 - Inheritance Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
Chapter 20- Virtual Functions and Polymorphism Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng.
Shape.h Shape Point Circle Cylinder // SHAPE.H
Esempi Ereditarietà1 // Definition of class Point #ifndef POINT_H #define POINT_H #include using std::ostream; class Point { friend ostream &operator
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected Members 9.4 Relationship.
C++ Inheritance Systems Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
 2002 Prentice Hall. All rights reserved. Some slides modified by LL 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base.
Outline 1 Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü ++ Bilişim Enstitüsü.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20- Virtual Functions and Polymorphism Outline 20.1Introduction 20.2Type Fields and switch Statements.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 - Inheritance Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes 19.3Protected.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 Review (Week7)_ Classes Part II Outline Composition: Objects as Members of Classes Dynamic Memory static Class Members Operator Overloading.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
1 Lecture Note 9_Polymorphism Outline Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class object Virtual.
Computer Science I Inheritance Professor Evan Korth New York University.
Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Chapter 12 Object-Oriented Programming: Inheritance Chapter 12 Object-Oriented Programming: Inheritance Part I.
Object-Oriented Programming: Polymorphism Zhen Jiang West Chester University
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2Base Classes and Derived Classes.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived.
 2003 Prentice Hall, Inc. All rights reserved 1 Object-Oriented Programming Inheritance Main Objectives: To understand how inheritance.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 19 - C++ Inheritance Outline 19.1Introduction.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance Part 2 Outline 19.8Direct Base Classes and Indirect Base Classes 19.9Using Constructors.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Inheritance Outline 9.1Introduction 9.2Inheritance: Base Classes and Derived Classes 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Superclasses and Subclasses.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Polymorphism Lecture 9 March 16, 2004.
Jozef Goetz contribution,  2011 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
 2003 Prentice Hall, Inc. All rights reserved Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point.
 2003 Prentice Hall, Inc. All rights reserved Virtual Functions Polymorphism –Same message, “print”, given to many objects All through a base.
 2003 Prentice Hall, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects.
Jozef Goetz contribution,  2011, 2014 Pearson Education, Inc. All rights reserved.  2002 Prentice Hall. All rights reserved.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 19 – Inheritance – Part 1 Outline 19.1Introduction 19.2Inheritance: Base Classes and Derived Classes.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Polymorphism Outline 20.5Polymorphism 20.6Case Study: A Payroll System Using Polymorphism.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Object-Oriented Programming: Polymorphism.
Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class.
Object-Oriented Programming: Polymorphism Outline 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class.
1 Inheritance inheritance –a mechanism that build a new class by deriving from an existing class –derived class (or subclass) inherit from based class.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Chapter 9 – Object-Oriented Programming: Inheritance
Operator Overloading, Inheritance Lecture 7 September 23, 2004
Chapter 9 - Object-Oriented Programming: Inheritance
Object-Oriented Programming: Inheritance
Chapter 20- Virtual Functions and Polymorphism
Object-Oriented Programming: Inheritance
Chapter 20 - C++ Virtual Functions and Polymorphism
Object-Oriented Programming: Polymorphism
Chapter 19 - Inheritance Outline 19.1 Introduction
Recitation Course 0610 Speaker: Liu Yu-Jiun.
Chapter 9 - Object-Oriented Programming: Inheritance
Virtual Functions Example
Presentation transcript:

1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology

2 Chapter 9 - Object-Oriented Programming: Inheritance Outline 9.1 Introduction 9.2 Base Classes and Derived Classes 9.3 protected Members 9.4 Relationship between Base Classes and Derived Classes 9.5 Case Study: Three-Level Inheritance Hierarchy 9.6 Constructors and Destructors in Derived Classes 9.8 public, protected and private Inheritance 9.9 Software Engineering with Inheritance

3 9.1 Introduction Inheritance –Software reusability –Create new class from existing class Absorb existing class’s data and behaviors Enhance with new capabilities –Derived class inherits from base class Derived class –More specialized group of objects –Behaviors inherited from base class Can customize –Additional behaviors

4 9.1 Introduction Class hierarchy –Direct base class Inherited explicitly (one level up hierarchy) –Indirect base class Inherited two or more levels up hierarchy –Single inheritance Inherits from one base class –Multiple inheritance Inherits from multiple base classes –Base classes possibly unrelated Chapter 22

5 9.1 Introduction Three types of inheritance –public Every object of derived class also object of base class –Base-class objects not objects of derived classes –Example: All cars are vehicles, but not all vehicles are cars Can access non- private members of base class –private Alternative to composition Chapter 17 –protected Rarely used

6 9.1 Introduction “is-a” vs. “has-a” –“is-a” Inheritance Derived class object treated as base class object Example: Car is a vehicle –Vehicle properties/behaviors also car properties/behaviors –“has-a” Composition Object contains one or more objects of other classes as members Example: Car has a steering wheel

7 9.2 Base Classes and Derived Classes Base classes and derived classes –Object of one class “is an” object of another class Example: Rectangle is quadrilateral. –Class Rectangle inherits from class Quadrilateral –Quadrilateral : base class –Rectangle : derived class –Base class typically represents larger set of objects than derived classes Example: –Base class: Vehicle Cars, trucks, boats, bicycles, … –Derived class: Car Smaller, more-specific subset of vehicles

8 9.2 Base Classes and Derived Classes Inheritance examples

9 9.2 Base Classes and Derived Classes Inheritance hierarchy –Inheritance relationships: tree-like hierarchy structure –Each class becomes Base class –Supply data/behaviors to other classes OR Derived class –Inherit data/behaviors from other classes

10 Single inheritance CommunityMember EmployeeStudent AdministratorTeacher AdministratorTeacher StaffFaculty Alumnus Single inheritance Multiple inheritance Fig. 9.2Inheritance hierarchy for university CommunityMember s.

11 Shape TwoDimensionalShapeThreeDimensionalShape CircleSquareTriangleSphereCubeTetrahedron Fig. 9.3Inheritance hierarchy for Shapes.

Base Classes and Derived Classes public inheritance –Specify with: class TwoDimensionalShape : public Shape Class TwoDimensionalShape inherits from class Shape –Base class private members Not accessible directly Still inherited –Manipulate through inherited member functions –Base class public and protected members Inherited with original member access –friend functions Not inherited

protected Members protected access –Intermediate level of protection between public and private –Base class’s protected members accessible to Base class member functions Base class friend s Derived class member functions Derived class friend s –Derived-class members Refer to public and protected members of base class –Simply use member names

Relationship between Base Classes and Derived Classes Base class and derived class relationship –Example: Point/circle inheritance hierarchy Point –x-y coordinate pair Circle –x-y coordinate pair –Radius

15 1 // Fig. 9.4: point.h 6 class Point { 8 public: 9 Point( int = 0, int = 0 ); 11 void setX( int ); 12 int getX() const; 14 void setY( int ); 15 int getY() const; 17 void print() const; 19 private: 20 int x; 21 int y; 23 }; class Point +Point( int = 0, int = 0 ); +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; +void print() const; +Point( int = 0, int = 0 ); +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; +void print() const; -int x; -int y; -int x; -int y; 1 // Fig. 9.5: point.cpp 10 Point::Point( int xValue, int yValue ) { 12 x = xValue; 13 y = yValue; 15 }

16 18 void Point::setX( int xValue ) { 20 x = xValue; 22 } 25 int Point::getX() const { 27 return x; 29 } void Point::setY( int yValue ) { 34 y = yValue; 36 } int Point::getY() const { 41 return y; 43 } 46 void Point::print() const { 48 cout << '[' << x << ", " << y << ']'; 50 } class Point +Point( int = 0, int = 0 ); +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; +void print() const; +Point( int = 0, int = 0 ); +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; +void print() const; -int x; -int y; -int x; -int y;

17 1 // Fig. 9.6: pointtest.cpp 10 int main() { 12 Point point( 72, 115 ); cout << "X coordinate is " << point.getX() 16 << "\nY coordinate is " << point.getY(); point.setX( 10 ); 19 point.setY( 10 ); cout << "\n\nThe new location of point is "; 23 point.print(); 24 cout << endl; return 0; 28 } X coordinate is 72 Y coordinate is 115 The new location of point is [10, 10] point x=72 y=115 point x=10 y=10 x=10 y=10

18 1 // Fig. 9.7: circle.h 6 class Circle { 8 public: 11 Circle(int= 0,int= 0,double= 0.0 ); 13 void setX( int ); 14 int getX() const; 16 void setY( int ); 17 int getY() const; 19 void setRadius( double ); 20 double getRadius() const; 22 double getDiameter() const; 23 double getCircumference() const; 24 double getArea() const; 26 void print() const; 28 private: 29 int x; 30 int y; 31 double radius; 33 }; Circle +Circle( int = 0, int = 0, double = 0.0 ); +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; +void setRadius( double ); +double getRadius() const; +double getDiameter() const; +double getCircumference() +const; +double getArea() const; +void print() const; +Circle( int = 0, int = 0, double = 0.0 ); +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; +void setRadius( double ); +double getRadius() const; +double getDiameter() const; +double getCircumference() +const; +double getArea() const; +void print() const; -int x; -int y; -double radius; -int x; -int y; -double radius; Creating a Circle Class Without Using Inheritance

19 10 Circle::Circle( int xValue, int yValue, double radiusValue ) { 12 x = xValue; 13 y = yValue; 14 setRadius( radiusValue ); 16 } …… 47 void Circle::setRadius( double radiusValue ){ 49 radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); 51 } 54 double Circle::getRadius() const { 56 return radius; 58 } 61 double Circle::getDiameter() const { 63 return 2 * radius; 65 } 68 double Circle::getCircumference() const { 70 return * getDiameter(); 72 } double Circle::getArea() const { 77 return * radius * radius; 79 } void Circle::print() const { 84 cout << "Center = [" << x << ", " << y << ']' 85 << "; Radius = " << radius; 87 }

20 1 // Fig. 9.9: circletest.cpp 15 int main() { 17 Circle circle( 37, 43, 2.5 ); 20 cout << "X coordinate is " << circle.getX() 21 << "\nY coordinate is " << circle.getY() 22 << "\nRadius is " << circle.getRadius(); 24 circle.setX( 2 ); 25 circle.setY( 2 ); 26 circle.setRadius( 4.25 ); 29 cout << "\n\nThe new location and radius of circle are\n"; 30 circle.print(); 33 cout << fixed << setprecision( 2 ); 36 cout << "\nDiameter is " << circle.getDiameter(); 39 cout << "\nCircumference is " << circle.getCircumference(); 42 cout << "\nArea is " << circle.getArea(); 44 cout << endl; 46 return 0; 48 } circle x=37 y=43 radius=2.5 x=37 y=43 radius=2.5 circle x=2 y=2 radius=4.25 x=2 y=2 radius=4.25

21 X coordinate is 37 Y coordinate is 43 Radius is 2.5 The new location and radius of circle are Center = [2, 2]; Radius = 4.25 Diameter is 8.50 Circumference is Area is 56.74

22 1 // Fig. 9.10: circle2.h 6 #include "point.h" 8 class Circle2 : public Point { 10 public: 13 Circle2( int = 0, int = 0, double = 0.0 ); 15 void setRadius( double ); 16 double getRadius() const; 18 double getDiameter() const; 19 double getCircumference() const; 20 double getArea() const; 22 void print() const; 24 private: 25 double radius; 27 }; 29 #endif 1 // Fig. 9.11: circle2.cpp 7 #include "circle2.h" // Circle2 class definition 10 Circle2::Circle2( int xValue, int yValue, double radiusValue ) { 12 x = xValue; 13 y = yValue; 14 setRadius( radiusValue ); 16 } Point/Circle Hierarchy Using Inheritance Circle2 class Point +Point( int = 0, int = 0 ); +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; +void print() const; +Point( int = 0, int = 0 ); +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; +void print() const; -int x; -int y; -int x; -int y;

23 1 // Fig. 9.12: point2.h 6 class Point2 { 8 public: 9 Point2( int = 0, int = 0 ); 11 void setX( int ); 12 int getX() const; 14 void setY( int ); 15 int getY() const; 17 void print() const; 19 protected: 20 int x; 21 int y; 23 }; class Point2 +Point2( int = 0, int = 0 ); +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; +void print() const; int x; int y; 1 // Fig. 9.14: circle3.h 6 #include "point2.h" 8 class Circle3 : public Point2 { 10 public: 13 Circle3(int=0,int=0,double=0.0); 15 void setRadius( double ); 16 double getRadius() const; 18 double getDiameter() const; 19 double getCircumference()const; 20 double getArea() const; 22 void print() const; 24 private: 25 double radius; 27 }; Point/Circle Hierarchy Using Protected Data class Circle3 : public Point2 +Circle3( int = 0,int = 0,double = 0.0 ); +void setRadius( double ); +double getRadius() const; +double getDiameter() const; +double getCircumference() +const; +double getArea() const; +void print() const; -double radius;

24 1 // Fig. 9.15: circle3.cpp 10 Circle3::Circle3( int xValue, int yValue, double radiusValue ) { 12 x = xValue; 13 y = yValue; 14 setRadius( radiusValue ); 16 } 19 void Circle3::setRadius( double radiusValue ) { 21 radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); 23 } 26 double Circle3::getRadius() const { 28 return radius; 30 } 33 double Circle3::getDiameter() const{ 35 return 2 * radius; 37 } 40 double Circle3::getCircumference() const { 42 return * getDiameter(); 44 } 47 double Circle3::getArea() const { 49 return * radius * radius; 51 } 54 void Circle3::print() const{ 56 cout << "Center = [" << x << ", " << y << ']' 57 << "; Radius = " << radius; 59 } class Circle3 : public Point2 + Circle3( int = 0,int = 0,double = 0.0 ); +void setRadius( double ); +double getRadius() const; +double getDiameter() const; +double getCircumference() +const; +double getArea() const; +void print() const; -double radius; +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; int x; int y;

25 15 int main() { 17 Circle3 circle( 37, 43, 2.5 ); 20 cout << "X coordinate is " << circle.getX() 21 << "\nY coordinate is " << circle.getY() 22 << "\nRadius is " << circle.getRadius(); 24 circle.setX( 2 ); 25 circle.setY( 2 ); 26 circle.setRadius( 4.25 ); 29 cout << "\n\nThe new location and radius of circle are\n"; 30 circle.print(); 33 cout << fixed << setprecision( 2 ); 36 cout << "\nDiameter is " << circle.getDiameter(); 39 cout << "\nCircumference is " << circle.getCircumference(); 42 cout << "\nArea is " << circle.getArea(); 44 cout << endl; 46 return 0; 48 } circle radius=2.5 x=37 y=43 circle radius=4.25 x=2 y=2

26 X coordinate is 37 Y coordinate is 43 Radius is 2.5 The new location and radius of circle are Center = [2, 2]; Radius = 4.25 Diameter is 8.50 Circumference is Area is 56.74

Relationship between Base Classes and Derived Classes Using protected data members –Advantages Derived classes can modify values directly Slight increase in performance –Avoid set/get function call overhead –Disadvantages No validity checking –Derived class can assign illegal value Implementation dependent –Derived class member functions more likely dependent on base class implementation –Base class implementation changes may result in derived class modifications Fragile (brittle) software

28 1 // Fig. 9.17: point3.h 6 class Point3 { 8 public: 9 Point3( int = 0, int = 0 ); 11 void setX( int ); 12 int getX() const; 14 void setY( int ); 15 int getY() const; 17 void print() const; 19 private: 20 int x; 21 int y; 23 }; Point/Circle Hierarchy Using Private Data

29 1 // Fig. 9.19: circle4.h 6 #include "point3.h" 8 class Circle4 : public Point3 { 10 public: 13 Circle4( int = 0, int = 0, double = 0.0 ); 15 void setRadius( double ); 16 double getRadius() const; 18 double getDiameter() const; 19 double getCircumference() const; 20 double getArea() const; 22 void print() const; 24 private: 25 double radius; 27 }; 1 // Fig. 9.20: circle4.cpp 10 Circle4::Circle4( int xValue, int yValue, double radiusValue ) 11 : Point3( xValue, yValue ) { //base-class initializer 13 setRadius( radiusValue ); 15 } 18 void Circle4::setRadius( double radiusValue ) { 20 radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); 22 } class Circle4 : public Point3 + Circle4( int = 0,int = 0,double = 0.0 ); +void setRadius( double ); +double getRadius() const; +double getDiameter() const; +double getCircumference() +const; +double getArea() const; +void print() const; -double radius; +void setX( int ); +int getX() const; +void setY( int ); +int getY() const; - int x; - int y;

30 25 double Circle4::getRadius() const { 27 return radius; 29 } 32 double Circle4::getDiameter() const { 34 return 2 * getRadius(); 36 } 39 double Circle4::getCircumference() const { 41 return * getDiameter(); 43 } 46 double Circle4::getArea() const { 48 return * getRadius() * getRadius(); 50 } 53 void Circle4::print() const { 55 cout << "Center = "; 56 Point3::print(); // invoke Point3's print function 57 cout << "; Radius = " << getRadius(); 59 }

31 1 // Fig. 9.21: circletest4.cpp 15 int main() { 17 Circle4 circle( 37, 43, 2.5 ); 20 cout << "X coordinate is " << circle.getX() 21 << "\nY coordinate is " << circle.getY() 22 << "\nRadius is " << circle.getRadius(); 24 circle.setX( 2 ); 25 circle.setY( 2 ); 26 circle.setRadius( 4.25 ); 29 cout << "\n\nThe new location and radius of circle are\n"; 30 circle.print(); 33 cout << fixed << setprecision( 2 ); 36 cout << "\nDiameter is " << circle.getDiameter(); 39 cout << "\nCircumference is " << circle.getCircumference(); 42 cout << "\nArea is " << circle.getArea(); 44 cout << endl; 46 return 0; 48 } circle radius=2.5 x=37 y=43 circle radius=4.25 x=2 y=2

32 X coordinate is 37 Y coordinate is 43 Radius is 2.5 The new location and radius of circle are Center = [2, 2]; Radius = 4.25 Diameter is 8.50 Circumference is Area is 56.74

Case Study: Three-Level Inheritance Hierarchy Three level point/circle/cylinder hierarchy –Point x-y coordinate pair –Circle x-y coordinate pair Radius –Cylinder x-y coordinate pair Radius Height

34 1 // Fig. 9.22: cylinder.h 6 #include "circle4.h" 8 class Cylinder : public Circle4 { 10 public: 13 Cylinder( int = 0, int = 0, double = 0.0, double = 0.0 ); 15 void setHeight( double ); 16 double getHeight() const; 18 double getArea() const; 19 double getVolume() const; 20 void print() const; 22 private: 23 double height; 25 }; 1 // Fig. 9.23: cylinder.cpp 10 Cylinder::Cylinder( int xValue, int yValue, double radiusValue, 11 double heightValue ) 12 : Circle4( xValue, yValue, radiusValue ) { 14 setHeight( heightValue ); 16 } Circle4 Point3 Cylinder class Cylinder : public Circle4 -double height; - int x; - int y; -double radius;

35 19 void Cylinder::setHeight( double heightValue ) { 21 height = ( heightValue < 0.0 ? 0.0 : heightValue ); 23 } double Cylinder::getHeight() const { 28 return height; 30 } double Cylinder::getArea() const { 35 return 2 * Circle4::getArea() + getCircumference() * getHeight(); 38 } double Cylinder::getVolume() const { 43 return Circle4::getArea() * getHeight(); 45 } void Cylinder::print() const { 50 Circle4::print(); 51 cout << "; Height = " << getHeight(); 53 }

36 1 // Fig. 9.24: cylindertest.cpp 15 int main() { 18 Cylinder cylinder( 12, 23, 2.5, 5.7 ); 21 cout << "X coordinate is " << cylinder.getX() 22 << "\nY coordinate is " << cylinder.getY() 23 << "\nRadius is " << cylinder.getRadius() 24 << "\nHeight is " << cylinder.getHeight(); 26 cylinder.setX( 2 ); 27 cylinder.setY( 2 ); 28 cylinder.setRadius( 4.25 ); 29 cylinder.setHeight( 10 ); 32 cout << "\n\nThe new location and radius of circle are\n"; 33 cylinder.print(); 36 cout << fixed << setprecision( 2 ); 39 cout << "\n\nDiameter is " << cylinder.getDiameter(); 42 cout << "\nCircumference is " 43 << cylinder.getCircumference(); 46 cout << "\nArea is " << cylinder.getArea(); 49 cout << "\nVolume is " << cylinder.getVolume(); 51 cout << endl; 53 return 0; 55 } circle height=5.7 x=12 y=23 radius=2.5 circle height=10 x=2 y=2 radius=4.25

37 X coordinate is 12 Y coordinate is 23 Radius is 2.5 Height is 5.7 The new location and radius of circle are Center = [2, 2]; Radius = 4.25; Height = 10 Diameter is 8.50 Circumference is Area is Volume is

Constructors and Destructors in Derived Classes Instantiating derived-class object –Chain of constructor calls Derived-class constructor invokes base class constructor –Implicitly or explicitly Base of inheritance hierarchy –Last constructor called in chain –First constructor body to finish executing –Example: Point3 / Circle4 / Cylinder hierarchy Point3 constructor called last Point3 constructor body finishes execution first Circle4 constructor Circle4 constructor Point3 constructor Point3 constructor Cylinder constructor Cylinder constructor call finish

Constructors and Destructors in Derived Classes Destroying derived-class object –Chain of destructor calls Reverse order of constructor chain Destructor of derived-class called first Destructor of next base class up hierarchy next –Continue up hierarchy until final base reached After final base-class destructor, object removed from memory Circle4 Point3 Cylinder Circle4 destructor Circle4 destructor Point3 destructor Point3 destructor Cylinder destructor Cylinder destructor Call then destroyed

Constructors and Destructors in Derived Classes Base-class constructors, destructors, assignment operators –Not inherited by derived classes –Derived class constructors, assignment operators can call Constructors Assignment operators

41 1 // Fig. 9.25: point4.h 6 class Point4 { 8 public: 9 Point4( int = 0, int = 0 ); 10 ~Point4(); 12 void setX( int ); 13 int getX() const; 15 void setY( int ); 16 int getY() const; 18 void print() const; 20 private: 21 int x; 22 int y; 24 }; 1 // Fig. 9.26: point4.cpp 11 Point4::Point4( int xValue, int yValue ):x( xValue), y( yValue) { 14 cout << "Point4 constructor: "; 15 print(); 16 cout << endl; 18 } 21 Point4::~Point4() { 23 cout << "Point4 destructor: "; 24 print(); 25 cout << endl; 27 } ……

42 1 // Fig. 9.27: circle5.h 8 class Circle5 : public Point4 { 10 public: 13 Circle5( int = 0, int = 0, double = 0.0 ); 15 ~Circle5(); 16 void setRadius( double ); 17 double getRadius() const; 19 double getDiameter() const; 20 double getCircumference() const; 21 double getArea() const; 23 void print() const; 25 private: 26 double radius; 28 }; class Circle5 : public Point4 Circle5(int=0,int=0,double=0.0); Point4 Point4( int = 0, int = 0 );

43 1 // Fig. 9.28: circle5.cpp 11 Circle5::Circle5( int xValue, int yValue, double radiusValue ) 12 : Point4( xValue, yValue ) { 14 setRadius( radiusValue ); 16 cout << "Circle5 constructor: "; 17 print(); 18 cout << endl; 20 } 23 Circle5::~Circle5() { 25 cout << "Circle5 destructor: "; 26 print(); 27 cout << endl; 29 } 32 void Circle5::setRadius( double radiusValue ) { 34 radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); 36 } double Circle5::getRadius() const { 41 return radius; 43 } …… class Circle5 : public Point4 Circle5(int=0,int=0,double=0.0); Point4 Point4( int = 0, int = 0 );

44 1 // Fig. 9.29: fig09_29.cpp 11 int main() { 13 { // begin new scope 15 Point4 point( 11, 22 ); 17 } // end scope 19 cout << endl; 20 Circle5 circle1( 72, 29, 4.5 ); 22 cout << endl; 23 Circle5 circle2( 5, 5, 10 ); 25 cout << endl; 27 return 0; 29 } Point4 constructor: [11, 22] Point4 destructor: [11, 22] Point4 constructor: [72, 29] Circle5 constructor: Center = [72, 29]; Radius = 4.5 Point4 constructor: [5, 5] Circle5 constructor: Center = [5, 5]; Radius = 10 Circle5 destructor: Center = [5, 5]; Radius = 10 Point4 destructor: [5, 5] Circle5 destructor: Center = [72, 29]; Radius = 4.5 Point4 destructor: [72, 29]

public, protected and private Inheritance

public, protected and private Inheritance public BaseClass public member protected member private member protected BaseClass public member protected member private member private BaseClass public member protected member private member DerivedClass public protected Inheritable, not accessible DerivedClass protected Inheritable, not accessible DerivedClass private Inheritable, not accessible inheritance

Software Engineering with Inheritance Customizing existing software –Inherit from existing classes Include additional members Redefine base-class members No direct access to base class’s source code –Link to object code –Independent software vendors (ISVs) Develop proprietary code for sale/license –Available in object-code format Users derive new classes –Without accessing ISV proprietary source code

48 Summary Circle2 Point Circle5 Point4 Circle4 Point3 Cylinder Circle3 Point2 Point Circle private data protected data private data

49 Reference C++ 教學範本 Ivor Horton 蔡明志譯 碁峰 \

50 Exercises a) Array, b) Complex, c) String

51

52

53

54

55