Top Banner
1 Object Oriented Programming Development
80
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Revision Lecture

1

Object Oriented ProgrammingDevelopment

Page 2: Revision Lecture

2

What are we doing today?

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

Page 3: Revision Lecture

3

What is Object Oriented Programming?

An object is like a black box.

The internal details are hidden.

• Identifying objects and assigning responsibilities to these objects.

• Objects communicate to other objects by sending messages.

• Messages are received by the methods of an object

Page 4: Revision Lecture

4

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, …

Page 5: Revision Lecture

5

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.

Page 6: Revision Lecture

6

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.

Page 7: Revision Lecture

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

Page 8: Revision Lecture

8

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.

= +

Page 9: Revision Lecture

9

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.

Page 10: Revision Lecture

10

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 teacher is a person

A dog is an animal

Page 11: Revision Lecture

11

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

Page 12: Revision Lecture

12

Basic Terminology:Aggregation

• Aggregation describes a “has a” relationship. One object is a part of another object.

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

A car has wheels.

Page 13: Revision Lecture

13

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

Page 14: Revision Lecture

14

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 15: Revision Lecture

15

Module Outline

• Introduction• The non object oriented basics• Classes• Design Approaches• Testing

• Inheritance• Aggregation• Polymorphism• Multifile Development

Page 16: Revision Lecture

16

Today:

• Extensive analysis of an example program.

• Data types, operators, functions and I/O.

• Arrays, strings, pointers

• Control structures.

Page 17: Revision Lecture

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; }}

Page 18: Revision Lecture

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; }}

The header of the file. Should contain general information as file name, author, description, etc.

The compiler ignores these lines (see next slide).

Page 19: Revision Lecture

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; }}

This is a C++ comment.

Lines starting with // are ignored by the compiler.

Also ignored is text enclosed by /* and */

Page 20: Revision Lecture

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 pre-processor directive. Lines starting with a # are interpreted by a pre-processor before the compiler processes the file.

Other important directives are #define, #ifdef, #endif, #pragma, ...

Page 21: Revision Lecture

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; }}

In this example we include the file iostream.h into the program. iostream.h defines classes and objects related to input and output.

We need it here because cout is declared there.

Page 22: Revision Lecture

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; }}

Observation:

cout is not a keyword of C++ but an identifier defined in the iostream library.

Page 23: Revision Lecture

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 function prototype.

In this line the compiler is told about a function with the name doSomething and its signature.

The function here has one parameter (int) and no return value (void).

Page 24: Revision Lecture

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; }}

Also global variables and class declarations usually come here.

Page 25: Revision Lecture

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; }}

The main function is the entry point of a C++ program. Each C++ program must have exactly one main() method.

The return value of main() is an int. This value is passed to the system which invoked the C++ program.

Page 26: Revision Lecture

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; }}

The code which implements the main function is enclosed in { and }.

Statements enclosed in { and } are called a block.

Page 27: Revision Lecture

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; }}

We declare a variable p of type int.

Other important data types of C++ are char, unsigned int, long, unsigned long, float, double, bool.

The variable p is immediately initialised with the value 7.

Page 28: Revision Lecture

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; }}

The function doSomething() is called with the parameter p.

Page 29: Revision Lecture

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; }}

This statement prints the string “I have something done.” to the screen.

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

Page 30: Revision Lecture

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; }}

Functions with a return value which is not void use the return keyword in order to return their value to the calling function.

In the special situation here, the main() method has no calling function. The value 0 is passed back to system when the program is finished. Usually 0 means that the program worked correctly.

Page 31: Revision Lecture

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; }}

The implementation of the previously defined function doSomething.

Page 32: Revision Lecture

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; }}

Page 33: Revision Lecture

33

Basics of C++ - data types

• Some Fundamental data types:

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

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

Page 34: Revision Lecture

34

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;

Page 35: Revision Lecture

35

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)

Page 36: Revision Lecture

36

Basics of C++ - operators

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

• Comparison:– ==, !=, <, >, >=, <=

• Logical:– &&, ||, !

• Assignment:– =

• Bitwise:– &, |, ~,^

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

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

Page 37: Revision Lecture

37

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 […]

Page 38: Revision Lecture

38

