1 Object Oriented Programming Development - Multi File Development z By: Marc Conrad & Rob Manton University of Luton z

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
CS 211 Inheritance AAA.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Inheritance, Polymorphism, and Virtual Functions
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
1 Object Oriented Programming Development z By: Marc Conrad University of Luton z z Room: D104.
1 Object Oriented Programming Development - Polymorphism I z By: Marc Conrad & Rob Manton University of Luton z
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z z Room: D104.
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
1 Object Oriented Programming Development - Week 3 z By: Marc Conrad University of Luton z z Room: D104.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Polymorphism.
Inheritance and Run time Polymorphism
Designing for Inheritance
Java Programming Language
Inheritance, Polymorphism, and Virtual Functions
Packages and Interfaces
Polymorphism Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Fundaments of Game Design
Inheritance and Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Polymorphism 2 CMSC 202.
CMSC 202 Lesson 19 Polymorphism 2.
Chapter 11 Class Inheritance
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

1 Object Oriented Programming Development - Multi File Development z By: Marc Conrad & Rob Manton University of Luton z z Room: D104

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

3 Today’s lecture zPolymorphism II recap yPolymorphic Pointers yOverriding Methods yStatic typing & Dynamic binding zMulti File Development yReferring to classes not yet defined y.H and.CPP files yLibraries

