Transcript

1

Object Oriented ProgrammingDevelopment

2

What are we doing today?

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

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

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

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.

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.

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

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.

= +

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.

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

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

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.

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

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.

15

Module Outline

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

• Inheritance• Aggregation• Polymorphism• Multifile Development

16

Today:

• Extensive analysis of an example program.

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

• Arrays, strings, pointers

• Control structures.

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>

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

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 */

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

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.

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.

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

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.

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.

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.

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.

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.

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

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.

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.

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

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

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;

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)

36

Basics of C++ - operators

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

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

• Logical:– &&, ||, !

• Assignment:– =

• Bitwise:– &, |, ~,^

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

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

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

38

Basics of C++ - functions

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

Name

Body

ParameterList

Return Type

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

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.

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>

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

43

Basics of C++ - Strings

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

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.

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;

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

47

Control Structures - Decisions

• The if statement:

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

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

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

50

Control Structures - Iteration

• The while loop:

while ( condition ) { // do something }

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

51

Control structures - do … while

• The do … while loop:

do { // something} while( condition);

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

52

Control Structures

• And finally:

• Yes, C++ has a goto.

Don’t use it.

53

Module Outline

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

• Inheritance• Aggregation• Polymorphism• Multifile Development

54

Today:

• Control structures.

• Pointers.

• Classes

• Objects

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

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

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

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

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.

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

born1997

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

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.

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.

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.

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.

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.

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

68

Objects & Classes

• A class is defined by:

–A Unique Name–Attributes–Methods

• An object is defined by:

– Identity– State–Behaviour

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.

70

Multiple Objects

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

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

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.

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!

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.

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.

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.

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.

77

Again: Objects & Classes

• A class is defined by:

–A Unique Name–Attributes–Methods

• An object is defined by:

– Identity– State–Behaviour

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!

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.

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

top related