Object Oriented Programming Development Week 4 z By

Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob. Manton@luton. ac. uk z Room: D 104 1

Module Outline z Introduction z The non object oriented basics z Classes z Design Approaches z Testing z Inheritance z Aggregation z Polymorphism z Multifile Development 2

Today: z. Functions recap z. Classes recap z. Object Persistence and Visibility. 3

Functions (not OO) void do. Something(); int main() { do. Something(); return 0; } void do. Something() { printf("Hello World!n"); }. Function declaration goes before main(). Function body goes afterwards or alternatively put these in a separate file and #include it. 4

Working with classes For small classes you can add the definition above main() and the implementation below, bu it is more usual to place them in separate files. . 5

Working with classes Definition file (. h) and implementation file (. cpp) are added to project automatically when you d Insert/New Class 6

The two steps of Object Oriented Programming z. Making Classes: Creating, extending or reusing abstract data types. z. Making Objects interact: Creating objects from abstract data types and defining their relationships. 7

Example: The Creature class Creature { private: int year. Of. Birth; public: void set. Year. Of. Birth(year) { year. Of. Birth = year; } int get. Year. Of. Birth() { return year. Of. Birth; } }; born 1997 8

Example: The Creature class Creature { private: int year. Of. Birth; public: void set. Year. Of. Birth(year) { year. Of. Birth = year; } int get. Year. Of. Birth() { return year. Of. Birth; } }; The definition of a class: • The class keyword, followed by the class name. • private attributes. • public methods. • the ; at the end 9

Example: The Creature class Creature { private: int year. Of. Birth; public: void set. Year. Of. Birth(year) { year. Of. Birth = year; } int get. Year. Of. Birth() { return year. Of. Birth; } }; This class has an attribute of type int. Note that each C++ data type and also abstract data types can be used as attribute types. 10

Example: The Creature class Creature { private: int year. Of. Birth; public: void set. Year. Of. Birth(year) { year. Of. Birth = year; } int get. Year. Of. Birth() { return year. Of. Birth; } }; This class has two (public) methods. One to set the attribute value and the other to retrieve the attribute value. 11

Example: The Creature class Creature { private: int year. Of. Birth; public: void set. Year. Of. Birth(year); int get. Year. Of. Birth(); }; void Creature: : set. Year. Of. Birth { year. Of. Birth = year; } int Creature: : get. Year. Of. Birth() { return year. Of. Birth; } Note that unless the methods are very short, declaration and implementation is usually separated. The declaration goes into a header file (. h), the implementation in a. cpp file. 12

Example: The Creature class Creature { private: int year. Of. Birth; public: void set. Year. Of. Birth(year) { year. Of. Birth = year; } int get. Year. Of. Birth() { return year. Of. Birth; } }; This method is an example for a ‘modifier’ method. It modifies the attribute. The method changes the state of the object. 13

Example: The Creature class Creature { private: int year. Of. Birth; public: void set. Year. Of. Birth(year) { year. Of. Birth = year; } int get. Year. Of. Birth() { return year. Of. Birth; } }; This method is an example for a ‘selector’ method. It returns information about the attribute but does not change the state of the object. 14

Classes & Objects z. What may be different for all objects in a class, and what remains the same? z. All the objects in a class may have different attribute values (state data), but their allowed behaviours are all the same. a s i s s s a t l c c e j a b o So r o f t n i r p blue 15

Objects & Classes z. A class is defined by: y. A Unique Name y. Attributes y. Methods z. An object is defined by: y. Identity y. State y. Behaviour 16

Instantiating Objects z. An object is instantiated just like any other data type: int x; char y; Creature z; Declaring z of type ‘creature’ means we have generated an object with the attributes and methods of the class. 17

Multiple Objects z. Of course we can create many objects of the same class: Creature my. Dog; Creature the. Milkman; Creature my. Best. Friend; Creates three objects. 18

Sending Messages / Calling Methods. z. A message is send to an object by calling a method of this object. Use the. (dot) for calling a method of an object. int k; k = the. Milkman. get. Year. Of. Birth(); my. Dog. set. Year. Of. Birth(1998); Messages are sent to my dog 19 and the milkman.

Back to the Instantiation. . . z. An object is instantiated just like any other data type: int x; char y; Creature z; Here the “default constructor” of the Creature class is automatically called. If we don’t like this we can specify constructors explicitly! 20