4 Polymorphic Pointers zIn C++ a pointer of a parent class is allowed to point to an object of the child class. E.g. class Vehicle { //... }; class Car : public Vehicle { //... }; //... Vehicle * vp = new Car(); Valid C++ syntax!

5 Overriding Methods Three ways for a derived class to implement a polymorphic method yInherit it unchanged yReplace with a different implementation yAdd to the existing implementation

6 Overriding Methods zMethods in the parent class can be redefined in the child class. zclass Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; //... Vehicle * vp = new Car(); vp->move(100); Valid C++ syntax!

7 Overriding Methods zMethods in the parent class can be redifined in the child class. zclass Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; //... Vehicle * vp = new Car(); vp->move(100); BUT: z Which of these two move() methods will be called?

8 Overriding Methods zMethods in the parent class can be redifined in the child class. zclass Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; //... Vehicle * vp = new Car(); vp->move(100); static typing! z In C++, static typing is the default behaviour. As vp is of type pointer to a Vehicle, the method of the Vehicle is called.

9

10 As vp is of type pointer to a Vehicle, the method of the Vehicle is called.

11 Overriding Methods - The keyword virtual zMethods in the parent class can be redefined in the child class. zclass Vehicle { virtual void move(int i); }; class Car : public Vehicle { virtual void move(int i); }; //... Vehicle * vp = new Car(); vp->move(100); dynamic binding! The keyword virtual allows the use of dynamic binding. As vp points to a Car object the method of the Car is called

12

13 Dynamic Binding: As vp points to a Car object the method of the Car is called

14 Abstract Methods & Classes zAbstract methods are methods without any implementation (pure virtual methods). zclass Vehicle { virtual void move(int i) = 0; }; class Car : public Vehicle { virtual void move(int i); }; //... Vehicle *vp = new Car(); vp->move(100); Syntax for declaring abstract methods. Note that Vehicle objects cannot be instantiated (but Car objects can).

15 Syntax for declaring abstract methods. We are not allowed to declare an object of an abstract type...

16 Static typing & Dynamic binding zStatic typing means that the legality of a member function invocation is checked at the earliest possible moment: by the compiler at compile time. The compiler uses the static type of the pointer to determine whether the member function invocation is legal. z Dynamic binding means that the address of the code in a member function invocation is determined at the last possible moment: based on the dynamic type of the object at run time. It is called "dynamic binding" because the binding to the code that actually gets called is accomplished dynamically (at run time).

17 Why is dynamic binding useful? zIf we have a variety of classes based on the same base class then each type can be pointed to by a pointer to the base class animal * pAnimal; pAnimal=new sheep(); pAnimal=new crocodile(); Animal mammalreptile humansheepcrocodile

18 Why is dynamic binding useful? zAny method declared in the base class.. Animal mammalreptile humansheepcrocodile MakeNoise()

19 MakeNoise() Why is dynamic binding useful? zAny method declared in the base class.....can be overridden by any derived class Animal mammalreptile humansheepcrocodile MakeNoise()

20 MakeNoise() Why is dynamic binding useful? zWith static binding, the version of the method that gets called is the one defined in the base class (not so useful!) animal * pAnimal; pAnimal=new sheep(); pAnimal->MakeNoise(); Animal mammalreptile humansheepcrocodile MakeNoise()

21 MakeNoise() Why is dynamic binding useful? zWith dynamic binding, the version of the method that gets called is the one defined in the class of the object that the pointer points to animal * pAnimal; pAnimal=new sheep(); pAnimal->MakeNoise(); Animal mammalreptile humansheepcrocodile virtual MakeNoise() MakeNoise()

22 Why is dynamic binding useful? zAt compile time we can create a pointer to the base class and call a method defined in the base class. This should compile properly. zAt run time we can make that pointer point to an object of any class in the same inheritance hierarchy and the correct version of the method gets called. zVery useful for handling situations where the number or composition or lifespan of objects cannot be predicted at compile time

23 Practical example zIn a computer game we might have an inheritance hierarchy containing many different characters (old man, woman, alien, spider, goblin, martial arts expert etc etc) all derived from a base character class. Character Update() Draw()

24 Practical example Character HumanAnimal Old man Martial arts expert Alienspider Each derived class can override the base class methods and provide its own update() and draw() methods

25 Practical example zWe don’t need to know at compile time how many of each type of character will exist at any time during the game zWe can have an array of pointers or a linked list or other container object to store a variable number of character objects zCharacter * characters[50];

26 Practical example zAt run time we can allocate dynamically created objects of any of the classes in the hierarchy to the pointers zeg characters[1]=new Alien(); zcharacters[2]=new Spider(); zcharacters[3]=new OldMan();

27 Practical example zBecause they are all derived from a standard base class we can call the same methods of each derived character for (i=0;i<50;i++) { if (Characters[i]!=NULL) { Character[i]->Update(); Character[i]->Draw(); }

28 Practical example zPolymorphism in action za single method name being called from different types of object (old man, spider, martial arts expert, alien etc) zCaters well with situations where number or composition or life time of objects can’t be predicted at compile time..

29 Important stuff to remember zOverriding methods zPolymorphic pointers zStatic binding zDynamic binding - the virtual keyword zAbstract methods

30 Referring to classes not yet defined a)If a class contains or references another class (aggregation or association) then the compiler needs the enclosed class to be defined first - what happens if we have a circular relationship?

31 Referring to classes not yet defined zwhat happens if we have a circular relationship - which class goes first? class Car { private: Person * myDriver; }; class Person { private: Car * myCar; }; Car has a pointer to person and person has a pointer to car

32 Referring to classes not yet defined Person object not defined yet so we get an error message

33 Referring to classes not yet defined zSolution is to provide a ‘forward declaration’ of the class which is being used but hasn’t been declared yet class Person; class Car { private: Person * myDriver; }; class Person { private: Car * myCar; }; Forward declaration of Person class means that compiler lets you refer to Person class even though Person class hasn’t been declared yet

34 Referring to classes not yet defined Car has a pointer to person and person has a pointer to car forward declaration makes this legal!

35.H and.CPP files zOn larger projects class definitions and implementations are usually split up zdefinitions go into a.H file zimplementations into a.CPP file zcan do this automatically in the compiler with Insert/New Class

36.H and.CPP files Name of class Optional base class Insert/New Class

37.H and.CPP files.h and.cpp files now appear in project Skeleton for class definition Skeleton for class implementation

38.H and.CPP files If we refer to a class defined in separate file we get an error

39.H and.CPP files Need to #include the.h header file then all is OK!

40 Using libraries zRapid application development - using existing modules or libraries rather than writing your own zReady made libraries like DirectX, OpenGL, Renderware etc comprise of a set of Header files (.H) and a set of library files (.LIB) zA static library (.LIB) is a file containing objects that is linked into your program when the executable file is built.

41 Using libraries zDon’t need to recompile the library files each time - saves time zPreserves security of source code (commercial reasons) zUser only needs to know about the interface to the classes which is defined in the header file zfollows C++ notion of separation of interface from implementation

42 Using libraries zHow do you make a library file? Static library is an option in the usual File/New Dialog

43 Using libraries How do you use an existing library file? zInsert both the.lib file and the.h header file into your project using Project/Add to project/Files zAdd the folder that contains the library/header files to the list of paths in Tools/Options/Directories One list of folders relates to include files (.h) another to library (.lib) files

44 That’s all for today zFirst session after Christmas will be revision for the exam zSee you then and have a good one..