1 object-oriented programming -- using c++ andres, wen-yuan liao department of computer science and...

55
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology [email protected] http://cse.dlit.edu.tw/~andres

Upload: shanna-lynch

Post on 18-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

1

Object-Oriented Programming-- Using C++

Andres, Wen-Yuan Liao

Department of Computer Science and EngineeringDe Lin Institute of Technology

[email protected]://cse.dlit.edu.tw/~andres

Page 2: 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

Outline9.1 Introduction9.2 Base Classes and Derived Classes9.3 protected Members9.4 Relationship between Base Classes and Derived Classes9.5 Case Study: Three-Level Inheritance Hierarchy9.6 Constructors and Destructors in Derived Classes9.8 public, protected and private Inheritance9.9 Software Engineering with Inheritance

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

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

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

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

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

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

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

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

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

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

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

8

9.2 Base Classes and Derived Classes

• Inheritance examples

Base class Derived classes

Student GraduateStudent UndergraduateStudent

Shape Circle Triangle Rectangle

Loan CarLoan HomeImprovementLoan MortgageLoan

Employee FacultyMember StaffMember

Account CheckingAccount SavingsAccount

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

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

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

10

Single inheritance

CommunityMember

Employee Student

Administrator Teacher

AdministratorTeacher

StaffFaculty

Alumnus

Single inheritance

Single inheritance

Multiple inheritance

Fig. 9.2 Inheritance hierarchy for university CommunityMembers.

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

11

Shape

TwoDimensionalShape ThreeDimensionalShape

Circle Square Triangle Sphere Cube Tetrahedron

Fig. 9.3 Inheritance hierarchy for Shapes.

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

12

9.2 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

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

13

9.3 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 friends• Derived class member functions• Derived class friends

– Derived-class members• Refer to public and protected members of base class

– Simply use member names

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

14

9.4 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

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

15

1 // Fig. 9.4: point.h6 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;-int x;

-int y;

1 // Fig. 9.5: point.cpp10 Point::Point( int xValue, int yValue ) {12 x = xValue;13 y = yValue;15 }

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

16

18 void Point::setX( int xValue ) {20 x = xValue; 22 }

25 int Point::getX() const {27 return x;29 }30 32 void Point::setY( int yValue ) {34 y = yValue; 36 }37 39 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;-int x;

-int y;

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

17

1 // Fig. 9.6: pointtest.cpp10 int main() {12 Point point( 72, 115 ); 13 15 cout << "X coordinate is " << point.getX() 16 << "\nY coordinate is " << point.getY();17 18 point.setX( 10 ); 19 point.setY( 10 ); 20 22 cout << "\n\nThe new location of point is ";23 point.print();24 cout << endl;25 26 return 0; 28 }

X coordinate is 72Y coordinate is 115 The new location of point is [10, 10]

point x=72

y=115

point x=10

y=10

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

18

1 // Fig. 9.7: circle.h6 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;-int x;

-int y;

-double radius;

Creating a Circle Class Without Using Inheritance

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

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 3.14159 * getDiameter();72 }73 75 double Circle::getArea() const {77 return 3.14159 * radius * radius;79 }80 82 void Circle::print() const {84 cout << "Center = [" << x << ", " << y << ']'85 << "; Radius = " << radius;87 }

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

20

1 // Fig. 9.9: circletest.cpp15 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

circlex=2

y=2

radius=4.25

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

21

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

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

22

1 // Fig. 9.10: circle2.h6 #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.cpp7 #include "circle2.h" // Circle2 class definition10 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;-int x;

-int y;

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

23

1 // Fig. 9.12: point2.h6 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.h6 #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;

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

24

1 // Fig. 9.15: circle3.cpp10 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 3.14159 * getDiameter();44 }47 double Circle3::getArea() const {49 return 3.14159 * 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;

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

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

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

26

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

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

27

9.4 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

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

28

1 // Fig. 9.17: point3.h6 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

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

29

1 // Fig. 9.19: circle4.h6 #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.cpp10 Circle4::Circle4( int xValue, int yValue, double radiusValue )11 : Point3( xValue, yValue ) { //base-class initializer13 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;

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

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 3.14159 * getDiameter();43 }

46 double Circle4::getArea() const {48 return 3.14159 * getRadius() * getRadius();50 }

53 void Circle4::print() const {55 cout << "Center = ";56 Point3::print(); // invoke Point3's print function57 cout << "; Radius = " << getRadius();59 }

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

31

1 // Fig. 9.21: circletest4.cpp15 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

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

32

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

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

33

9.5 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

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

34

1 // Fig. 9.22: cylinder.h6 #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.cpp10 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;

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

35

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

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

36

1 // Fig. 9.24: cylindertest.cpp15 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

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

37

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

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

38

9.6 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 firstCircle4

constructor

Point3

constructor

Cylinder

constructor

call

finish

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

39

9.6 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

Point3

destructor

Cylinder

destructor

Call then destroyed

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

40

9.6 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

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

41

1 // Fig. 9.25: point4.h6 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.cpp11 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 }……

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

42

1 // Fig. 9.27: circle5.h8 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 Point4Circle5(int=0,int=0,double=0.0);

Point4

Point4Point4( int = 0, int = 0 );

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

43

1 // Fig. 9.28: circle5.cpp11 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 }37 39 double Circle5::getRadius() const {41 return radius;43 }……

class Circle5 : public Point4Circle5(int=0,int=0,double=0.0);

Point4

Point4Point4( int = 0, int = 0 );

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

44

1 // Fig. 9.29: fig09_29.cpp11 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 = 10Point4 destructor: [5, 5]Circle5 destructor: Center = [72, 29]; Radius = 4.5Point4 destructor: [72, 29]

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

45

9.8 public, protected and private Inheritance

Type of inheritance Base class member access specifier

public inheritance

protected inheritance

private inheritance

Public

public in derived class. Can be accessed directly by any non-static member functions, friend functions and non-member functions.

protected in derived class. Can be accessed directly by all non-static member functions and friend functions.

private in derived class. Can be accessed directly by all non-static member functions and friend functions.

Protected

protected in derived class. Can be accessed directly by all non-static member functions and friend functions.

protected in derived class. Can be accessed directly by all non-static member functions and friend functions.

private in derived class. Can be accessed directly by all non-static member functions and friend functions.

Private

Hidden in derived class. Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class.

Hidden in derived class. Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class.

Hidden in derived class. Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class.

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

46

9.8 public, protected and private Inheritance

public BaseClasspublic member

protected member

private member

protected BaseClasspublic member

protected member

private member

private BaseClasspublic member

protected member

private member

DerivedClasspublic

protected

Inheritable, not accessible

DerivedClassprotected

protected

Inheritable, not accessible

DerivedClassprivate

private

Inheritable, not accessible

inheritance

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

47

9.9 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

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

48

Summary

Circle2

Point

Circle5

Point4

Circle4

Point3

Cylinder

Circle3

Point2Point

Circle

private data protected data

private data private data

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

49

Reference

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

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

50

Exercises

• 8.7• 8.8• 8.9 a) Array, b) Complex, c) String• 9.4• 9.7

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

51

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

52

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

53

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

54

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

55