The Creature class with a user defined default constructor. class Creature { private: int year. Of. Birth; public: // … Creature() { year. Of. Birth = 1970; cout << “Hello. ”; } }; The syntax for a constructor is similar as for a method, but: • It has the same name as the class. • It has no return value. 21

The Creature with a parametrized constructor. class Creature { private: int year. Of. Birth; public: // … Creature(int year) { year. Of. Birth = year; } }; This constructor can be used as follows: Creature the. Milkman(1953); instantiates a 49 years old milkman. 22

The Creature with a copy constructor. Example: Creature my. Dog(1995); Creature my. Cat(my. Dog); class Creature { private: int year. Of. Birth; creates a cat of public: age as the dog. // … Creature(Creature & other. Creature) { year. Of. Birth = other. Creature. get. Year. Of. Birth(); } }; the same 23

Constructors - summary z. A constructor is always called when an object is created. z. We can define our own constructors (Note: a class can have more than one constructor). z. If an object is copied from another object then the copy constructor is called. 24

Classes exercise z. On paper for a change…. 25

Again: Objects & Classes z. A class is defined by: y. A Unique Name y. Attributes y. Methods z. An object is defined by: y. Identity y. State y. Behaviour 26

Again: Objects & Classes z. A class is defined by: y. A Unique Name y. Attributes y. Methods z. An object is defined by: y. Identity y. State y. Behaviour But: We can give a class state and behaviour with the keyword static! 27

Example: The Creature class Note that all objects share the same value of the “class attribute” number. Of. All. Creatures. class Creature { private: int year. Of. Birth; static int number. Of. All. Creatures = 0; public: Creature() { // Constructor - counts the creatures. number. Of. All. Creatures++; } static int get. Number. Of. All. Creatures() { return number. Of. All. Creatures; } }; 28

Last Week’s Summary. z A class is a blueprint for an object. z Objects are created similar to other data types (int, char, …). z The construction of an object can be defined by the user. z Messages are sent to an object by calling a method. z static messes the concept of classes and objects (but is nevertheless useful). 29

This week: types of object z. Four types of object (or any other data type) y. Automatic (local) objects y. External (global) objects y. Static objects y. Dynamic objects 30

Types of object z. Four types of object (or any other data type) y. Automatic (local) objects y. External (global) objects y. Static objects y. Dynamic objects First three are objects with specific names 31

Types of object z. Four types of object (or any other data type) When objects are predictable y. Automatic (local) objects enough to be y. External (global) objects identified at y. Static objects compile time y. Dynamic objects 32

Types of object z. Four types of object (or any other data type) y. Automatic (local) objects y. External (global) objects y. Static objects y. Dynamic objects No fixed unique name Identified by the memory address which they occupy 33

Types of object z. Four types of object (or any other data type) y. Automatic (local) objects y. External (global) objects y. Static objects y. Dynamic objects For objects that can’t be defined at compile time: their number or identity may vary at run time 34

Automatic objects z. Instantiated within the scope of a part of the program (between curly brackets somewhere) z. Automatically destroyed when object falls out of scope zvisible only within that scope (between when object declared and closing } ) 35

External (global) objects z. Persistent z. Visible throughout program module z. Instantiated outside any scope (curly brackets in C++) - usually at the top of your. cpp file zautomatically destroyed when program finishes zcan be referenced from other modules via 36 extern keyword

Static objects z. As mentioned last week z. Persistent for whole program - the same lifetime as an external (global) object useful to ‘remember’ state zscope as for automatic object zuses keyword static 37

Dynamic objects z. Useful where we can’t predict object identities, number or lifetimes. z. Created using the new keyword (you get a pointer to the object) z. Destroyed using the delete keyword z. Not destroyed automatically: You have to do it yourself!! 38

The lifetime of named objects (the first three) z. Automatic (local) objects exist while they are in scope z. External (global) objects have file scope and exist for the whole program z. Static objects - may be instantiated in local scope with local visibility but persist from their declaration to the end of the program 39

class header file creature. h class creature { private: int year. Of. Birth; public: creature(); virtual ~creature(); void set. Year. Of. Birth(int year); int get. Year. Of. Birth(); }; Private member variable (for encapsulation. . ) Public constructor with no parameters Modifier function (‘set’ something) Accessor function (‘get’ something) 40

class implementation file creature. cpp creature: : creature() { cout << "constructor called for creature class. " << endl; } Text message added to constructor and destructor to demonstrate when objects are created and destroyed creature: : ~creature() { cout << "destructor called for creature class. " << endl; } int creature: : get. Year. Of. Birth() { return year. Of. Birth; 41

Automatic objects int main() { cout << "beginning of main function. " << endl; creature my. Dog; cout << "end of main function. " << endl; return 0; } 42

Automatic objects With automatic object, object destroyed automatically when it goes out of scope (destructor gets called) 43

Automatic objects int main() { cout << "beginning of main function. " << endl; { creature my. Dog; } Automatic object now within local scope defined by the curly brackets cout << "end of main function. " << endl; return 0; cin; } 44

Automatic objects Because it is declared in local scope the automatic object is now automatically destroyed when it goes out of scope, which is before the end of 45 the main function

Automatic objects { creature my. Dog; my. Dog. set. Year. Of. Birth(1966); } 46

Automatic objects { creature my. Dog; my. Dog. set. Year. Of. Birth(1966); } This is legal. my. Dog is still in scope. 47

Automatic objects { creature my. Dog; } my. Dog. set. Year. Of. Birth(1966); 48

Automatic objects { creature my. Dog; } my. Dog. set. Year. Of. Birth(1966); This is not legal because my. Dog has gone out of scope (and been automatically destroyed) when the call to set. Year. Of. Birth() is made. 49

External (global) objects creature my. Dog; Object declared outside of any function or scope. int main() { cout << "beginning of main function. " << endl; This is legal because my. Dog. set. Year. Of. Birth(1966); object is visible throughout entire program cout << "end of main function. " << endl; return 0; } 50

External (global) objects With external (global) object, object is automatically destroyed when program ends 51

External (global) objects creature my. Dog; Not legal: can only call my. Dog. set. Year. Of. Birth(1966); methods and functions from within the braces of a function body int main() { cout << "beginning of main function. " << endl; cout << "end of main function. " << endl; return 0; } 52

External (global) objects When is it useful to have a global object or variable? z To make the object or variable visible in other source files within the same project z only externally declared objects can be referenced in other program modules (other. cpp files) z known as external linkage z use extern keyword 53

External (global) objects extern creature my. Dog; Say we have another class called person declared in person. h and implemented in person. cpp To refer to the my. Dog declared in the main file, we repeat the declaration creature my. Dog, but adding the extern keyword void person: : check. Dog() { int dob=my. Dog. get. Year. Of. Birth(); cout << "the dog was born in" << dob; } This calls the same creature object declared in the other program file 54

Static objects object: : object() This is part of the implementation file for a new class called object { which we use to demonstrate the value=0; difference between auto and static } objects. . . void object: : add. Value(int value_in) { value+=value_in; } int object: : get. Value() { return value; } 55

Static objects for (i=0; i<5; i++) { cout << "now on iteration number " << i <<endl; object auto_object; We compare two different types of object - the auto version and the auto_object. add. Value(1); static one. Both are created from the same class, the only difference static object static_object; is the use of the keyword static_object. add. Value(1); cout << "auto object contains " << auto_object. get. Value() << endl; cout << "static object contains " << static_object. get. Value() << endl; } 56

Static objects Auto object gets destroyed when it goes out of scope at end of each loop. Static object persists and doesn’t get destroyed when it goes out of scope 57

Static objects Auto object gets destroyed when it goes out of scope at end of each loop. Static objec persists and doesn’t get destroyed when it goes out of scope 58

Dynamic objects z. Create a pointer to the object type - good practice to initialize them to NULL zthen use the new operator to return a pointer to the newly created object zobject * my. Object. Ptr=NULL; zmy. Object. Ptr = new object( ); 59

Dynamic objects int main() { cout << "beginning of main function. " << endl; creature * p. Dog=NULL; p. Dog = new creature(); p. Dog is declared as a pointer to a creature object using creature * type. cout << "end of main function. " << endl; return 0; cin; } 60

Dynamic objects Note that there is no message from the destructor - the dynamic object is not automatically destroyed 61

Dynamic objects Note that there is no message from the destructor - the dynamic object is not automatically destroyed This is your job! You need to manually destroy the object using the delete command otherwise you will get a memory leak! 62

Dynamic objects int main() { cout << "beginning of main function. " << endl; creature * p. Dog=NULL; p. Dog = new creature(); delete p. Dog; Manually destroy dynamic object when you have finished it using the delete command 63

Dynamic objects Now we are destroying the dynamically generated object manually - very important!! 64

Dynamic objects z. How do you access the methods of a dynamic object? z. Use the -> operator instead of the dot (. ) syntax zp. Dog->set. Year. Of. Birth(1966); 65

Summary Automatic (local) objects z destroyed automatically when go out of scope z visible only within scope {} External (global) objects z destroyed automatically at end of program z visible throughout module z visible throughout other modules in program using extern keyword 66

Summary static objects z visible only within scope {} z persist throughout program Dynamic objects z get a pointer to the object z not automatically destroyed z your job to delete them otherwise you get a memory leak 67
![Summary #define NUMDOGS 5 int main() { creature * my. Dogs[NUMDOGS]; int i; for Summary #define NUMDOGS 5 int main() { creature * my. Dogs[NUMDOGS]; int i; for](http://slidetodoc.com/presentation_image_h2/765c487eed77bc90bb883c601f2c5b15/image-68.jpg)
Summary #define NUMDOGS 5 int main() { creature * my. Dogs[NUMDOGS]; int i; for (i=0; i<NUMDOGS; i++) { my. Dogs[i]=NULL; my. Dogs[i]=new creature(); my. Dogs[i]->set. Year. Of. Birth(1970+i); } for (i=0; i<NUMDOGS; i++) { cout << "dog number "<<i<<" was born in "<<my. Dogs[i]->get. Year. Of. Birth() << endl; } for (i=0; i<NUMDOGS; i++) { delete my. Dogs[i]; } 68 return 0;

Summary 69

Summary Automatic/external/static objects z Have a unique name z Useful when objects are predictable enough to be identified at compile time Dynamic objects z No fixed unique name z Identified by the memory address which they occupy z For objects that can’t be defined at compile time: their number or identity may vary at run time 70
- Slides: 70