87
1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently M. Deek.

1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

1

Inheritance

Lesson #5

Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek.

Page 2: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

2

Correction (to Point Class)

class Point { public:…

int getX() const {return x;};int getY(){return y;};

void setX(int valX) {x=valX;}; // set x…}

Page 3: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

3

Correction

const Point P1(10, 20);//P43

Point P2(30, 50);

P1.getX();

P2.getX();//a const function can be //called by a non-const object!!!

P1.SetX(100); //error! P1 is a const obj

P2.SetX(100); //ok

Page 4: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

4

An Example: FString #include <iostream.h> #include <iomanip.h> #include <string.h> class FString { public: FString(); FString( const char * s ); // Construct from a C-style string. FString( const FString & s ); // Construct from another FString. FString & Append( const FString & s ); // Append another FString to current object. FString & Assign( const char * s ); // Assign a C-style string to current object.

Page 5: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

5

Fstring.h

FString & Assign( const FString & s ); // Assign an FString to current object. const char * CString() const; // Convert current object to a C-style string. int Compare( const FString & s ) const; // Implement the standard strcmp() function. // Case-sensitive. int IsLess( const FString & s ) const; // Return 1 if current object is less than s. int IsGreater( const FString & s ) const; // Return 1 if current object is greater than s.

Page 6: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

6

FString

int IsEqual( const FString & s ) const; // Return 1 if current object is equal to s. FString & GetLine( istream & inp ); // Get a line of input from a stream. friend istream & operator >>( istream & inp, FString & s ); friend ostream & operator <<( ostream & inp, const FString & s ); enum { MaxSize = 256 }; // Maximum allowable string size private: char str[MaxSize+1]; // String characters };

Page 7: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

7

Fstring.cpp #include "fstring.h" FString::FString() { str[0] = '\0'; } FString::FString( const char * S ) { strncpy( str, S, MaxSize ); str[MaxSize] = '\0'; } FString::FString( const FString & S ) { strcpy( str, S.CString() ); } FString & FString::Append( const FString & S ) { strncat( str, S.CString(), MaxSize ); return *this; }

Page 8: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

8

Fstring.cpp FString & FString::Assign( const char * S ) { strncpy( str, S, MaxSize ); return *this; } FString & FString::Assign( const FString & S2 ) { strncpy( str, S2.str, MaxSize ); return *this; } const char * FString::CString() const { return str; } int FString::Compare( const FString & S2 ) const { return strcmp( str, S2.str ); }

Page 9: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

9

Fstring.cpp int FString::IsLess( const FString & S2 ) const { if (strcmp( str, S2.str ) < 0) return 1; else return 0; } int FString::IsGreater( const FString & S2 ) const // Return 1 if current object is greater than s. { if (strcmp( str, S2.str ) > 0) return 1; else return 0; } int FString::IsEqual( const FString & S2 ) const // Return 1 if current object is equal to s. { if (strcmp( str, S2.str ) == 0) return 1; else return 0; }

Page 10: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

10

Fstring.cpp FString & FString::GetLine( istream & inp ) { inp.getline( str, MaxSize+1 ); return *this; } istream & operator >>( istream & inp, FString & S ) { inp >> setw(S.MaxSize+1) >> S.str; return inp; } ostream & operator <<( ostream & os, const FString & S ) { os << S.str; return os; }

Page 11: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

11

Fstrtst.cpp

#include "fstring.h" class Employee { public: Employee( const char *, const char * ); friend ostream & operator <<( ostream &, const

Employee & ); private: FString LastName; FString FirstName; };

Page 12: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

12

Fstrtst.cpp

Employee::Employee( const char * lname, const char * fname )

:LastName(lname), FirstName(fname) { } ostream & operator <<( ostream & os, const

Employee & E ) { os << E.FirstName << ' ' << E.LastName; return os; }

Page 13: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

13

