Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes, Pure virtual functions Design issues UML examples Templates.

Similar presentations


Presentation on theme: "Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes, Pure virtual functions Design issues UML examples Templates."— Presentation transcript:

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

2 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/http://www.deitel.com/ – Fourth Edition available Oct 2002 – Includes a version of Microsoft Visual C++

3 Object Model Key ideas  Abstraction  Encapsulation  Modularity  Hierarchy Minor elements of the object model – Typing (language dependent – data typing) – Concurrency (OS dependent) – Persistence

4 Abstraction Helps to deal with complexity by focusing on certain features and suppressing others. Focus on interface (outside view) Separate behaviour from implementation

5 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

6 Encapsulation Also known as information hiding Hides the details of the implementation Complementary to abstraction

7 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.

8 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

9 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.

10 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

11 Example Class Hierarchy

12 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”

13 Inheritance in C++ Terminology – Base class (super class or parent class) – Derived class (sub class or child class) Base Derived

14 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!

15 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

16 Public inheritance Base object Private Base Private Derived object Private Public functions Interface Derived Interface

17 Compare with Composition Base object Private Public functions Base object Private Public functions Interface

18 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.

19 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”

20 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. };

21 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.

22 Multiple inheritance A derived class can inherit from more than one base class class D : public A, public B {.... baseA baseB derivedD

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


Download ppt "Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes, Pure virtual functions Design issues UML examples Templates."

Similar presentations


Ads by Google