/
CMPT 225 Object Oriented Programming CMPT 225 Object Oriented Programming

CMPT 225 Object Oriented Programming - PowerPoint Presentation

quinn
quinn . @quinn
Follow
65 views
Uploaded On 2023-09-26

CMPT 225 Object Oriented Programming - PPT Presentation

Outline OOP Basic Principles C Classes September 2004 John Edgar 2 Examples Colours How should we work with colours How should we store them How should we modify or operate on them ID: 1021422

2004john class methods object class 2004john object methods variables constructor values copy classes creates colours file implementation data september

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CMPT 225 Object Oriented Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. CMPT 225Object Oriented Programming

2. OutlineOOP Basic PrinciplesC++ ClassesSeptember 2004John Edgar2

3. ExamplesColoursHow should we work with colours?How should we store them?How should we modify or operate on them?Linked listsHow should we provide the functionality of a linked list?Shapes…September 2004John Edgar3

4. OOP Principles

5. OOP PrinciplesEncapsulationColor ClassDesigning ClassesSeptember 2004John Edgar5

6. Representing ColourLet's say we need to represent colours There are many different colour modelsOne such is the RGB (red green blue) modelRGB coloursA colour is represented by three numbers, which represent the amount of red, green and blueThese values are sometimes recorded as doubles (between 0.0 and 1.0) or sometimes asIntegers, between 0 and 255 (or some other number)How many colours can be represented?September 2004John Edgar6

7. Colours and rgb ValuesSeptember 2004John Edgar7255,0,00,0,2550,255,00,0,0255,128,0255,255,255128,128,128128,128,192

8. Storing Colour DataWe need three variables to represent one colourIt would be convenient to refer to colours in the same way we refer to primitive typesObject Oriented Programming (OOP) organizes programs to collect variables and methodsA class is a factory (or blueprint) for creating objects of a particular typeAn object is a collection of variables and methods, and is an instantiation of a classColor * c = new Color();September 2004John Edgar8classpointer to objectconstructor

9. EncapsulationAn object combines both variables and methods in the same constructVariables give the structure of an objectMethods dictate its behaviourA class should be a cohesive construct that performs one task (or set of related tasks) wellObjects can be used as if they were primitive typesTo encapsulate means to encase or encloseEach object should protect and manage its own information, hiding the inner detailsObjects should interact with the rest of the system only through a specific set of methods (its public interface)September 2004John Edgar9

10. Classes and ObjectsThe class describes the data and operationsFor colours these include:Attributes for red, green and blueMethods to access and change and create coloursAn individual object is an instance of a classSimilar to the way that a variable is of a typeEach object has its own space in memory, and therefore each object has its own stateIndividual Color objects represent individual colours, each with their own values for red, green and blueSeptember 2004John Edgar10

11. Information HidingTo achieve loose coupling, classes are only allowed to communicate through their interfacesThereby hiding their implementations detailsLoose coupling is desirable as it:Decreases the chance that changing one module's implementation causes changes to other modulesPrevents other modules from assigning invalid values to attributesInformation hiding is relatively easy to achieve using object oriented programming September 2004John Edgar11

12. Designing a ClassThere are many ways to design classes, as the purpose of classes differs widelyClasses may store data and require operations to support this, orMay implement an algorithm, orCombine both data and operationsThe initial focus may either be on a class's variables or its methodsThere are, however, some general principles of good designSeptember 2004John Edgar12

13. Design: Make Variables PrivateVariables should generally be made directly inaccessible from outside the classThis is achieved by making them privateThe values of variables can be accessed using getter methods (or accessors)New values can be assigned to variables using setter methods (or mutators)A setter method assigns the value passed to its parameter to a variableWhile protecting any class invariantsSeptember 2004John Edgar13

14. Design: Write ConstructorsConstructors should initialize all of the variables in an objectIt is often necessary to write more than one constructorDefault constructor, with no parameters that assigns default values to variablesConstructor with parameters for each variable, that assigns the parameter values to those variablesCopy constructor that takes an object of the same class and creates a copy of itSeptember 2004John Edgar14

