1 Object Oriented Programming Development - Week7 z Rob Manton z z Room D104.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Inheritance and Polymorphism.
1 Object Oriented Programming Development - Week 5 z By: Marc Conrad University of Luton z z Room: D104.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Dynamic Polymorphism Lecture-11. Method Polymorphism In a monomorphic language there is always a one to one relationship between a function name and it’s.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
1 Object Oriented Programming Development - Week8 z Rob Manton z z Room D104.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
OOP Etgar 2008 – Recitation 61 Object Oriented Programming Etgar 2008 Recitation 6.
OOP Spring 2007 – Recitation 41 Object Oriented Programming Spring 2007 Recitation 4.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
1 Object Oriented Programming Development z By: Marc Conrad University of Luton z z Room: D104.
LECTURE 07 Programming using C# Inheritance
Abstract classes and Interfaces. Abstract classes.
Inheritance using Java
1 Object Oriented Programming Development - Polymorphism I z By: Marc Conrad & Rob Manton University of Luton z
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Object Oriented Programming Development
1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z z Room: D104.
Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
1 Practical test tomorrow zWill involve writing a simple class zinstantiating objects zother C++ constructs as practised in the lab sheets to date zinheritance.
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
1 Object Oriented Programming Development - Multi File Development z By: Marc Conrad & Rob Manton University of Luton z
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
1 Object Oriented Programming Development - Week 3 z By: Marc Conrad University of Luton z z Room: D104.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.
Object Oriented Software Development
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
1 Object Oriented Programming Development z By: Marc Conrad University of Luton z z Room: D104.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Overview of C++ Polymorphism
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Inheritance & Classification Hierarchies Lecture-8.
Inheritance and Polymorphism
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance II CMSC 202.
Understanding Inheritance
Learning Objectives Inheritance Virtual Function.
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
CISC/CMPE320 - Prof. McLeod
Overview of C++ Polymorphism
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Lesson 17 Inheritance II.
Presentation transcript:

1 Object Oriented Programming Development - Week7 z Rob Manton z z Room D104

2 Module Outline zIntroduction zNon object oriented basics zClasses z Inheritance z Aggregation z Polymorphism z Multifile Development

3 Today: zTiming of the practical test in week 9 zAdvice for lab sessions zClasses & Objects recap zInheritance

4 Practical Test in Week 9 Who already has a lecture/practical on Tuesday 26th November 5-8pm Wednesday 27th November 5-8pm Thursday 28th November 5-8pm

5 Advice for lab sessions zCreate a ‘hello world’ project instead of an empty one ztype a line or two of code then compile - this should make it easier to identify and fix syntax problems zWhen writing a class or method add the opening and closing braces/semicolon and compile before ‘fleshing out’.

6 Advice for lab sessions #include "stdafx.h" int main(int argc, char* argv[]) { printf("Hello World!\n"); return 0; } The skeleton program you get from a ‘hello world’ project

7 Advice for lab sessions #include "stdafx.h" int main(int argc, char* argv[]) { printf("Hello World!\n"); return 0; } It will compile, giving you a good starting point for your code. Remember to use Build/Rebuild All

8 Advice for lab sessions #include "stdafx.h" int main(int argc, char* argv[]) { printf("Hello World!\n"); return 0; } This is specific to the Microsoft compiler - just leave it there and everything should work properly

9 Advice for lab sessions #include "stdafx.h" int main(int argc, char* argv[]) { printf("Hello World!\n"); return 0; } These are used if you need to access any command line parameters passed to the program. Having them here won’t do any harm, but you can delete them if you like

10 Advice for lab sessions #include "stdafx.h" int main( ) { printf("Hello World!\n"); return 0; } You can get rid of the printf line but you do need to keep the return 0; line

11 Advice for lab sessions #include "stdafx.h" class creature { }; int main() { return 0; } Add your class definition as a ‘skeleton’: add the opening and closing braces and semicolon and compile before adding any more detail

12 Advice for lab sessions #include "stdafx.h" class creature { private: int yearOfBirth; public: int getYearOfBirth(); void setYearOfBirth(int YOB); }; int main() { printf("Hello World!\n"); return 0; } Now you can start to ‘flesh out’ the class definition

13 Classes and objects recap zThe creature class - analysis of the component parts (handout)

