![Page 1: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/1.jpg)
1
Object Oriented ProgrammingDevelopment - Week 4
By: Rob MantonUniversity of Luton
Email: [email protected]: D104
![Page 2: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/2.jpg)
2
Module Outline
IntroductionThe non object
oriented basicsClassesDesign ApproachesTesting
InheritanceAggregationPolymorphismMultifile
Development
![Page 3: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/3.jpg)
3
Today:
Functions recap
Classes recap
Objects recap
Object Persistence and Visibility.
![Page 4: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/4.jpg)
4
Functions (not OO)
void doSomething();
int main(){
doSomething();return 0;
}void doSomething(){
printf("Hello World!\n");}.
Function declaration goes before main(). Function body goes afterwards or alternatively put these in a separate file and #include it.
![Page 5: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/5.jpg)
5
Working with classesFor small classes you can add the definition above main() and the implementation below, but it is more usual to place them in separate files..
![Page 6: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/6.jpg)
6
Working with classes
Definition file (.h) and implementation file (.cpp) are added to project automatically when you do Insert/New Class
![Page 7: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/7.jpg)
7
The two steps of Object Oriented Programming
Making Classes: Creating, extending or reusing abstract data types.
Making Objects interact: Creating objects from abstract data types and defining their relationships.
![Page 8: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/8.jpg)
Example: The Creature classclass Creature { private: int yearOfBirth;public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } };
born1997
![Page 9: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/9.jpg)
Example: The Creature classclass Creature {
private:
int yearOfBirth;
public:
void setYearOfBirth(year) {
yearOfBirth = year;
}
int getYearOfBirth() {
return yearOfBirth;
}
};
The definition of a class:•The class keyword, followed by the class name.•private attributes.•public methods.•the ; at the end
![Page 10: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/10.jpg)
Example: The Creature classclass Creature {
private:
int yearOfBirth;
public:
void setYearOfBirth(year) {
yearOfBirth = year;
}
int getYearOfBirth() {
return yearOfBirth;
}
};
This class has anattribute of typeint. Note that each C++data type and also abstract data types can be used as attribute types.
![Page 11: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/11.jpg)
Example: The Creature classclass Creature {
private:
int yearOfBirth;
public:
void setYearOfBirth(year) {
yearOfBirth = year;
}
int getYearOfBirth() {
return yearOfBirth;
}
};
This class has two (public) methods. One to set the attribute value and the other to retrieve the attribute value.
![Page 12: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/12.jpg)
Example: The Creature classclass Creature { private: int yearOfBirth;public: void setYearOfBirth(year); int getYearOfBirth();};void Creature::setYearOfBirth { yearOfBirth = year; }int Creature::getYearOfBirth() { return yearOfBirth; }
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.
![Page 13: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/13.jpg)
Example: The Creature classclass Creature { private: int yearOfBirth;public: void setYearOfBirth(year)
{ yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } };
This method is an example for a ‘modifier’ method. It modifies the attribute. The method changes the state of the object.
![Page 14: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/14.jpg)
Example: The Creature classclass Creature { private: int yearOfBirth;public: void setYearOfBirth(year)
{ yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } };
This method is an example for a ‘selector’ method. It returns information about the attribute but does not change the state of the object.
![Page 15: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/15.jpg)
15
Classes & Objects
What may be different for all objects in a class, and what remains the same?
All the objects in a class may have different attribute values (state data), but their allowed behaviours are all the same.
So a class is a
blueprint for objects
![Page 16: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/16.jpg)
16
Objects & Classes
A class is defined by:
A Unique Name
Attributes Methods
An object is defined by:
Identity State Behaviour
![Page 17: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/17.jpg)
17
Instantiating Objects
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.
![Page 18: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/18.jpg)
18
Multiple Objects
Of course we can create many objects of the same class:
Creature myDog;Creature theMilkman;Creature myBestFriend;
Creates three objects.
![Page 19: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/19.jpg)
19
Sending Messages / Calling Methods.
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 = theMilkman.getYearOfBirth();myDog.setYearOfBirth(1998);
Messages are sent to my dog
and the milkman.
![Page 20: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/20.jpg)
20
Back to the Instantiation...
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!
![Page 21: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/21.jpg)
The Creature class with a user defined default constructor.class Creature { private: int yearOfBirth;public: // … Creature() { yearOfBirth = 1970; cout << “Hello.”; } };
The syntax for a constructoris similar as for a method, but:•It has the same name as the class.•It has no return value.
![Page 22: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/22.jpg)
The Creature with a parametrized constructor.class Creature { private: int yearOfBirth;public: // … Creature(int year) { yearOfBirth = year; } };
This constructor can be used as follows:
Creature theMilkman(1953);
instantiates a 49 years old milkman.
![Page 23: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/23.jpg)
The Creature with a copy constructor.
class Creature { private: int yearOfBirth;public: // … Creature(Creature & otherCreature) { yearOfBirth = otherCreature.getYearOfBirth(); } };
Example:Creature myDog(1995);Creature myCat(myDog);
creates a cat of the same age as the dog.
![Page 24: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/24.jpg)
24
Constructors - summary
A constructor is always called when an object is created.
We can define our own constructors (Note: a class can have more than one constructor).
If an object is copied from another object then the copy constructor is called.
![Page 25: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/25.jpg)
25
Classes exercise
On paper for a change….
![Page 26: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/26.jpg)
26
Again: Objects & Classes
A class is defined by:
A Unique Name
Attributes Methods
An object is defined by:
Identity State Behaviour
![Page 27: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/27.jpg)
27
Again: Objects & Classes
A class is defined by:
A Unique Name
Attributes Methods
An object is defined by:
Identity State Behaviour
But: We can give a class state and behaviour with the keyword static!
![Page 28: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/28.jpg)
Example: The Creature class
class Creature { private: int yearOfBirth;
static int numberOfAllCreatures = 0;public: Creature() { // Constructor - counts the creatures. numberOfAllCreatures++; } static int getNumberOfAllCreatures() { return numberOfAllCreatures; }};
Note that all objects share the same value of the “class attribute” numberOfAllCreatures.
![Page 29: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/29.jpg)
29
Last Week’s Summary.
A class is a blueprint for an object.Objects are created similar to other data
types (int, char, …).The construction of an object can be
defined by the user.Messages are sent to an object by calling
a method.static messes the concept of classes and
objects (but is nevertheless useful).
![Page 30: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/30.jpg)
30
This week:types of object
Four types of object (or any other data type)
Automatic (local) objects External (global) objects Static objects Dynamic objects
![Page 31: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/31.jpg)
31
Types of object
Four types of object (or any other data type)
Automatic (local) objects External (global) objects Static objects Dynamic objects
First three are objects with
specific names
![Page 32: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/32.jpg)
32
Types of object
Four types of object (or any other data type)
Automatic (local) objects External (global) objects Static objects Dynamic objects
When objects are predictable enough to be identified at compile time
![Page 33: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/33.jpg)
33
Types of object
Four types of object (or any other data type)
Automatic (local) objects External (global) objects Static objects Dynamic objects
No fixed unique nameIdentified by the memory
address which they occupy
![Page 34: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/34.jpg)
34
Types of object
Four types of object (or any other data type)
Automatic (local) objects External (global) objects Static objects Dynamic objects
For objects that can’t be
defined at compile time: their number
or identity may vary at run
time
![Page 35: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/35.jpg)
35
Automatic objects
Instantiated within the scope of a part of the program (between curly brackets somewhere)
Automatically destroyed when object falls out of scope
visible only within that scope (between when object declared and closing } )
![Page 36: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/36.jpg)
36
External (global) objects
PersistentVisible throughout program moduleInstantiated outside any scope (curly
brackets in C++) - usually at the top of your .cpp file
automatically destroyed when program finishes
can be referenced from other modules via extern keyword
![Page 37: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/37.jpg)
37
Static objects
As mentioned last weekPersistent for whole program - the
same lifetime as an external (global) object - useful to ‘remember’ state
scope as for automatic objectuses keyword static
![Page 38: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/38.jpg)
38
Dynamic objects
Useful where we can’t predict object identities, number or lifetimes.
Created using the new keyword (you get a pointer to the object)
Destroyed using the delete keywordNot destroyed automatically: You
have to do it yourself!!
![Page 39: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/39.jpg)
39
The lifetime of named objects (the first three)
Automatic (local) objects exist while they are in scope
External (global) objects have file scope and exist for the whole program
Static objects - may be instantiated in local scope with local visibility but persist from their declaration to the end of the program
![Page 40: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/40.jpg)
40
class header file creature.h
class creature{private:
int yearOfBirth;
public:creature();virtual ~creature();void setYearOfBirth(int year); int getYearOfBirth();
};
Private member variable (for encapsulation..)
Public constructor with no parameters
Accessor function (‘get’ something)
Modifier function (‘set’ something)
![Page 41: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/41.jpg)
41
class implementation file creature.cpp
creature::creature(){
cout << "constructor called for creature class." << endl;}
creature::~creature(){
cout << "destructor called for creature class." << endl;}
int creature::getYearOfBirth(){
return yearOfBirth;}void creature::setYearOfBirth(int year){
yearOfBirth = year;}
Text message added to constructor and destructor to demonstrate when objects are
created and destroyed
![Page 42: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/42.jpg)
42
Automatic objects
int main(){
cout << "beginning of main function." << endl;
creature myDog;
cout << "end of main function." << endl;return 0;
}
![Page 43: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/43.jpg)
43
Automatic objects
With automatic object, object
destroyed automatically when it goes out of scope
(destructor gets called)
![Page 44: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/44.jpg)
44
Automatic objects
int main(){
cout << "beginning of main function." << endl;
{creature myDog;}
cout << "end of main function." << endl;return 0;cin;
}
Automatic object now within local scope defined by the curly brackets
![Page 45: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/45.jpg)
45
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 the main
function
![Page 46: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/46.jpg)
46
Automatic objects
{creature myDog;myDog.setYearOfBirth(1966);}
![Page 47: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/47.jpg)
47
Automatic objects
{creature myDog;myDog.setYearOfBirth(1966);}
This is legal. myDog is still in scope.
![Page 48: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/48.jpg)
48
Automatic objects
{creature myDog;}
myDog.setYearOfBirth(1966);
![Page 49: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/49.jpg)
49
Automatic objects
{creature myDog;}
myDog.setYearOfBirth(1966);This is not legal because myDog has gone out of
scope (and been automatically destroyed)
when the call to setYearOfBirth() is made.
![Page 50: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/50.jpg)
50
External (global) objects
creature myDog;
int main(){
cout << "beginning of main function." << endl;
myDog.setYearOfBirth(1966);
cout << "end of main function." << endl;return 0;
}
Object declared outside of any function or scope.
This is legal because object is visible throughout
entire program
![Page 51: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/51.jpg)
51
External (global) objects
With external (global) object,
object is automatically
destroyed when program ends
![Page 52: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/52.jpg)
52
External (global) objects
creature myDog;myDog.setYearOfBirth(1966);
int main(){
cout << "beginning of main function." << endl; cout << "end of main function." << endl;
return 0;}
Not legal: can only call methods and functions
from within the braces of a function body
![Page 53: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/53.jpg)
53
External (global) objects
When is it useful to have a global object or variable?
To make the object or variable visible in other source files within the same project
only externally declared objects can be referenced in other program modules (other .cpp files)
known as external linkage use extern keyword
![Page 54: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/54.jpg)
54
External (global) objects
extern creature myDog;
void person::checkDog(){int dob=myDog.getYearOfBirth();cout << "the dog was born in" << dob;}
Say we have another class called person declared in
person.h and implemented in person.cpp
To refer to the myDog declared in the main file, we
repeat the declaration creature myDog, but adding
the extern keyword
This calls the same creature object declared in the other
program file
![Page 55: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/55.jpg)
55
Static objects
object::object(){value=0;}void object::addValue(int value_in){
value+=value_in;}int object::getValue(){
return value;}
This is part of the implementation file for a new class called object which we
use to demonstrate the difference between auto and
static objects...
![Page 56: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/56.jpg)
56
Static objects
for (i=0;i<5;i++){ cout << "now on iteration number " << i <<endl;
object auto_object;auto_object.addValue(1);
static object static_object;static_object.addValue(1);cout << "auto object contains " << auto_object.getValue() << endl;
cout << "static object contains " << static_object.getValue() << endl;}
}
We compare two different types of object - the auto
version and the static one. Both are created from the
same class, the only difference is the use of the
keyword static
![Page 57: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/57.jpg)
57
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
![Page 58: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/58.jpg)
58
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
![Page 59: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/59.jpg)
59
Dynamic objects
Create a pointer to the object type - good practice to initialize them to NULL
then use the new operator to return a pointer to the newly created object
object * myObjectPtr=NULL;myObjectPtr = new object( );
![Page 60: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/60.jpg)
60
Dynamic objects
int main(){
cout << "beginning of main function." << endl;
creature * pDog=NULL;pDog = new creature();
cout << "end of main function." << endl;return 0;cin;
}
pDog is declared as a pointer to a creature object using
creature * type.
![Page 61: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/61.jpg)
61
Dynamic objects
Note that there is no message from the
destructor - the dynamic object is not automatically
destroyed
![Page 62: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/62.jpg)
62
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!
![Page 63: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/63.jpg)
63
Dynamic objects
int main(){
cout << "beginning of main function." << endl;
creature * pDog=NULL;pDog = new creature();
delete pDog;Manually destroy dynamic
object when you have finished it using the delete command
![Page 64: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/64.jpg)
64
Dynamic objects
Now we are destroying the dynamically
generated object manually - very
important!!
![Page 65: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/65.jpg)
65
Dynamic objects
How do you access the methods of a dynamic object?
Use the -> operator instead of the dot (.) syntax
pDog->setYearOfBirth(1966);
![Page 66: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/66.jpg)
66
Summary
Automatic (local) objectsdestroyed automatically when go out of scopevisible only within scope {}
External (global) objectsdestroyed automatically at end of programvisible throughout modulevisible throughout other modules in program
using extern keyword
![Page 67: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/67.jpg)
67
Summary
static objectsvisible only within scope {}persist throughout program
Dynamic objectsget a pointer to the object not automatically destroyedyour job to delete them otherwise you get a
memory leak
![Page 68: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/68.jpg)
68
Summary
#define NUMDOGS 5int main(){ creature * myDogs[NUMDOGS];
int i;for (i=0;i<NUMDOGS;i++){
myDogs[i]=NULL;myDogs[i]=new creature();myDogs[i]->setYearOfBirth(1970+i);
}for (i=0;i<NUMDOGS;i++){
cout << "dog number "<<i<<" was born in "<<myDogs[i]->getYearOfBirth() << endl;}for (i=0;i<NUMDOGS;i++){
delete myDogs[i];}return 0;
}
![Page 69: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/69.jpg)
69
Summary
![Page 70: 1 Object Oriented Programming Development - Week 4 z By: Rob Manton University of Luton z Email: Rob.Manton@luton.ac.uk z Room: D104](https://reader036.vdocuments.mx/reader036/viewer/2022062422/56649e855503460f94b88035/html5/thumbnails/70.jpg)
70
Summary
Automatic/external/static objects Have a unique name Useful when objects are predictable enough to be
identified at compile time
Dynamic objects No fixed unique name Identified by the memory address which they occupy For objects that can’t be defined at compile time:
their number or identity may vary at run time