27
Rossella Lau Lecture 7, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design Lecture 7: OOP: Inheritance Inheritance and composition • Basic OOD concept • “is-a” relationship & “has-a” relationship Implementation of Inheritance • Syntax in C++ • Construction from the UML • Scope referencing • The sample application: Grade -- By Rossella Lau

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design Lecture 7: OOP: Inheritance Inheritance and composition

Embed Size (px)

Citation preview

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

DCO10105 Object-Oriented Programming and Design

Lecture 7: OOP: Inheritance

Inheritance and composition• Basic OOD concept• “is-a” relationship & “has-a” relationship

Implementation of Inheritance• Syntax in C++• Construction from the UML• Scope referencing• The sample application: Grade

-- By Rossella Lau

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Three basic principles Encapsulation

• Pack data and operations into a class to hide its details from the outside world (class construction)

Inheritance• Classes can be derived from existing classes to save programmers’

effort – code reuse Polymorphism

• Same class can play as different types (of ancestors)

• Same expression can perform different operations

OOP implements OOD

Basic OOD concept

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Construction from an UML class notation

Malik’s example: Ch 12:personType.h & personTypeImp.cpp

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Instantiation of objects

Each object instantiated from a class will have its own data members with some values

lastNamefirstName

personType

ChanTerence

person1

TseCyril

person2

WongPatrick

person3

LauRossella

person4

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Instantiation but not static member

E.g., DayType includes a static member weekDays

Instead of a weekDays for each instance, there is only one single value residence somewhere of the program

weekDayweekDays

DayType

Mon

day1 Tue

day2 Wed

day3

weekDays

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Application: Grade

Programs under the folder “GradeReport” of Chapter 13

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Composition

Inside studentType, courseEnrolled is in type of courseType[]

I.e., studentType is constructed with data members of the type of another class courseType

This is called composition

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

“has-a” relationship

Composition can be interpreted as a “has-a” relationship A student has a number of courses enrolled; an orderItem

has a “Product”; a retail system has a “Catalog” and some “Cashiers”

Composition allows a class to be constructed with data members of the type of another class

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Construction with aggregation

When there is an aggregation in an application, the instance of the class ended at the diamond should be declared as a container (an array or a vector)

courseEnrolled in studentType studentList in the main program

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Construction with generalization

When there is a specialization (vs generalization), the class definitions should be written as a sub class of the generalization class – the concept of inheritance

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Inheritance

Inheritance allows new classes to be created, derived from existing classes

The existing class is called “base class” or “parent class” The new derived class is called “sub class” or “child

class”

Derived classes inherit, carry or own, all the properties of the base class

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Syntax of Inheritance

class SubClass : [AccessSpecifier] BaseClass {......};

: (colon) indicates the relationship of inheritance The left hand side is the new class, the sub class, and is

going to be derived, i.e., to own all the properties (data members, function members, and the type), from the base class

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Access Specifier

AccessSpecifier can be one of these three: public, private, and protected

It specifies the ability of an outsider (things other than the members of SubClass) on how to access the members in BaseClass

public allows an outsider to access every public member in BaseClass through an instance of SubClass

private does not allow an outsider to access any members, even if it is a public member, in BaseClass

protected allows a child class of SubClass to access public members in BaseClass but does not allow other outsiders to access any members, even if it is a public member, in BaseClass

In our course, we will focus on public inheritance

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

An example:

Malik’s example: Ch 13:studentType.h & studentTypeImp.h

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Instantiation of a student

Each object instantiated from studentType will include not only the values of studentType’s data but also values of personType’s data

sIDnumberOfCoursesisTuitionPaidcourseEnrolled[]courseGrades

studentType

ChanJudy501234566True{(OOP,DCO10105,3),…}{A,…}

student1

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Inheritance is “is-a” relationship

Inheritance can be interpreted as an “is-a” relationship

“Coffee” is a “Product”; “Coffee Brewer” is a “Product” “Cash payment” is a “Payment”, “Octopus payment” is a

“Payment”

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Protected members of a class

With inheritance, members of a class can also be protected in areas other than public and private

Protected members are public to all sub classes of the class the members belong to but private to classes not belonging to the family of the class including the protected members

In this course, we will focus on private or public members

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Scope referencing

For a user program which has: stuendType student;

student.getFirstName() uses the member defined in personType

student.setInfo() uses the member defined in studentType

How about student.print()?

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

When two functions are identical ……

For student.print() , it uses the print() in studentType since the compiler finds studentType first then its base class(es) personType

Indeed, both print() are identical, i.e., they have the same signatures

print()of studentType redefines, or overrides, the print() in personType

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

More about scope resolution operation ::

When a sub class uses an identical function (with same signature) in the base class, :: should be used

In personType, void print(){ outF << getFirstName() << " "

<< getLastName() << endl;

} In studentType, output of a student’s name can become: outF << "Student Name: " << personType::print();

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Avoid identical codes – code reuse

Therefore, identical codes in sub-classes should be avoided:

E.g., on line 48-49 in studentTypeImp.cpp: outF << "Student Name: " << getFirstName()

<< " " << getLastName() << endl; part of the statement is the same as in personType::print()

It can be changed, for the sake of code reuse or redundant code elimination, as follows:outF << "Student Name: " << personType::print();

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

A sub class can play as its base class

As a sub class would have all the properties of its base class – it can act as its base class It can be in the type of its base class

One more example on scope referencing class: DonaldFamily.h and scopePlayer.cpp

Donald boxD(dewey) : dewey plays as a Donald object deduct7(huey) : huey plays as a Donald object for

calling

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

static_cast<ClassName> (variable )

Temporary change a variable’s type to ClassName e.g., line 33, 48, 50 in DonaldFamily.h

Formal C uses (ClassName ) variable

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Function overload and override

In DonaldFamily.h: In class Dewey and Huey, withdraw() is overloaded

• Indeed, withdraw(int) in Dewey seemed redundant because usually it can be referenced automatically by type matching; however, double is a type compatible to int and cause calling to withdraw(double) even if the actual parameter is an integer from dewey.withdraw(static_cast<int> (5));

In class Donald, getMoney(), withdraw(int), and print() are overridden by both Dewey and Huey

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Initialization of data members in base class

Since data members in the base class are also data members of the sub class, initialization of those members should use a constructor of the base class

Dewey(double) uses Donald(int) Huey(double) uses Donald(), the default constructor,

even if it is not explicitly written there; i.e., the compiler will automatically generate a call to the default constructor to initialize data members in the base class if no such initialization in the constructor of the sub class

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Summary

Members of a class can be public, private, and protected

Inheritance is an “is-a” relationship while composition is a “has-a” relationship

A derived class inherits all its base class’ members + type

Inheritance increase code reuse

C++ allows for three access types of inheritance, we have examples for public inheritance

When identical functions, with same id and signatures, in sub class and base class, instance of sub class access the function in sub class and this is called function override

Rossella Lau Lecture 7, DCO10105, Semester B,2005-6

Reference

Malik: 12.5, 13

http://bdn.borland.com/article/0,1410,31863,00.html

-- END --