Basics of C++ - functions

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

Name

Body

ParameterList

Return Type

Page 39: Revision Lecture

39

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

Page 40: Revision Lecture

40

Basics of C++ - functions

• Pass by reference, example:– void square(int &v) { v = v * v; }

• In contrast, pass by value:– int square(int v) { return v * v; }

The parameter v is not copied.

Page 41: Revision Lecture

41

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>

Page 42: Revision Lecture

42

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];

Page 43: Revision Lecture

43

Basics of C++ - Strings

• There aren’t any strings in C++.

Page 44: Revision Lecture

44

Basics of C++ - Strings

• There aren’t any strings in C++.

• O.k., that’s 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.

Page 45: Revision Lecture

45

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;

Page 46: Revision Lecture

46

Basics of C++ - pointer

• In C++ pointer and arrays are strongly related (“array = pointer + memory”).

– int primes[] = {2, 3, 5, 7, 11 };– int *aPr = primes;– aPr++;– cout << “The third prime is “ << *(aPr + 2);

pointer arithmetic

The * operator accesses the data on the memory address

Page 47: Revision Lecture

47

Control Structures - Decisions

• The if statement:

if ( x > 0 ) { cout << “positive”;} else { cout << “negative or zero”;}

Page 48: Revision Lecture

48

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";}

Page 49: Revision Lecture

49

Control Structures - Iteration

• The for loop:

for(k = 0; k < 10; k++ ) { cout << “The square of “ << k << “ is “ << k * k << endl; }

Start condition

Action taking place atthe end of each iteration

Terminating condition

Page 50: Revision Lecture

50

Control Structures - Iteration

• The while loop:

while ( condition ) { // do something }

Equivalent to: for( ; condition ; ) { // do something }

Page 51: Revision Lecture

51

Control structures - do … while

• The do … while loop:

do { // something} while( condition);

Equivalent to: // somethingwhile( condition) { // something }

Page 52: Revision Lecture

52

Control Structures

• And finally:

• Yes, C++ has a goto.

Don’t use it.

Page 53: Revision Lecture

53

Module Outline

• Introduction• The non object oriented basics• Classes• Design Approaches• Testing

• Inheritance• Aggregation• Polymorphism• Multifile Development

Page 54: Revision Lecture

54

Today:

• Control structures.

• Pointers.

• Classes

• Objects

Page 55: Revision Lecture

55

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

Page 56: Revision Lecture

56

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];

Page 57: Revision Lecture

57

Basics of C++ - pointer

• In C++ pointer and arrays are strongly related (“array = pointer + memory”).

– int primes[] = {2, 3, 5, 7, 11 };– int *aPr = primes;– cout << “The third prime is “ << *(aPr + 3);

The same as primes[3]

The * operator accesses the data on the memory address

Page 58: Revision Lecture

58

What is Object Oriented Programming?

An object is like a black box.

The internal details are hidden.

• Identifying objects and assigning responsibilities to these objects.

• Objects communicate to other objects by sending messages.

• Messages are received by the methods of an object

Page 59: Revision Lecture

59

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 60: Revision Lecture

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

born1997

Page 61: Revision Lecture

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 62: Revision Lecture

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 63: Revision Lecture

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 64: Revision Lecture

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 65: Revision Lecture

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 66: Revision Lecture

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 67: Revision Lecture

67

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 68: Revision Lecture

68

Objects & Classes

• A class is defined by:

–A Unique Name–Attributes–Methods

• An object is defined by:

– Identity– State–Behaviour

Page 69: Revision Lecture

69

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 70: Revision Lecture

70

Multiple Objects

• Of course we can create many objects of the same class:

Creature myDog;Creature theMilkman;Creature myBestFriend; Creates three objects.

Page 71: Revision Lecture

71

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 72: Revision Lecture

72

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 73: Revision Lecture

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 74: Revision Lecture

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 75: Revision Lecture

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 76: Revision Lecture

76

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 77: Revision Lecture

77

Again: Objects & Classes

• A class is defined by:

–A Unique Name–Attributes–Methods

• An object is defined by:

– Identity– State–Behaviour

Page 78: Revision Lecture

78

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 79: Revision Lecture

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 80: Revision Lecture

80

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