14 #include "stdafx.h" #include class creature { private: int yearOfBirth; public: creature(); virtual ~creature(); int getYearOfBirth(); void setYearOfBirth(int year); }; int main() { creature myDog; myDog.setYearOfBirth(1966); cout << "my dog was born in" << myDog.getYearOfBirth() << endl; return 0; }

15 creature::creature() { cout << "constructor called for creature object." << endl; } creature::~creature() { cout << "destructor called for creature object." << endl; } void creature::setYearOfBirth(int year) { yearOfBirth = year; } int creature::getYearOfBirth() { return yearOfBirth; }

16 Now for something new.. zInheritance

17 What is Inheritance? zClasses organised into a ‘classification hierarchy’ zClasses can inherit attributes and methods from other classes zInheriting class can add extra attributes and or methods of its own

18 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two or more classes zImproved efficiency and greater robustness zPlays a major part in polymorphism -more about this in two weeks time

19 Terminology zDerived class or subclass or child class. A class which inherits some of its attributes and methods from another class zBase class or superclass or parent class. A class from which another class inherits zancestor. A class’s ancestors are those from which its own superclasses inherit zdescendant. A class’s descendants are those which inherit from its subclasses

20 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block

21 Terminology - a classification Hierarchy Generalisation Specialisation Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block

22 Terminology - a classification Hierarchy Generalised ‘base class’ Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block

23 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block A ‘kind of’ Building (AKO)

24 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block A ‘kind of’ Commercial building (AKO)

25 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block Arrow in diagram means ’inherits from’

26 Designing your classification hierarchy: ‘A kind of’ or ‘a part of’? Car Vehicle A car is ‘a kind of’ vehicle car class can inherit from vehicle class

27 Car Wheel Vehicle  A car is ‘a kind of’ vehicle car class can inherit from vehicle class A wheel isn’t ‘a kind of’ car. A wheel is ‘a part of’ a car - this is dealt with by aggregation which is next week’s topic Designing your classification hierarchy: ‘A kind of’ or ‘a part of’?

28 yNeed to analyse whether differences between objects are dependant on type (such as a house being different to a factory) or state. (different values of the class’s attributes) Building Short Building Tall Building  short building and tall building might vary only in the value of the height attribute - don’t need separate classes Designing your classification hierarchy: Different classes or different states?

29 What do objects inherit? Line Attributes: start position end position Methods: draw Coloured Line Attributes: colour Methods: set colour A ‘coloured line’ is a kind of line the coloured line class inherits all the attributes and methods of the line class and adds attributes and methods of its own An object of the ‘coloured line’ class has all the attributes and methods of the ‘line’ base class as well as the attributes and methods added by the derived class

30 Specialisation Extending the functionality of an existing class eg a coloured line is a specialised kind of line

31 Specialisation A class is both yclosed in that it has an encapsulated, private part which cannot be affected by external manipulation yand open in that it allows itself to be used as part of a larger software unit.

32 Generalisation Sharing commonality between two or more classes xIf we were modelling animals in a zoo would we create a separate class for each animal type? xThis would duplicate attributes such as legs and methods such as getAge() CowWhaleEagleElephant

33 Generalisation xHelpful to place common elements (attributes and methods) in a shared base class and organise problem into an inheritance hierarchy. CowWhaleEagleElephant Animal MammalBird

34 Generalisation xSometimes this leads to the creation of abstract classes which can’t be instantiated directly CowWhaleEagleElephant Animal MammalBird Abstract classes

35 Generalisation xconcrete classes can be instantiated directly CowWhaleEagleElephant Animal MammalBird Concrete classes

36 C++ Syntax zThe colon (:) operator to denote inheritance zPublic and private derivation of object methods from a base class zThe ‘protected’ keyword to allow derived classes to access inherited attributes

37 C++ Syntax zclass BaseClass z{ zprivate: yint x; zpublic: yvoid setX(int x_in); yint getX(); z} A simple base class with one private attribute x and two public methods

38 C++ Syntax: public derivation zclass DerivedClass: public BaseClass z{ zprivate: yint y; zpublic: yvoid setY(int y_in); yint getY(); z} A derived class. The colon operator means the derived class inherits from the base class

39 C++ Syntax: public derivation zclass DerivedClass: public BaseClass z{ zprivate: yint y; zpublic: yvoid setY(int y_in); yint getY(); z} the public derivation means that objects of the derived class can access the public methods and attributes of the base class This is the most usual type of derivation