Fstrtst.cpp int main() {FString name1; FString name2; name1.Assign( "Fred" ); name2.Assign( name1 ); name1.Assign( "Fred " ); name2.Assign( "Smith" ); name1.Append( name2 ); // "Fred Smith" int n = name1.Compare( name2 ); if( n < 0 ) cout << "The first name is less\n"; else if( n == 0 ) cout << "The names are equal\n"; else cout << "The second name is less\n"; if( name1.IsLess( name2 )) cout << "The first name is less\n";

Page 14: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

14

Fstrtst.cpp cout << "Enter two names:\n"; name1.GetLine( cin ); name2.GetLine( cin ); n = name1.Compare( name2 ); if( n < 0 ) cout << "The first name is less\n"; else if( n == 0 ) cout << "The names are equal\n"; else cout << "The second name is less\n"; if( name1.IsLess( name2 )) cout << "The first name is less\n"; const char * vp = name1.CString(); cout << vp << endl; Employee E( "Johnson", "Harvey" ); cout << E << endl; return 0; }//cis601/cis601source/chap06/Fstring/fstrtst.dsw

Page 15: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

15

Page 16: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

16

Page 17: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

17

Contents

1. Classification2. Sharing3. Inheritance4. Subclass5. The Categories of Inheritance6. Abstract Class

Page 18: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

18

Classification An object can be of a certain class but not its

creator. An apple is of Fruit, but is not the instance of Fruit.

In this view, classes are like sets. John is a man. Mary is a woman. Spot is a dog. All men are people. All women are people. All people are mammals. All dogs are mammals.

Page 19: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

19

Classification/Inheritance

Mammal

People

Bird

Dog

Animal

man woman

John Mary

. . . . .

Classification

Inheritance

Page 20: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

20

Classification

Classification arises from the universal need to describe uniformities of collections of instances.

Classification is a basic idea for understanding the concept inheritance

Page 21: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

21

Classification/Inheritance Commonality

The base class captures the common information (attributes) and features (operations) of the derived classes.

Page 22: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

22

Classification/Inheritance Customization

An existing class is used to create a customized version of the class.

Common Design Interface A base class may define the design

requirements for its derived classes by specifying a set of member functions that are required to be provided by each of its derived classes.

Page 23: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

23

Sharing Sharing is important in object-orientation. Inheritance is a technique that promotes

sharing. Inheritance means that new classes can

be derived from existing classes . A subclass inherits the attributes and the

operations of a superclass (base class) but a subclass (derived class) can define additional operations and attributes.

Page 24: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

24

Sharing Sharing can be seen as a specialization

mechanism. Instances of a subclass are specializations

(additional state and behavior) of the instances of a superclass.

Sharing can also be seen as generalization mechanism. Instances of a superclass generalizes

(restricts) the instances of a subclasses.

Page 25: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

25

Sharing

Sharing is also the basic idea of inheritance. The ability of one class to share the

behavior of another class without explicit redefinition.

An approach which allows classes to be created based on a old class.

Page 26: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

26

Three dimensions of sharing

Static or dynamic sharing: At times, sharing patterns have to be fixed. This can be

done at object creation (static) or when an object receives a message (dynamic).

Implicit or explicit sharing: The programmer directs the patterns of sharing (explicit)

or the system does it automatically (implicit).

Per object or per group sharing: Behaviors can be specified for an entire group of objects

or can be attached to a single object.

Page 27: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

27

Inheritance Purpose

1. Reusing existing design reduces software development and testing costs.

2. A new class inherits the data members and member functions of an existing class.

Page 28: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

28

What is Inheritance?

1. Inheritance is a mechanism for expressing similarity.

2. Inheritance is a natural property of classification.

3. Inheritance is a mechanism that allows a class A to inherit properties of a class B.

Page 29: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

29

What is Inheritance?

Assume ''A inherits from B''. Then, objects of class A have access to attributes and methods of class B without the need to redefine them.

Page 30: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

30

Superclass/Subclass If class A inherits from class B, then B

is called superclass of A. A is called subclass of B. Objects of a subclass can be used where objects of the corresponding superclass are expected. This is due to the fact that objects of the subclass share the same behavior as objects of the superclass.

Page 31: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

31

Person

Administrator Faculty Student

superclass

subclasses

Example:

Page 32: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

32

Subclasses

Subclasses refer to not only inheritance of specifications but also to inheritance of implementation and so can be viewed as reusability mechanism.

Page 33: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

33

Important aspects of subclasses

Modifiability The degree of modifiability determines

how attributes and methods inherited from a superclass can be modified in a subclass. In this context, an approach of distinguishing modifiability of the objects state (attributes) and the objects behavior (operations) are used.

