Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes,...

23
Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes, Pure virtual functions Design issues UML examples Templates (time permitting)

Transcript of Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes,...

Topics

Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes, Pure virtual functions Design issues UML examples Templates (time permitting)

Books

Thinking in C++ by Bruce Eckel– Vol.1 (basics) and Vol.2 (advance features)– Assumes knowledge of ‘C’– Free Electronic versions available from-

http://www.mindview.net/Books

C++ : How to program by Deitel & Deitel– See info at http://www.deitel.com/– Fourth Edition available Oct 2002– Includes a version of Microsoft Visual C++

Object Model

Key ideas Abstraction Encapsulation Modularity Hierarchy

Minor elements of the object model– Typing (language dependent – data typing)– Concurrency (OS dependent)– Persistence

Abstraction

Helps to deal with complexity by focusing on certain features and suppressing others.

Focus on interface (outside view)

Separate behaviour from implementation

Object Hierarchies

A way of ordering abstractions

Object hierarchical abstractions (“HAS A” or “PART OF” relationship)

Interfaces and behaviours at each level

Higher levels are more abstract

Encapsulation

Also known as information hiding

Hides the details of the implementation

Complementary to abstraction

Abstraction, Encapsulation and Software Design

Interface should be simple providing the required behaviour.

User is presented with high level abstract view. The detail of the implementation hidden from user.

The designer may change the implementation keeping interface the same.

Modularity

A common “Divide and conquer” approach

Partitions a problem into sub-problems reduced complexity

Modularity packages abstractions into discrete units

In C++ classes are the basic modules providing encapsulation and abstraction

Re-usability - Inheritance

Class Hierarchies Derived classes inherit

properties and behaviour of base class

Allows code re-use. Derived classes can

have – additional properties

and behaviour,– or over-ride inherited

behaviour.

Inheritance

Allows code re-use : Fine in theory – requires good design

Use when relationship between classes is – “Kind of” or “is a”

Create class hierarchies– Factor out common attributes and behaviour– Place common features in base class– Differences in separate derived classes

Example Class Hierarchy

Class hierarchies

Class at top of hierarchy is most abstract Classes become more “specialised” or “concrete” as

we go down the class hierarchy. Often we will only create real objects of the lowest

classes in the hierarchy not the classes at or near the top of the hierarchy.

In C++ we can prohibit the creation of objects of the base classes by making the class an “abstract class”

Inheritance in C++

Terminology– Base class (super class or

parent class)– Derived class (sub class or

child class)

Base

Derived

Inheritance in C++

class b

{

….

etc.

};

class d: public b

{

….

etc.

};

public inheritance is the normal inheritance method used. The inheritance could also be private or protected – very rarely used.

Note: if the method of inheritance is omitted it defaults to private!

Inheritance

The derived class :– will inherit all the attributes and functions/methods

of the base class– can have additional attributes– can have additional functions/methods– can override functions/methods of the base class– will have the SAME INTERFACE as the base

class plus possible additions

Public inheritance

Base object

Private

Base

Private

Derived object

Private

Public functions

Public functions

Interface

Derived Interface

Compare with Composition

Base object

Private

Public functions

Base object

Private

Private

Public functions

Interface

The Derived class

The constructor function of the derived class should invoke the constructor of the class from which it has been derived.

Use the initialiser list of the derived constructor to invoke the base class constructor.

Base constructor invocation may be omitted iff the base class has a default constructor which will be automatically invoked.

Inheritance vs composition

Use inheritance where the base interface is required to be available in derived class either as is or with some functions overidden.

Use composition when the base interface is not needed or needs to be augmented or controlled by the enclosing class.

Use composition when the enclosing class uses several objects of differing classes

Given a choice use composition in preference to inheritance.

Look for “has a” vs. “is a”

class B { public: // constructor

B(int x) { privint = x; } private: int privint;

etc.};

class D : public B{ public: // constructor

D(float i, int j) : B(j) { privfloat = i;} private: float privfloat;

etc.};

Protected access specifier

As well as public and private access specifiers we have protected access specifier.

Protected access is an intermediate level between private and public.

protected class members can only be accessed by the class and any derived classes

Guide - Still use private in preference to protected unless there is a good reason not to.

Multiple inheritance

A derived class can inherit from more than one base class

class D : public A, public B

{

....

baseAbaseA baseB

derivedD

Example programs

Read info.txt and inspect code– Inheritance – base classes ex01 and ex01a– Inheritance – derived classes ex02– Virtual functions – ex03– Virtual destructor – ex04a