Top Banner
Chapter 4 Inheritance Bernard Chen Spring 2006
27

Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Jan 11, 2016

Download

Documents

Arlene Webb
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Chapter 4Inheritance

Bernard ChenSpring 2006

Page 2: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Objective IS-A relationships and the allowable

changes for derived classes The concept of polymorphism Public, private, and protected members

and inheritance Static vs. dynamic binding Default constructor, copy constructor,

and copy assignment operator Abstract classes Tricky stuff

Page 3: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

4.1 What is Inheritance?

Another mechanism for code reuse. A process of deriving classes from

a base class w/o disturbing the implementation of the base class.

Ex: Vehicle is a class

Car is a Vehicle => Car derived

from Vehicle

Page 4: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Figure 4.2

IOS

istream

ostream

iostream

ifstream

istringstream

ostringstream

ofstream

fstream

stringstream

Page 5: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Polymorphism Variables can reference objects of several

type. When operation are applied to the variable, the operation appropriate to the referenced object is automatically selected.

Derived class is a new class which inherits all public & protected properties of a base class.

Derived class is type-compatible with its base class.

Page 6: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

4.2 Inheritance Basics

Public inheritance: all public members of the base class remain public in the derived class =>models is-a relationship =>mostly used.

Private inheritance: hidden all inherited members from the public => models has-a relationship.

Page 7: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

General layout of public inheritanceclass Derived : public Base{//any member are not listed are inherited//except constructor, destructor, copy, constructor and

operator=public:

//new constructor and destructor if necessary//modify base member function//new function

private://new data member or private function//base members should be disable

};

Page 8: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Inheritance cont. . . Derived class inherits all member

functions from the base class. It may accept, disallow, or redefine them.

Derived class can define new functions.

Derived class can access public and protected members of the base class.

Page 9: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Access rules

Page 10: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Constructor and Base class initialization

If there is no constructor in a derived class, a default zero-parameter constructor will be generated

Base class constructors can be explicitly called by name in the initializer list.

Derived() : Base() {}

Page 11: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Constructor for the new exception class Underflow

class Underflow : public underflow_error{

public:underflow(const string & msg =“ “): underflow_error(msg.c_str()) {}

};

Page 12: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Overriding a method(member function)

Overriding base class methods: Derived class methods must have the same signature and compatible return types with the base class methods.

Partial overriding: overriding base class methods by adding more functionality.

Page 13: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Example of partial overriding

class Workaholic : public Worker{public:

void doWork() //overriding base method

{ Worker::doWork();// call base methoddrinkCoffee(); // new methodWorker::doWork();// call base method

}};

Page 14: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Bindings

Static binding: the decision about which function/type to use to resolve an overload is made at compile time.

Dynamic binding: the decision must be made at run time.

Page 15: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

ExamplesWorker w;Workaholic wh;

. . . w.doWork();wh.doWork();

figure 4.8 static binding

Worker *wptr;cin>>x;if(x != 0)

wptr = new Workaholic();else

wptr = new Worker();. . .//which doWork is used ?wptr -> doWork();

Figure 4.9 dynamic binding

Page 16: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Virtual If a function is redefined in a derived

class, it should be declared virtual in the base class.

Example:class Worker{

public:virtual void doWork();

};

Page 17: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Virtual Cont…

Constructors are “NEVER” virtual

Destructors should be virtual for base class => Let the computer know which one to call for deallocation.

Page 18: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Abstract

Abstract method, pure virtual function, has no meaningful definition in the base class and must always be defined in derived classes.

Abstract class: a class containing any abstract method = > can’t be instantiated and must be inherited. Example: shape class

Page 19: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

The abstract base class shapeclass Shape { public: Shape( const string & shapeName = "" ) :

name( shapeName ) { } virtual ~Shape( ) { } virtual double area( ) const = 0;

//abstract method bool operator< ( const Shape & rhs ) const

{ return area( ) < rhs.area( ); } virtual void print( ostream & out = cout const

{ out << name << " of area "<< area( ); } private:

string name; }; //figure 4.13

Page 20: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

General rules Nonvirtual functions: Overloading

is resolved at compile time. Virtual functions: Overloading is

resolved at run-time (Base class provide a default implementation)

Pure virtual functions Overloading is resolved at run-time. (Base class provide no default implementation)

Page 21: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

4.4 Tricky C++ Details

Changing the default value in a derived class is unsafe because doing so can create an inconsistency with virtual functions.

Page 22: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Cont…

When a method is declared in a derived class, it “hides” all methods of the same name in the base class => get away by partial overriding or using “using” directive

Page 23: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Illustration of hidingclass Base{

public:virtual void bar();

};class Derived{

public:void bar(int x);

};Void test( Base & arg1, Derived & arg2){

arg1.bar(); // call Base::bar() => OKarg1.bar(4); //call Base::bar(int) => fails for not arg2.bar(4); // call Derived::bar(int) => OKarg2.bar(); // fails b/c Derived::bar(int) has hidden

Base::bar()}

Page 24: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Two ways to solve Hinding

Override the zero-parameter bar in Derived:

void bar() {Base::bar();}

Use keyword “using” using Base::bar;

Page 25: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

4.5 Multiple Inheritance A mechanism for deriving a class from

several base classes.Ex: Student and Employee are derived

classes of UniversityPerson.class Student: virtual public UniversityPerson()

{. . . };class Employee:virtual public UniversityPerson()

{…};class StudentEmployee : public Student,

public Employee{. . .};

Page 26: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Common errors (page 151) Inheritance is private by default. A

common error is to omit the keyword public, which is needed to specify public inheritance.

Objects of abstract classes can NEVER be initiated.

Constructors can never be declared virtual.

More errors defined in page 151..

Page 27: Chapter 4 Inheritance Bernard Chen Spring 2006. Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.

Summary

Inheritance: a powerful way for code reuse

Virtual: Dynamic binding, binding at execution time.

Abstract: must be defined in derived classes. Abstract classes can’t be instantiated.