Page 34: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

34

Attributes

1. No redefinition: modification is not allowed.

2. Arbitrary redefinition: redefinition without constrained is allowed.

3. Constrained redefinition: the domain of attributes is constrained.

4. Hidden redefinition: definitions of attributes are hidden in subclass to avoid conflicts.

Page 35: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

35

For operations

1. Arbitrary redefinition: all changes to operations are allowed.

2. Constrained redefinition: Parts of the signature of methods in

subclasses have to be subtypes of the parts of the methods of the superclass.

This is important in overriding and overloading of methods.

Page 36: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

36

Naming conflicts

Conflicts between a superclass and a subclass. when attributes or methods defined in a

subclass have the same name as attributes or methods defined in the superclass.

This is resolved by overriding.

Page 37: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

37

The categories of Inheritance

Whole/Partial Inheritance: Whole Inheritance is when a class inherits all

the properties and operations from its superclass.

Partial Inheritance is when only some properties are inherited while others are suppressed.

Page 38: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

38

The Categories of Inheritance

Default inheritance: inherited properties and constraints can be modified.

Strict inheritance: doesn't allow the user to modify inherited properties or constraints.

Page 39: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

39

The categories of Inheritance

Single (simple) inheritance: A class can inherit from only one

superclass. This means the inheritance hierarchy forms a tree.

Multiple inheritance: A class can have more than one

superclass.

Page 40: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

40

What is Multiple Inheritance?

Multiple inheritance is when a class inherits from more than one class (e.g. A inherits from B1, B2, ..., Bn).

Naming conflicts can be introduced if at least two superclasses define properties with the same name.

Page 41: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

41

Naming Conflicts

If attributes or methods defined in one superclass have the same name as attributes or methods defined in another superclass.

Page 42: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

42

