You are on page 1of 80

Object Oriented Programming Development

What are we doing today?


Introduction of:
the lecturer Objects Basic Terminology C++ the module

What is Object Oriented Programming?


Identifying objects and assigning responsibilities to these objects. Objects communicate to other An object is like a objects by sending messages. black box. Messages are received by the The internal details methods of an object are hidden.

What is an object?
Tangible Things as a car, printer, ... Roles as employee, boss, ... Incidents as flight, overflow, ... Interactions as contract, sale, ... Specifications as colour, shape,

So, what are objects?


an object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain. Or An "object" is anything to which a concept applies. Etc.
5

Why do we care about objects?


Modularity - large software projects can be split up in smaller pieces. Reuseability - Programs can be assembled from pre-written software components. Extensibility - New software components can be written or developed from existing ones.
6

Example: The Person class


#include<string> #include<iostream> class Person{ char name[20]; int yearOfBirth; public: void displayDetails() { cout << name << " born in " << yearOfBirth << endl; } //... };

private data

public processes

The two parts of an object


Object = Data + Methods or to say the same differently:

An object has the responsibility to know and the responsibility to do.

+
8

Basic Terminology
Abstraction is the representation of the essential features of an object. These are encapsulated into an abstract data type. Encapsulation is the practice of including in an object everything it needs hidden from other objects. The internal state is usually not accessible by other objects.

Basic Terminology: Inheritance


Inheritance means that one class inherits the characteristics of another class. This is also called a is a relationship:
A car is a vehicle A dog is an animal A teacher is a person

10

Basic Terminology: Polymorphism


Polymorphism means having many forms. It allows different objects to respond to the same message in different ways, the response specific to the type of the object.
E.g. the message displayDetails() of the Person class should give different results when send to a Student object (e.g. the enrolment number).

11

Basic Terminology: Aggregation


Aggregation describes a has a relationship. One object is a part of another object.
A car has wheels.

We distinguish between composite aggregation (the composite owns the part) and shared aggregation (the part is shared by more then one composite).
12

Basic Terminology: Behaviour and Messages


The most important aspect of an object is its behaviour (the things it can do). A behaviour is initiated by sending a message to the object (usually by calling a method).

13

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.

14

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

15

Today:
Extensive analysis of an example program. Data types, operators, functions and I/O. Arrays, strings, pointers Control structures.

16

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p); int main() { int p = 7; doSomething(p); cout << I have something done. << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> The header of the file. Should void doSomething(int p);

int main() { int p = 7; doSomething(p); cout << I have something done. << endl; The compiler ignores these lines return 0; } (see next slide).

contain general information as file name, author, description, etc.

void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> This is a C++ comment. void doSomething(int p);

Lines starting with // int main() { are ignored by the compiler. int p = 7; doSomething(p); Also ignored is text enclosed by cout << I have something done. << endl; return 0; /* and */ }
void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p);

A pre-processor directive. Lines int main() starting with a # are interpreted { int p = 7; by a pre-processor before the doSomething(p); cout << I have somethingcompiler done. << endl; processes the file.
} return 0;

Other important directives are void doSomething(int p) { #define, #ifdef, #endif, #pragma, for( int i = 0; i < p; i++ ) { cout << * << endl; ...
} }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p);

In this example we include the file iostream.h into the program. int main() { iostream.h defines classes and int p = 7; objects related to input and doSomething(p); cout << I have something done. << endl; output. return 0;
}

void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

We need it here because cout is declared there.

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p);

Observation:

int main() cout is not a keyword of C++ but { int p = 7; an identifier defined in the doSomething(p); iostream library. cout << I have something done. << endl; return 0; }

void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p); int main() { A function prototype. int p = 7; doSomething(p); cout << I have something done. endl; is told about a In this line the << compiler return 0; function with the name doSomething }

and its void doSomething(int p) { signature. for( int i = 0; i < p; i++ ) { cout << * <<The endl; function here has one parameter } (int) and no return value (void). }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p); int main() { Also global variables and class int p = 7; doSomething(p);declarations usually come here. cout << I have something done. << endl; return 0; }

