logging in or signing up week12 Cubemiddle Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 143 Category: Entertainment License: All Rights Reserved Like it (1) Dislike it (0) Added: October 10, 2007 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Object Oriented ProgrammingDevelopment - Multi File Development: Object Oriented Programming Development - Multi File Development By: Marc Conrad & Rob Manton University of Luton Email: Marc.Conrad@luton.ac.uk Rob.Manton@luton.ac.uk Room: D104Module Outline: Module Outline Introduction Non object oriented basics Classes Inheritance Aggregation Polymorphism Multifile Development Today’s lecture: Today’s lecture Polymorphism II recap Polymorphic Pointers Overriding Methods Static typing & Dynamic binding Multi File Development Referring to classes not yet defined .H and .CPP files LibrariesPolymorphic Pointers: Polymorphic Pointers In 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!Overriding Methods: Overriding Methods Three ways for a derived class to implement a polymorphic method Inherit it unchanged Replace with a different implementation Add to the existing implementationOverriding Methods: Overriding Methods Methods in the parent class can be redefined in the child class. class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; // ... Vehicle * vp = new Car(); vp->move(100); Valid C++ syntax!Overriding Methods: Overriding Methods Methods in the parent class can be redifined in the child class. class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; // ... Vehicle * vp = new Car(); vp->move(100); BUT: Which of these two move() methods will be called?Overriding Methods: Overriding Methods Methods in the parent class can be redifined in the child class. class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; // ... Vehicle * vp = new Car(); vp->move(100); static typing! In C++, static typing is the default behaviour. As vp is of type pointer to a Vehicle, the method of the Vehicle is called. Slide10: As vp is of type pointer to a Vehicle, the method of the Vehicle is called. Overriding Methods - The keyword virtual: Overriding Methods - The keyword virtual Methods in the parent class can be redefined in the child class. class 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 calledSlide13: Dynamic Binding: As vp points to a Car object the method of the Car is calledAbstract Methods & Classes: Abstract Methods & Classes Abstract methods are methods without any implementation (pure virtual methods). class 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).Slide15: Syntax for declaring abstract methods. We are not allowed to declare an object of an abstract type...Static typing & Dynamic binding: Static typing & Dynamic binding Static 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. 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). Why is dynamic binding useful?: Why is dynamic binding useful? If 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 mammal reptile human sheep crocodileWhy is dynamic binding useful?: Why is dynamic binding useful? Any method declared in the base class.. Animal mammal reptile human sheep crocodile MakeNoise()Why is dynamic binding useful?: MakeNoise() Why is dynamic binding useful? Any method declared in the base class... ..can be overridden by any derived class Animal mammal reptile human sheep crocodile MakeNoise() MakeNoise() MakeNoise() MakeNoise() MakeNoise()Why is dynamic binding useful?: MakeNoise() Why is dynamic binding useful? With 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 mammal reptile human sheep crocodile MakeNoise() MakeNoise() MakeNoise() MakeNoise() MakeNoise() Why is dynamic binding useful?: MakeNoise() Why is dynamic binding useful? With 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 mammal reptile human sheep crocodile virtual MakeNoise() MakeNoise() MakeNoise() MakeNoise() MakeNoise() Why is dynamic binding useful?: Why is dynamic binding useful? At compile time we can create a pointer to the base class and call a method defined in the base class. This should compile properly. At 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. Very useful for handling situations where the number or composition or lifespan of objects cannot be predicted at compile time Practical example: Practical example In 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()Practical example: Practical example Character Human Animal Old man Martial arts expert Alien spider Each derived class can override the base class methods and provide its own update() and draw() methodsPractical example: Practical example We don’t need to know at compile time how many of each type of character will exist at any time during the game We can have an array of pointers or a linked list or other container object to store a variable number of character objects Character * characters[50];Practical example: Practical example At run time we can allocate dynamically created objects of any of the classes in the hierarchy to the pointers eg characters[1]=new Alien(); characters[2]=new Spider(); characters[3]=new OldMan();Practical example: Practical example Because 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(); } }Practical example: Practical example Polymorphism in action a single method name being called from different types of object (old man, spider, martial arts expert, alien etc) Caters well with situations where number or composition or life time of objects can’t be predicted at compile time..Important stuff to remember: Important stuff to remember Overriding methods Polymorphic pointers Static binding Dynamic binding - the virtual keyword Abstract methodsReferring to classes not yet defined: Referring to classes not yet defined 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?Referring to classes not yet defined: Referring to classes not yet defined what 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 carReferring to classes not yet defined: Referring to classes not yet defined Person object not defined yet so we get an error messageReferring to classes not yet defined: Referring to classes not yet defined Solution 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 yetReferring to classes not yet defined: Referring to classes not yet defined Car has a pointer to person and person has a pointer to car forward declaration makes this legal!.H and .CPP files: .H and .CPP files On larger projects class definitions and implementations are usually split up definitions go into a .H file implementations into a .CPP file can do this automatically in the compiler with Insert/New Class.H and .CPP files: .H and .CPP files Name of class Optional base class Insert/New Class.H and .CPP files: .H and .CPP files .h and .cpp files now appear in project Skeleton for class definition Skeleton for class implementation.H and .CPP files: .H and .CPP files If we refer to a class defined in separate file we get an error .H and .CPP files: .H and .CPP files Need to #include the .h header file then all is OK! Using libraries: Using libraries Rapid application development - using existing modules or libraries rather than writing your own Ready made libraries like DirectX, OpenGL, Renderware etc comprise of a set of Header files (.H) and a set of library files (.LIB) A static library (.LIB) is a file containing objects that is linked into your program when the executable file is built. Using libraries: Using libraries Don’t need to recompile the library files each time - saves time Preserves security of source code (commercial reasons) User only needs to know about the interface to the classes which is defined in the header file follows C++ notion of separation of interface from implementationUsing libraries: Using libraries How do you make a library file? Static library is an option in the usual File/New Dialog Using libraries: Using libraries How do you use an existing library file? Insert both the .lib file and the.h header file into your project using Project/Add to project/Files Add 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 That’s all for today: That’s all for today First session after Christmas will be revision for the exam See you then and have a good one.. You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
week12 Cubemiddle Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINTLite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 143 Category: Entertainment License: All Rights Reserved Like it (1) Dislike it (0) Added: October 10, 2007 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript Object Oriented ProgrammingDevelopment - Multi File Development: Object Oriented Programming Development - Multi File Development By: Marc Conrad & Rob Manton University of Luton Email: Marc.Conrad@luton.ac.uk Rob.Manton@luton.ac.uk Room: D104Module Outline: Module Outline Introduction Non object oriented basics Classes Inheritance Aggregation Polymorphism Multifile Development Today’s lecture: Today’s lecture Polymorphism II recap Polymorphic Pointers Overriding Methods Static typing & Dynamic binding Multi File Development Referring to classes not yet defined .H and .CPP files LibrariesPolymorphic Pointers: Polymorphic Pointers In 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!Overriding Methods: Overriding Methods Three ways for a derived class to implement a polymorphic method Inherit it unchanged Replace with a different implementation Add to the existing implementationOverriding Methods: Overriding Methods Methods in the parent class can be redefined in the child class. class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; // ... Vehicle * vp = new Car(); vp->move(100); Valid C++ syntax!Overriding Methods: Overriding Methods Methods in the parent class can be redifined in the child class. class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; // ... Vehicle * vp = new Car(); vp->move(100); BUT: Which of these two move() methods will be called?Overriding Methods: Overriding Methods Methods in the parent class can be redifined in the child class. class Vehicle { void move(int i); }; class Car : public Vehicle { void move(int i); }; // ... Vehicle * vp = new Car(); vp->move(100); static typing! In C++, static typing is the default behaviour. As vp is of type pointer to a Vehicle, the method of the Vehicle is called. Slide10: As vp is of type pointer to a Vehicle, the method of the Vehicle is called. Overriding Methods - The keyword virtual: Overriding Methods - The keyword virtual Methods in the parent class can be redefined in the child class. class 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 calledSlide13: Dynamic Binding: As vp points to a Car object the method of the Car is calledAbstract Methods & Classes: Abstract Methods & Classes Abstract methods are methods without any implementation (pure virtual methods). class 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).Slide15: Syntax for declaring abstract methods. We are not allowed to declare an object of an abstract type...Static typing & Dynamic binding: Static typing & Dynamic binding Static 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. 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). Why is dynamic binding useful?: Why is dynamic binding useful? If 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 mammal reptile human sheep crocodileWhy is dynamic binding useful?: Why is dynamic binding useful? Any method declared in the base class.. Animal mammal reptile human sheep crocodile MakeNoise()Why is dynamic binding useful?: MakeNoise() Why is dynamic binding useful? Any method declared in the base class... ..can be overridden by any derived class Animal mammal reptile human sheep crocodile MakeNoise() MakeNoise() MakeNoise() MakeNoise() MakeNoise()Why is dynamic binding useful?: MakeNoise() Why is dynamic binding useful? With 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 mammal reptile human sheep crocodile MakeNoise() MakeNoise() MakeNoise() MakeNoise() MakeNoise() Why is dynamic binding useful?: MakeNoise() Why is dynamic binding useful? With 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 mammal reptile human sheep crocodile virtual MakeNoise() MakeNoise() MakeNoise() MakeNoise() MakeNoise() Why is dynamic binding useful?: Why is dynamic binding useful? At compile time we can create a pointer to the base class and call a method defined in the base class. This should compile properly. At 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. Very useful for handling situations where the number or composition or lifespan of objects cannot be predicted at compile time Practical example: Practical example In 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()Practical example: Practical example Character Human Animal Old man Martial arts expert Alien spider Each derived class can override the base class methods and provide its own update() and draw() methodsPractical example: Practical example We don’t need to know at compile time how many of each type of character will exist at any time during the game We can have an array of pointers or a linked list or other container object to store a variable number of character objects Character * characters[50];Practical example: Practical example At run time we can allocate dynamically created objects of any of the classes in the hierarchy to the pointers eg characters[1]=new Alien(); characters[2]=new Spider(); characters[3]=new OldMan();Practical example: Practical example Because 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(); } }Practical example: Practical example Polymorphism in action a single method name being called from different types of object (old man, spider, martial arts expert, alien etc) Caters well with situations where number or composition or life time of objects can’t be predicted at compile time..Important stuff to remember: Important stuff to remember Overriding methods Polymorphic pointers Static binding Dynamic binding - the virtual keyword Abstract methodsReferring to classes not yet defined: Referring to classes not yet defined 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?Referring to classes not yet defined: Referring to classes not yet defined what 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 carReferring to classes not yet defined: Referring to classes not yet defined Person object not defined yet so we get an error messageReferring to classes not yet defined: Referring to classes not yet defined Solution 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 yetReferring to classes not yet defined: Referring to classes not yet defined Car has a pointer to person and person has a pointer to car forward declaration makes this legal!.H and .CPP files: .H and .CPP files On larger projects class definitions and implementations are usually split up definitions go into a .H file implementations into a .CPP file can do this automatically in the compiler with Insert/New Class.H and .CPP files: .H and .CPP files Name of class Optional base class Insert/New Class.H and .CPP files: .H and .CPP files .h and .cpp files now appear in project Skeleton for class definition Skeleton for class implementation.H and .CPP files: .H and .CPP files If we refer to a class defined in separate file we get an error .H and .CPP files: .H and .CPP files Need to #include the .h header file then all is OK! Using libraries: Using libraries Rapid application development - using existing modules or libraries rather than writing your own Ready made libraries like DirectX, OpenGL, Renderware etc comprise of a set of Header files (.H) and a set of library files (.LIB) A static library (.LIB) is a file containing objects that is linked into your program when the executable file is built. Using libraries: Using libraries Don’t need to recompile the library files each time - saves time Preserves security of source code (commercial reasons) User only needs to know about the interface to the classes which is defined in the header file follows C++ notion of separation of interface from implementationUsing libraries: Using libraries How do you make a library file? Static library is an option in the usual File/New Dialog Using libraries: Using libraries How do you use an existing library file? Insert both the .lib file and the.h header file into your project using Project/Add to project/Files Add 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 That’s all for today: That’s all for today First session after Christmas will be revision for the exam See you then and have a good one..