B1{//… int x;//…}

B2{//… int x; public: int getValue();}

B3{//… int x;public: int getValue();}

A{}

Multiple Inheritance and Naming conflicts

Page 43: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

43

Conflict Resolution

1. Using the order of the superclass If there are more attributes or methods with

the same name in different superclasses, the ones occurring in the first class in the list of superclasses are inherited (done by the compiler).

If the order is from left to right, then x means x of B1, and getValue() means the getValue() of B2.

Page 44: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

44

Conflict Resolution

2. Determined by the user The user can inherit conflicting

attributes or methods, but has to explicitly rename conflicting attributes or methods in the subclass (done by the user)

B1::x; or B2::x; B2::getValue(); or B3::getValue();

Page 45: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

45

Precedence conflicts We have class A with attribute a, class B which

is a subclass of A and redefines attribute a, class C which is also a subclass of A, and class D which is a subclass of B and C (multiple inheritance).

Class D can inherit the redefined version of a from class B or the original version from class C.

Which version of attribute a should be inherited?

Page 46: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

46

Precedence conflicts

class A{a;}

class B: public A{}

class C: public A{a;//redefined}

class C: public B, C{?}

Page 47: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

47

Conflict Resolution

The same as with naming conflicts: Use the order of the superclass set by

the compiler. Use the explicit class name given by

the user.

Page 48: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

48

Abstract class

A class without any instance. Mammal is an abstract class: only for

classification and inheritance. Never make objects of the class.

Page 49: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

49

Abstract class

It is also possible to have a classification which does not any have code behind it: Resizable class is a class of objects that

may be resized.

Page 50: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

50

What is Abstract Class

A class A is called abstract class if it is only used as a superclass for other classes. Class A only specifies properties. It is not used to create objects. Derived classes must define the properties of A.

Page 51: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

51

Abstract Classabstract class DrawableObject { attributes: methods:

print() }

class Point inherits from DrawableObject { attributes:

int x, y;

methods: setX(int newX) ;getX();setY(int newY);getY();print(); /* Redefined for Point */

}

Page 52: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

52

A Formula for Subclasses

Subclass ::=<Id, SupeId, Ds, Ops, Intfc>, where Id is the identification or name of the subclass; SuperId is the identification or name of its

superclassor a set of ids of its superclases. Ds is the new space description (template) for

memory of the subclass; Op is the new set of operations’

implementation of the subclass; Int is the new interface of the subclass.

Page 53: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

53

Reuse

Many take inheritance as a reusing tool.

In this case, we can take inheritance as a code reusing tool.

For example: There are location class, point class and

a line class.

Page 54: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

54

ReuseClass Loc{

int x, y;

public:

int getX();

int getY();

void setX(int);

void setY(int)

};

Class point inherited from Loc

{int color;

int getColor();

void setColor(int);}

Class line inherited from Point

{int endX, endY;

float weight;

float getWeight();

void setWeight(int);

}

Page 55: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

55

Reuse Is that all right? Location, point and line belong to three

different categories of things. They should not be considered as

having superclass/subclass relation.

Page 56: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

56

Inheritance and Classification

Classification implies inheritance (whole and simple inheritance).

Inheritance may effect clarity of classification (for multiple inheritance and for part inheritance)

Page 57: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

57

Example

Is a class of ostrich a subclass of class bird? (an ostrich can not fly).

In terms of classification: No, if there is a Ds for “can fly”. Yes, if there was no Ds for “can fly”.

In terms of inheritance: Yes for both cases, because an ostrich

inherits partly from bird.

Page 58: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

58

Two views for a plane class Head{dsh, oph}; class Body{dsb, opb}; class Wing{dsw, opw}; class Tail{dst, opt}; class Plane inherits

from Head, Body, Wing, Tail

{ dsp, opp};

class Head{dsh, oph};

class Body{dsb, opb};

class Wing{dsw, opw};

class Tail{dst, opt};

class Plane

{Head h;

Body b;

Wing w;

Tail t;

dsp;

opp;

};

Page 59: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

59

Declaring Derived Classes

Class class_name: access_specifieropt or base_class {

Member_list

} access_specifier ::= public|protected|

private(default)

Page 60: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

60

A simple Example #include <iostream.h> class Door { public: Door(); bool isOpen() const; void open(); void close(); protected: bool shut; };

Page 61: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

61

Ex5door.cpp Door::Door() : shut(true) {}; bool Door:: isOpen() const { return !shut;}; void Door:: open() { shut= false;}; void Door:: close() { shut = true;};

Page 62: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

62

Ex5door.cpp class LockableDoor : public Door { public: LockableDoor(); bool isLocked() const; void open(); void lock(); void unlock(); protected: bool locked; };

Page 63: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

63

Ex5door.cpp LockableDoor::LockableDoor():Door(),

locked(true) {}; bool LockableDoor::isLocked() const { return locked;}; void LockableDoor::open() { if (!locked) Door::open();}; void LockableDoor::lock() { locked = true;}; void LockableDoor::unlock() { locked = false;};

Page 64: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

64

Ex5door.cpp

main() { Door generic; LockableDoor bathDoor; generic.open(); bathDoor.lock(); bathDoor.open();

Page 65: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

65

Ex5door.cpp if (!bathDoor.isOpen()) bathDoor.unlock(); if (generic.isOpen()) cout << "generic is open!"<< "\n"; else cout << "generic is closed!"<< "\n"; if (bathDoor.isOpen()) cout << "bathDoor is open!"<< "\n"; else cout << "bathDoor is closed!"<< "\n"; }

Page 66: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

66

Page 67: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

67

Page 68: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

68

Page 69: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

69

A complex Example

Test if the specified file exists If yes, find the lines containing “#include”

and output the lines. Else, do nothing.

Page 70: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

70

Example ----The files

Testsrc.cpp (main())(File Structure) Fexcept.cpp File.cpp Srcfile.cpp Txtfile.cpp

Fexcept.h

File.h

Srcfile.h

Txtfile.h

Page 71: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

71

Example The class structure

File

Text_File

Source_File

File_Exception

Page 72: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

72

Page 73: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

73

Page 74: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

74

Page 75: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

75

Page 76: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

76

Page 77: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

77

Page 78: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

78

Page 79: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

79

Page 80: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

80

Page 81: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

81

Page 82: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

82

Page 83: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

83

Page 84: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

84

Page 85: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

85

Page 86: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

86

Page 87: 1 Inheritance Lesson #5 Note: CIS 601 notes were originally developed by H. Zhu for NJIT DL Program. The notes were subsequently revised by M. Deek

87

Readings

Readings Chapter 6 Sections 6.1.1 - 6.1.2