void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p); int main() The main function is the entry point of { a C++ program. Each C++ program int p = 7; doSomething(p); must have exactly one main() method. cout << I have something done. << endl; return 0; The return value of main() is an int. }

This is passed to the system void doSomething(int p) value { for( int i = 0; i < p;which i++ ) { invoked the C++ program. cout << * << endl; } }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p); int main() The code which implements the main { function is enclosed in { and }. int p = 7; doSomething(p); cout << I have something done. << endl; in { and } are Statements enclosed return 0; called a block. }

void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p); int main() We declare a variable p of type int. { int p = 7; doSomething(p); Other important data types of C++ are cout << I have something done. << endl; char, unsigned int, long, unsigned long, return 0; float, double, bool. }

void doSomething(int p)variable { The p is immediately initialised for( int i = 0; i < p; i++ ) { cout << * << with endl; the value 7. } }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something The function doSomething() is #include <iostream.h> called with the parameter p. void doSomething(int p); int main() { int p = 7; doSomething(p); cout << I have something done. << endl; return 0; }

void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

A typical C++ program


This statement prints the string I // FileID: hello.cpp // Title: The program doing something have something done. to the
#include <iostream.h> screen. void doSomething(int p);

int main() { int p = 7; doSomething(p); cout << I have something done. << endl; return 0; }

The stream manipulator endl outputs a newline and then flushes the output buffer.

void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

A typical C++ program


Functions with a return value // FileID: hello.cpp // Title: The program doing something which is not void use the return
#include <iostream.h> keyword in order to return their void doSomething(int p);value to the calling function. int main() In the special situation here, the { int p = 7; main() method has no calling doSomething(p); function. The value 0 is passed cout << I have something done. << endl; return 0; back to system when the program }

void doSomething(int p) { program worked correctly. for( int i = 0; i < p; i++ ) the { cout << * << endl; } }

is finished. Usually 0 means that

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p); int main() { int p = 7; The implementation of the doSomething(p); cout << I have something done. << endl; previously defined function return 0; } doSomething.

void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

A typical C++ program


// FileID: hello.cpp // Title: The program doing something #include <iostream.h> void doSomething(int p); int main() { int p = 7; doSomething(p); cout << I have something done. << endl; return 0; } void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }

Basics of C++ - data types


Some Fundamental data types:
char int double bool characters: a, b, \n, \0, 7 integers: 3, 6883, -5, 0 floating point numbers: 3.14, 7e9 true or false.

Also: float, long, unsigned long, short, unsigned char, wchar_t

33

Basics of C++ - variables


Declaring variables in a program:
char a; int b; double c;

Assignment:
b = 4; a = 'w; c = -3.777; int x = 78;

34

Basics of C++ - variables


Constants:
const double PI=3.1415926; const int MAXBUFFER=20;

Note: the const keyword is also used for method parameters, methods and return values (later)

35

Basics of C++ - operators


Arithmetic operators:
+, -, *, /, %

Bitwise:
&, |, ~,^

Comparison:
==, !=, <, >, >=, <=

Shortcuts:
+=, *=, ^=, (etc.)

Logical:
&&, ||, !

Other:
<<, >>, ? :, ->, ., ,

Assignment:
=

36

Basics of C++ - operators


The unary operators ++ and --:
++ -increment by 1 decrement by 1

The language C++ got its name by this operator! Note, that i++ and ++i have different behaviour *+
37

Basics of C++ - functions


Name

int someFunction(double f, char c) { // Body }


Parameter List Return Type

38

Basics of C++ - functions


Please note, that a function is specified by the name and the parameter types. The following functions are all different:
int exampleFunction(int i, char c); int exampleFunction(double f); int exampleFunction(); int exampleFunction(char c, int i);

39

Basics of C++ - functions


Pass by reference, example:
void square(int &v) { v = v * v; }
The parameter v is not copied.

In contrast, pass by value:


int square(int v) { return v * v; }

40

Basics of C++: I/O


For output use cout, e.g.
cout << The result is: << result << endl;

For input use cin, e.g.


cin >> x;

Note that iostream.h must be included via


#include <iostream.h>

41

Basics of C++ - arrays


Declaration:
int numbers[10];

Declaration & Initialisation:


int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19 };