15. Design: Make Helper Methods PrivateHelper methods are methods that assist class methods in performing their tasksHelper methods are often created to implement part of a complex task or toPerform sub-tasks that are required by more than one class methodsThey are therefore only useful to the class and should not be visible outside the classHelper methods only relate to the implementation of a class, and should not be made part of the interfaceSeptember 2004John Edgar15

16. Design: Setters Only When Needed Class variables are made privateTo prevent them from being assigned inappropriate values, andTo prevent classes from depending on each others' implementations andConsider whether or not each variable requires a setter methodIs it more appropriate to create a new object rather than changing an existing object's variables?Setters should always respect class invariantsSeptember 2004John Edgar16

17. C++ Classes

18. Basic C++ ClassesEvery C++ class should be divided into header and implementation filesThe header file contains the class definitionThe implementation file contains the definiton of class methodsThe implementation file has a .cpp extensionAnd should contain the definition of each method declared in the header fileEach method name must be preceded by the class name and “::”September 2004John Edgar18

19. C++ Header FilesThe header file has a .h extension and containsClass definition (class keyword and class name)Class variablesMethod declarations (not definitions) forConstructors, a destructor, getters and setters as necessary, and any other methods that are requiredThe class should be divided into public and private sections as necessarySeptember 2004John Edgar19

20. // Thing.hclass Thing{public: Thing(); Thing(int startAge); //copy constructor and destructor // made by the compiler void display();private: int age; //the one and only attribute};Basic C++ Classes, .hJohn Edgar20the file is divided into public and private sectionsconstructors have the same name as the class and do not have a return typenote the semi-colons

21. // Thing.cpp#include "thing.h"#include <iostream>using namespace std;Thing::Thing(){ age = 0;}//default constructorThing::Thing(int startAge){ age = startAge;}//constructorvoid Thing::display(){ cout << age << endl;}//displayBasic C++ Classes, .cppJohn Edgar21the file contains method definitions for each methodIf a method is not preceded by the class name and :: it is not an implementation of a class methodomitting Thing:: from a method name may not result in a compiler error

22. C++ ConstructorsIf no constructor exists for a class the C++ compiler creates a default constructorCreating any constructor prevents this default from being createdIf no copy constructor exists C++ creates oneThis copy constructor makes a shallow copyIt only copies the values of data members; which, for pointers, are addresses, and not the dynamically allocated dataIf the class uses dynamically allocated memory a copy constructor that performs a deep copy must be writtenSeptember 2004John Edgar22

23. C++ DestructorsEvery C++ class must have a destructor which is responsible for destroying a class instance~Thing(); //tilde specifies destructor A class can have only one destructorC++ automatically creates a destructor for a class if one has not been writtenIf a class does not use dynamically allocated memory it can depend on the compiler generated destructorOtherwise a destructor must be written to deallocate any dynamically allocated memory, using deleteSeptember 2004John Edgar23

24. Objects in Stack (Static) MemoryUnlike Java C++ objects do not have to be created in dynamic memoryThing th; creates a new Thing object in stack memoryAnd calls the default constructorThing th(3); would call the second constructorSeptember 2004John Edgar24

25. Copying ObjectsSeptember 2004John Edgar25

26. Shallow CopiesConsider a copy constructor for a Linked ListLinkedList::LinkedList(LinkedList& ll){head = ll.head;}This constructor has not created a new list, it has just created a new pointer to the existing listThere is still only one listThis is an example of a shallow copyWhere only the references are copied, and not the underlying data in dynamic memorySeptember 2004John Edgar26

27. Deep CopiesA deep copy creates a copy of an object's data and not just its pointersBy creating a new object in dynamic memory for each such object in the originalFor a linked list this would mean traversing the list making a new node for each original nodeDeep copies are required whenever a class allocates space in dynamic memoryThat is, creates objects using newLab 3 will demonstrate this conceptSeptember 2004John Edgar27

28. SummarySeptember 2004John Edgar28

29. SummaryObject-oriented programmingEncapsulation, information hidingC++ classes.h file to specify methods/variables, .cpp for detailsObjects can be created in heap (dynamic) or stack (static) memorySeptember 2004John Edgar29

30. ReadingsCarranoCh. 8September 2004John Edgar30