700 likes | 826 Views
By: Rob Manton University of Luton Email: Rob.Manton@luton.ac.uk Room: D104. Object Oriented Programming Development - Week 4. Introduction The non object oriented basics Classes Design Approaches Testing. Inheritance Aggregation Polymorphism
E N D
By: Rob Manton University of Luton Email: Rob.Manton@luton.ac.uk Room: D104 Object Oriented ProgrammingDevelopment - Week 4
Introduction The non object oriented basics Classes Design Approaches Testing Inheritance Aggregation Polymorphism Multifile Development Module Outline
Today: • Functions recap • Classes recap • Objects recap • Object Persistence and Visibility.
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.
Working 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..
Working with classes • Definition file (.h) and implementation file (.cpp) are added to project automatically when you do Insert/New Class
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.
class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class born1997
class Creature{ private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class • The definition of a • class: • The class keyword, followed by the class name. • private attributes. • public methods. • the ; at the end
class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class • This class has anattribute of typeint. • Note that each C++ • data type and also abstract data types can be used as attribute types.
class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class • This class has two (public) methods. One to set the attribute value and the other to retrieve the attribute value.
class Creature { private: int yearOfBirth; public: void setYearOfBirth(year); int getYearOfBirth(); }; void Creature::setYearOfBirth { yearOfBirth = year; } int Creature::getYearOfBirth() { return yearOfBirth; } Example: The Creature class 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.
class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class This method is an example for a ‘modifier’ method. It modifies the attribute. The method changes the state of the object.
class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } }; Example: The Creature class This method is an example for a ‘selector’ method. It returns information about the attribute but does not change the state of the object.
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
A class is defined by: A Unique Name Attributes Methods An object is defined by: Identity State Behaviour Objects & Classes
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.
Multiple Objects • Of course we can create many objects of the same class: Creature myDog; Creature theMilkman; Creature myBestFriend; Creates three objects.
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.
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!
class Creature { private: int yearOfBirth; public: // … Creature() { yearOfBirth = 1970; cout << “Hello.”; } }; The Creature class with a user defined default constructor. • The syntax for a constructoris similar as for a method, but: • It has the same name as the class. • It has no return value.
class Creature { private: int yearOfBirth; public: // … Creature(int year) { yearOfBirth = year; } }; The Creature with a parametrized constructor. • This constructor can be used as follows: • Creature theMilkman(1953); • instantiates a 49 years old milkman.
class Creature { private: int yearOfBirth; public: // … Creature(Creature & otherCreature) { yearOfBirth = otherCreature.getYearOfBirth(); } }; The Creature with a copy constructor. • Example: • Creature myDog(1995); • Creature myCat(myDog); • creates a cat of the same age as the dog.
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.
Classes exercise • On paper for a change….
A class is defined by: A Unique Name Attributes Methods An object is defined by: Identity State Behaviour Again: Objects & Classes
A class is defined by: A Unique Name Attributes Methods An object is defined by: Identity State Behaviour Again: Objects & Classes But: We can give a class state and behaviour with the keyword static!
class Creature { private: int yearOfBirth; static int numberOfAllCreatures = 0; public: Creature() { // Constructor - counts the creatures. numberOfAllCreatures++; } static int getNumberOfAllCreatures() { return numberOfAllCreatures; } }; Example: The Creature class Note that all objects share the same value of the “class attribute” numberOfAllCreatures.
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).
This week:types of object • Four types of object (or any other data type) • Automatic (local) objects • External (global) objects • Static objects • Dynamic objects
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
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
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 name Identified by the memory address which they occupy
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
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 } )
External (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
Static 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
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 keyword • Not destroyed automatically: You have to do it yourself!!
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
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 Modifier function (‘set’ something) Accessor function (‘get’ something)
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
Automatic objects int main() { cout << "beginning of main function." << endl; creature myDog; cout << "end of main function." << endl; return 0; }
Automatic objects With automatic object, object destroyed automatically when it goes out of scope (destructor gets called)
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
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
Automatic objects { creature myDog; myDog.setYearOfBirth(1966); }
Automatic objects { creature myDog; myDog.setYearOfBirth(1966); } This is legal. myDog is still in scope.
Automatic objects { creature myDog; } myDog.setYearOfBirth(1966);
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.
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