Access:
numbers[6] = 2483; cout << The fourth prime is << primes*4+;

42

Basics of C++ - Strings


There arent any strings in C++.

43

Basics of C++ - Strings


There arent any strings in C++.

O.k., thats only half of the truth. In fact, by convention, strings are represented in C++ as \0 terminated arrays of characters. In addition, the file string.h declares useful string manipulating functions, e.g. strcpy, strlen which deal with \0 terminated character arrays.

44

Basics of C++ - pointer


A pointer points to a memory location which contains data of a particular type. The contents of a pointer is the address of some data Declaration:
int *p; double *aDoublePointer;

45

Basics of C++ - pointer


In C++ pointer and arrays are strongly related (array = pointer + memory).
int primes[] = {2, 3, 5, 7, 11 }; pointer arithmetic int *aPr = primes; aPr++; cout << The third prime is << *(aPr + 2);
The * operator accesses the data on the memory address
46

Control Structures - Decisions


The if statement:
if ( x > 0 ) { cout << positive; } else { cout << negative or zero; }

47

Control Structures - Decisions


The switch statement - example:
int x; cout << "Enter choice (1, 2, or 3)"; cin >> x; switch(x) { case 1: doThis(); break; case 2: doThat(); break; case 3: doSomethingElse(); break; default: cout << "Sorry, invalid Input"; }
48

Control Structures - Iteration


The for loop:
Terminating condition Start condition

for(k = 0; k < 10; k++ ) { cout << The square of << k << is << k * k << endl; }
Action taking place at the end of each iteration

49

Control Structures - Iteration


The while loop:
while ( condition ) { // do something }
Equivalent to: for( ; condition ; ) { // do something }
50

Control structures do while


The do while loop:
do { // something } while( condition);

Equivalent to:
// something while( condition) { // something }
51

Control Structures
And finally:

Yes, C++ has a goto.

Dont use it.

52

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

53

Today:
Control structures. Pointers. Classes Objects

54

Basics of C++ - pointer


A pointer points to a memory location which contains data of a particular type. The contents of a pointer is the address of some data Declaration:
int *p; double *aDoublePointer;
2.73817

55

Again: Basics of C++ - arrays


Declaration:
int numbers[10];

Declaration & Initialisation:


int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19 };

Access:
numbers[6] = 2483; cout << The fourth prime is << primes*4+;

56

Basics of C++ - pointer


In C++ pointer and arrays are strongly related (array = pointer + memory).
The same as primes[3]

int primes[] = {2, 3, 5, 7, 11 }; int *aPr = primes; cout << The third prime is << *(aPr + 3);
The * operator accesses the data on the memory address
57

What is Object Oriented Programming?


Identifying objects and assigning responsibilities to these objects. Objects communicate to other An object is like a objects by sending messages. black box. Messages are received by the The internal details methods of an object are hidden.

58

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.

59

Example: The Creature class


class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } };
born1997

Example: 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

Example: The Creature class


class Creature { private: int yearOfBirth; public: void setYearOfBirth(year) { yearOfBirth = year; } int getYearOfBirth() { return yearOfBirth; } };

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.

Example: 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.

Example: The Creature class


class 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.

Example: 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.

Example: 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.

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.

67

Objects & Classes


A class is defined by: A Unique Name Attributes Methods An object is defined by: Identity State Behaviour

68

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.

69

Multiple Objects
Of course we can create many objects of the same class:
Creature myDog; Creature theMilkman; Creature myBestFriend;

Creates three objects.

70

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.
71

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 dont like this we can specify constructors explicitly!

72

class Creature { private: int yearOfBirth; public: // Creature() { yearOfBirth = 1970; cout << Hello.; } };

The Creature class with a user defined default constructor.


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.

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.

The Creature with a copy constructor.


Example:

Creature myDog(1995); class Creature { Creature myCat(myDog); private: int yearOfBirth; creates a cat of the same age as the dog. public: // Creature(Creature & otherCreature) { yearOfBirth = otherCreature.getYearOfBirth(); } };

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.

76

Again: Objects & Classes


A class is defined by: A Unique Name Attributes Methods An object is defined by: Identity State Behaviour

77

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!
78

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.

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).

80

You might also like