Title: Object Oriented Programming Development - Week 4
1Object Oriented ProgrammingDevelopment - Week 4
- By
- Rob Manton
- University of Luton
- Email
- Rob.Manton_at_luton.ac.uk
- Room
- D104
2Module Outline
- Introduction
- The non object oriented basics
- Classes
- Design Approaches
- Testing
- Inheritance
- Aggregation
- Polymorphism
- Multifile Development
3Today
- Functions recap
- Classes recap
- Objects recap
- Object Persistence and Visibility.
4Functions (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.
5Working with classes
- For small classes you can add the definition
above main() and the implementation below, but it
is more usual to place them in separate files..
6Working with classes
- Definition file (.h) and implementation file
(.cpp) are added to project automatically when
you do Insert/New Class
7The 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.
8Example The Creature class
- class Creature
- private
- int yearOfBirth
- public
- void setYearOfBirth(year)
- yearOfBirth year
-
- int getYearOfBirth()
- return yearOfBirth
-
born1997
9Example The Creature class
- class 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
10Example The Creature class
- class 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.
11Example The Creature class
- class 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.
12Example The Creature class
- class Creature
- private
- int yearOfBirth
- public
- void setYearOfBirth(year)
- int getYearOfBirth()
-
- void CreaturesetYearOfBirth
- yearOfBirth year
-
- int CreaturegetYearOfBirth()
- 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.
13Example The Creature class
- class 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.
14Example The Creature class
- class 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.
15Classes 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
16Objects Classes
- A class is defined by
- A Unique Name
- Attributes
- Methods
- An object is defined by
- Identity
- State
- Behaviour
17Instantiating 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.
18Multiple Objects
- Of course we can create many objects of the same
class - Creature myDog
- Creature theMilkman
- Creature myBestFriend
Creates three objects.
19Sending 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.
20Back 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 dont like
this we can specify constructors explicitly!
21The Creature class with a user defined default
constructor.
- class Creature
- private
- int yearOfBirth
- public
- //
- Creature()
- yearOfBirth 1970
- cout ltlt 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.
22The 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.
23The Creature with a copy constructor.
Example Creature myDog(1995) Creature
myCat(myDog) creates a cat of the same age as
the dog.
- class Creature
- private
- int yearOfBirth
- public
- //
- Creature(Creature otherCreature)
- yearOfBirth
- otherCreature.getYearOfBirth()
-
24Constructors - 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.
25Classes exercise
26Again Objects Classes
- A class is defined by
- A Unique Name
- Attributes
- Methods
- An object is defined by
- Identity
- State
- Behaviour
27Again 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!
28Example 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.
29Last Weeks 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).
30This weektypes of object
- Four types of object (or any other data type)
- Automatic (local) objects
- External (global) objects
- Static objects
- Dynamic objects
31Types 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
32Types 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
33Types of object
- Four types of object (or any other data type)
- Automatic (local) objects
- External (global) objects
- Static objects
- Dynamic objects
No fixed unique name Identified by the memory
address which they occupy
34Types of object
- Four types of object (or any other data type)
- Automatic (local) objects
- External (global) objects
- Static objects
- Dynamic objects
For objects that cant be defined at compile
time their number or identity may vary at run
time
35Automatic 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 )
36External (global) objects
- Persistent
- Visible throughout program module
- Instantiated 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
37Static objects
- As mentioned last week
- Persistent for whole program - the same lifetime
as an external (global) object - useful to
remember state - scope as for automatic object
- uses keyword static
38Dynamic objects
- Useful where we cant predict object identities,
number or lifetimes. - Created using the new keyword (you get a pointer
to the object) - Destroyed using the delete keyword
- Not destroyed automatically You have to do it
yourself!!
39The 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
40class 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
Modifier function (set something)
Accessor function (get something)
41class implementation file creature.cpp
- creaturecreature()
-
- cout ltlt "constructor called for creature class."
ltlt endl -
- creaturecreature()
-
- cout ltlt "destructor called for creature class."
ltlt endl -
- int creaturegetYearOfBirth()
-
- return yearOfBirth
-
- void creaturesetYearOfBirth(int year)
-
- yearOfBirth year
Text message added to constructor and destructor
to demonstrate when objects are created and
destroyed
42Automatic objects
- int main()
-
- cout ltlt "beginning of main function." ltlt endl
-
- creature myDog
- cout ltlt "end of main function." ltlt endl
- return 0
43Automatic objects
With automatic object, object destroyed
automatically when it goes out of scope
(destructor gets called)
44Automatic objects
- int main()
-
- cout ltlt "beginning of main function." ltlt endl
-
-
- creature myDog
-
- cout ltlt "end of main function." ltlt endl
- return 0
- cin
Automatic object now within local scope defined
by the curly brackets
45Automatic 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
46Automatic objects
-
- creature myDog
- myDog.setYearOfBirth(1966)
-
47Automatic objects
-
- creature myDog
- myDog.setYearOfBirth(1966)
-
This is legal. myDog is still in scope.
48Automatic objects
-
- creature myDog
-
- myDog.setYearOfBirth(1966)
49Automatic 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.
50External (global) objects
- creature myDog
- int main()
-
- cout ltlt "beginning of main function." ltlt endl
- myDog.setYearOfBirth(1966)
- cout ltlt "end of main function." ltlt endl
- return 0
Object declared outside of any function or scope.
This is legal because object is visible
throughout entire program
51External (global) objects
With external (global) object, object is
automatically destroyed when program ends
52External (global) objects
- creature myDog
- myDog.setYearOfBirth(1966)
- int main()
-
- cout ltlt "beginning of main function." ltlt endl
- cout ltlt "end of main function." ltlt endl
- return 0
Not legal can only call methods and functions
from within the braces of a function body
53External (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
54External (global) objects
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
- extern creature myDog
- void personcheckDog()
-
- int dobmyDog.getYearOfBirth()
- cout ltlt "the dog was born in" ltlt dob
-
This calls the same creature object declared in
the other program file
55Static objects
- objectobject()
-
- value0
-
- void objectaddValue(int value_in)
-
- valuevalue_in
-
- int objectgetValue()
-
- 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...
56Static objects
- for (i0ilt5i)
- cout ltlt "now on iteration number " ltlt i
ltltendl - object auto_object
- auto_object.addValue(1)
-
- static object static_object
- static_object.addValue(1)
- cout ltlt "auto object contains " ltlt
auto_object.getValue() ltlt endl - cout ltlt "static object contains " ltlt
static_object.getValue() ltlt 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
57Static objects
Auto object gets destroyed when it goes out of
scope at end of each loop. Static object persists
and doesnt get destroyed when it goes out of
scope
58Static objects
Auto object gets destroyed when it goes out of
scope at end of each loop. Static object persists
and doesnt get destroyed when it goes out of
scope
59Dynamic 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 myObjectPtrNULL
- myObjectPtr new object( )
60Dynamic objects
- int main()
-
- cout ltlt "beginning of main function." ltlt endl
- creature pDogNULL
- pDog new creature()
- cout ltlt "end of main function." ltlt endl
- return 0
- cin
-
pDog is declared as a pointer to a creature
object using creature type.
61Dynamic objects
Note that there is no message from the destructor
- the dynamic object is not automatically
destroyed
62Dynamic objects
This is your job! You need to manually destroy
the object using the delete command otherwise you
will get a memory leak!
Note that there is no message from the destructor
- the dynamic object is not automatically
destroyed
63Dynamic objects
- int main()
-
- cout ltlt "beginning of main function." ltlt endl
- creature pDogNULL
- pDog new creature()
-
- delete pDog
-
Manually destroy dynamic object when you have
finished it using the delete command
64Dynamic objects
Now we are destroying the dynamically generated
object manually - very important!!
65Dynamic objects
- How do you access the methods of a dynamic
object? - Use the -gt operator instead of the dot (.) syntax
- pDog-gtsetYearOfBirth(1966)
66Summary
- Automatic (local) objects
- destroyed automatically when go out of scope
- visible only within scope
- External (global) objects
- destroyed automatically at end of program
- visible throughout module
- visible throughout other modules in program using
extern keyword
67Summary
- static objects
- visible only within scope
- persist throughout program
- Dynamic objects
- get a pointer to the object
- not automatically destroyed
- your job to delete them otherwise you get a
memory leak
68Summary
- define NUMDOGS 5
- int main()
- creature myDogsNUMDOGS
- int i
- for (i0iltNUMDOGSi)
-
- myDogsiNULL
- myDogsinew creature()
- myDogsi-gtsetYearOfBirth(1970i)
-
- for (i0iltNUMDOGSi)
-
- cout ltlt "dog number "ltltiltlt" was born in
"ltltmyDogsi-gtgetYearOfBirth() ltlt endl -
- for (i0iltNUMDOGSi)
-
- delete myDogsi
-
- return 0
69Summary
70Summary
- 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 cant be defined at compile
time their number or identity may vary at run
time