Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf ·...

63
Advanced Object Oriented Programming Inheritance and aggregation Seokhee Jeon Department of Computer Engineering Kyung Hee University [email protected] 1 1

Transcript of Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf ·...

Page 1: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Advanced Object Oriented

Programming

Inheritance and aggregation

Seokhee JeonDepartment of Computer Engineering

Kyung Hee University

[email protected]

1 1

Page 2: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Inheritance ?

2

• Extend a class to create a new class while retaining

the members of the original class and adding new

members

• Inheritance is an is-a relationship

• A derived class is-a base class with more detail added

Page 3: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example - Polygons

3

Page 4: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Common attributes

4

Page 5: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Common attributes

5

Page 6: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Base Class - Polygon

6

Page 8: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Base and derived classes

8

Page 9: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Base and derived classes

9

Page 10: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Derived classes for Polygons

1

0

Page 11: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Derived class object

1

1

Page 12: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Derived class object

1

2

Page 14: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Inheritance access rules

1

4

Page 15: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Inheritance types

1

5

Page 16: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Polygon Class code (Base Class for

Triangle/Square/Rectangle)

1

6

class Polygons {

protected:

double area;

double perimeter;

public:

Polygons () {};

~Polygons () {};

void printArea () const;

void printPeri () const;

}; // Class Polygons

/* ====== Polygons :: printArea ======

Prints the area of a polygon.

Pre area calculated & stored in area

Post area printed */

void Polygons :: printArea () const

{

cout << "The area of your polygon is "

<< area << endl;

return;

} // Polygons printArea

/* ====== Polygons :: printPeri ======

Prints the perimeter of a polygon.

Pre polygon perimeter calculated and stored

Post perimeter printed */

void Polygons :: printPeri () const

{

cout << "The perimeter of your polygon is "

<< perimeter << endl;

return;

} //Polygons printPeri

Page 17: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Triangle class code

1

7

Triangle :: Triangle (double sideAIn,double sideBIn, double sideCIn)

{// Verify sides are validif ( ((sideAIn + sideBIn) <= sideCIn)|| ((sideBIn + sideCIn) <= sideAIn)|| ((sideCIn + sideAIn) <= sideBIn) )

{cout << "Invalid Triangle\n";exit (100);} // if

// Valid TrianglesideA = sideAIn;sideB = sideBIn;sideC = sideCIn;

calcPeri();calcArea();return ;

} // Triangle initialization constructor

class Triangle : public Polygons

{

private:

double sideA;

double sideB;

double sideC;

void calcArea ();

void calcPeri ();

public:

// initialization constructor

Triangle (double sideAIn,

double sideBIn,

double sideCIn);

}; // Class Triangle

void Triangle :: calcArea (){

double halfPeri = perimeter / 2;area = ( halfPeri

* (halfPeri - sideA)* (halfPeri - sideB)* (halfPeri - sideC) );

area = sqrt(area);return;

} // Triangle calcAreavoid Triangle :: calcPeri (){

perimeter = sideA + sideB + sideC;return;

} // Triangle calcPeri

Page 18: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Main function code for Triangle Class

1

8

Page 19: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Another example: EmpInfo inheritance

1

9

class EmpInfo

{

private:

int ID;

protected:

int Salary;

public:

char Name[200];

};

Base Class class EmpInfo : private SecurityEmp

{

. . .

}

ID (private) inaccessible

Salary (protected) private

Name (public) private

class EmpInfo : protected ImportantEmp

{

. . .

};

ID (private) inaccessible

Salary (protected) protected

Name (public) protected

class EmpInfo : public RegularEmp

{

. . .

};

ID (private) inaccessible

Salary (protected) protected

Name (public) public

Derived Classes

Page 20: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

TIP! Overriding access specifier

2

0

Page 21: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Constructor of derived class

2

1

base-member-initialization list

Page 22: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Destructor of derived class

2

2

Page 23: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Employee Class

2

3

Page 24: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Employee Class

2

4

Page 25: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Employee Class

2

5

Page 26: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Copy constructor of derived class

• Pass the parameter, which is a reference to the existing

derived object, to the constructor of the base class

Derived::Derived (const Derived& derivedObject)

:Base(derivedObject)

{

… // Derived Part Initialization

}

2

6

The reference type of a base class can

also be used to refer to an object of a

derived class

Page 28: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Overriding Example

2

8

Page 29: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Overriding Example

2

9

int main ()

{

BaseClass baseObj (1);

Derived derObj (12, 13);

cout << "Using print in base class: \n";

baseObj.print ();

cout << "\nUsing print in derived class: \n";

derObj.print ();

return 0 ;

} // main

/*Results

Using print in base class:

**Datum in Base Class is: 1

Using print in derived class:

**Datum in Base Class is: 12

**Datum in Derived Class is: 13

*/

Page 30: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Polymorphism: overloading

3

0

Allow many different forms with the same name; The meaning is determined by the context

Example in real life “open” a door

“open” a jar

“open” a book

Operator/function overloading is an simple example of polymorphism

Page 31: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Polymorphism with inheritance

3

1