40 C++ Syntax: public derivation zclass BaseClass z{ zprivate: yint x; zpublic: yvoid setX(int x_in); yint getX(); z} zclass DerivedClass: public BaseClass z{ zprivate: yint y; zpublic: yvoid setY(int y_in); yint getY(); z} Main() { BaseClass base_object; DerivedClass derived_object; base_object.setX(7); derived_object.setX(12); derived_object.setY(1); return 0; } Object of the derived class can access methods of the derived class and also methods of the base class

41 C++ Syntax: private derivation zclass DerivedClass: private BaseClass z{ zprivate: yint y; zpublic: yvoid setY(int y_in); yint getY(); z} Another derived class - the private derivation means that objects of the derived class can’t access the public methods and attributes of the base class - but the methods of the derived class can! This is the least common type

42 C++ Syntax: the ‘protected’ keyword zAn object of a publicly derived class can access the public methods of the base class, but not the private attributes zChanging the private keyword in the base class to protected makes the attributes available to derived classes

43 C++ Syntax: inheriting constructors zA derived class always inherits the constructor of the base class. The base class constructor is called first. zIf the base class constructor takes no parameters then the inheritance is implicit - you don’t need to do anything! zIf the base class constructor takes parameters then each derived class needs to declare a constructor with the same parameters. You can pass the arguments given to the derived class constructor to the constructor for the base class

44 C++ Syntax: inheriting constructors class Customer { Customer (char * name_in); … } Class AccountCustomer:public Customer { AccountCustomer(char * name_in);.. } Base class declares a constructor that takes a char pointer parameter Derived class declares a constructor that takes the same char pointer parameter

45 C++ Syntax: inheriting constructors AccountCustomer: AccountCustomer(char * name_in): Customer (name_in) { //main body of constructor.. } In the implementation of the constructor for the derived class, the parameter passed to the derived class constructor is passed down to the base class constructor. Note use of the colon (:) syntax once again

46 C++ Syntax: inheriting constructors class creature { private: int yearOfBirth; public: creature(int YOB); intgetYearOfBirth(); }; int main() { creature myCreature(1985); cout << "my creature was born in " << myCreature.getYearOfBirth() <<endl; return 0; } This class has a constructor that takes an integer argument. When instantiating an object of this class you pass a parameter to the constructor.

47 C++ Syntax: inheriting constructors class dog:public creature { public: void bark(); }; int main() { creature myCreature(1985); dog myDog(1985); cout << "my creature was born in " << myCreature.getYearOfBirth() <<endl; return 0; } At the moment we can’t do this: there is no constructor for the dog class that takes an integer argument Dog class derived from creature class

48 C++ Syntax: inheriting constructors class dog:public creature { public: dog(int YOB); void bark(); }; //implementation for dog constructor dog::dog(int YOB): creature(YOB) { //other constructor stuff goes here } Now we have defined a constructor that does take an integer argument The argument sent to the dog constructor gets sent to the creature constructor so the YearOfBirth attribute of the base class gets set properly

49 C++ Syntax: inheriting constructors class dog:public creature { public: dog(int YOB); void bark(); }; int main() { creature myCreature(1985); dog myDog(1985); cout << "my creature was born in " << myCreature.getYearOfBirth() <<endl; cout << "my dog was born in " << myDog.getYearOfBirth() <<endl; return 0; } Now we do have an appropriate constructor for the dog class which correctly initialises the attributes defined in the base class

50 C++ Syntax: inheriting destructors zA derived class always inherits the destructor of the base class. The derived class destructor is called first. This is the reverse of the sequence for constructors zBecause destructors never take an argument there is no issue with inheritance of destructor parameters.

51 Summary: Inheritance zInheritance allows classes to inherit attributes and methods from other classes in a classification hierarchy zInheritance allows specialisation (extending the functionality of an existing class) and generalisation (sharing commonality between two or more classes) zInheritance is appropriate where a class can be said to be ‘a kind of’ other class

52 Summary: Inheritance zInheriting from a class doesn’t affect the integrity of that class - objects of the original base class can still be created zGeneralisation allows removal of redundancy and duplication among classes zSome base classes are abstract - they are not specific enough to be instantiated but act as holders for common attributes and methods of derived classes

53 Summary: Inheritance zIn C++ the protected keyword allows methods of a derived class access to its inherited attributes zBase class constructor methods are automatically called by the constructors of derived classes but argument lists must be compatible zDestructors are called in the reverse order of constructors