Primary use of polymorphism in object-oriented

programming

write several versions of a function, each in a separate

class related by inheritance

When called, the function appropriate for the object

being referenced is executed

The programmer (and the program) does not have to

know the exact type of the object in advance, and so

the exact behavior is determined at run-time (this is

called late binding or dynamic binding). (from Wikipedia)

Page 32: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Pointing Derived Class using Pointer : Static Binding

3

2

Page 33: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Pointing Derived Class using Pointer : Static Binding

Example

3

3

Page 34: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Pointing Derived Class using Pointer : Dynamic

Binding

3

4

34

Page 35: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Pointing Derived Class using Pointer : Dynamic Binding

Example

3

5

Page 36: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Problem for Destructor & Virtual Destructor

3

6

Page 37: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Virtual Destructor

3

7

Page 38: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Model is required

3

8

The virtual functions do not force the overriding of a function

There are occasions that we want to force the derived class to override (redefine) a function

What if in the future we want more polygons?

We need to define a model in the base class and let the derived classes follow the model

In the base class, we can define the minimum number of functions and the format that is needed for each derived class to include

Page 39: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Pure Virtual Function

3

9

Syntax

Declared in a base class

Have no action in base class (just for conceptual requirement)

Should be defined in each derived class

virtual return_type identifier (parameter list) = 0

Page 40: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Abstract Class

4

0

Page 41: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Modified Polygon Class

4

1

Page 42: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Modified Triangle Class

4

2

Page 43: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Modified Rectangle Class

4

3

Page 44: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Modified Square Class

4

4

Page 45: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Modified main()

4

5

Page 46: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Modified main() using Base Class pointer

4

6

Page 47: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Review: Casting Standard Data Types

4

7

Can convert a standard data type to another standard data type using either implicit or explicit casting (static casting)

Can convert a pointer to a standard data type to a pointer to another standard data type using explicit casting (implicit is not allowed)

Let’s expand the concept to objects and pointer

to objects

Page 48: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Class : Upcasting and Downcasting

4

8

casting of objects (or pointer to objects) is applicable when the classes form a hierarchy of inheritance

Page 49: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Upcasting Objects

4

9

Note:

A derived class object is a special type of its base class object

Page 50: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Implicit and explicit class conversion

5

0

int main (){

cout << "Before assign: ";Base baseObj (1);baseObj.print ();

Derived derObj (3, 5 );baseObj = derObj; // implicit upcastingcout << "After assign: ";baseObj.print ();return 0;

} // main

/* Results

Before assign: Value of

baseData in class Base is: 1

After assign: Value of baseData

in class Base is: 3

*/

If we use explicit upcasting,

baseObj = static_cast<Base>derObj;

This has the same effect with the implicit casting

Page 51: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Downcasting objects

5

1

Downcasting of objects is not allowed in C++

Notice that a derived class object normally has more data member than the base class

The language system does not know how to initialize the extra data

Page 52: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Upcasting Pointers to Objects

5

2

Page 53: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Downcasting Pointers to Objects

5

3

Downcasting pointers to objects is possible only with

dynamic casting

Dynamic casting: casting in which the type is determined

during the execution of the program, as opposed to during

the compilation

Recall dynamic binding

Dynamic casting can be done only with polymorphic

classesthat is, classes using virtual functions

Syntax

dynamic_cast<type> (data)

Page 54: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Downcasting Pointers to Objects (Valid case)

5

4

Dynamic casting is valid only when the base class

pointer is already pointing to a derived class object

Page 55: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Downcasting Pointers to Objects (Invalid case)

5

5

cannot downcast when the base class pointer is not

pointing to a derived class object

the cast returns a null pointer value (0)

Page 56: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Multiple Inheritance

5

6

Page 57: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Multiple Inheritance : Examples

5

7

Page 58: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Another Example: C++ File Library

5

8

ios_base

ios

istream ostream

iostream

istringstream ofstream ostringstreamifstream

fstream stringstream

Page 59: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Aggregation

• Inclusion of objects within an object

• Has-a relationship

5

9

Page 60: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Aggregation

6

0

Page 61: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Instantiating an aggregated object

6

1

Page 62: Inheritance and aggregation - khu.ac.krhaptics.khu.ac.kr/aoop/6._Inheritance and aggregation.pdf · 2014. 8. 11. · Polymorphism with inheritance 3 1 Primary use of polymorphism

Example: Implementation of Aggregation

6

2

class Student{

private:long id;int units;int grPts;Date dob;

public: Student (long idIn, int unitsIn,

int grPts, unsigned short day, unsigned short mon, unsigned short year);

void print () const;}; // Class Student

Student :: Student (long idIn, int unitsIn, int grPtsIn,unsigned short dayIn, unsigned short monIn, unsigned short yearIn)

: dob (dayIn, monIn, yearIn){

id = idIn;units = unitsIn;grPts = grPtsIn;

} // Constructor

void Student :: print () const

{

cout << "Student: " << id

<< " Units: " << units

<< "\nGr Pts: " << setw (4) << grPts

<< " Birth Date: ";

dob.print();

cout << endl;

} // Student