Professional Documents
Culture Documents
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,
private data
public processes
+
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.
10
11
We distinguish between composite aggregation (the composite owns the part) and shared aggregation (the part is shared by more then one composite).
12
13
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
int main() { int p = 7; doSomething(p); cout << I have something done. << endl; The compiler ignores these lines return 0; } (see next slide).
void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }
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 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; ...
} }
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; } }
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; } }
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). }
void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }
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; } }
void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }
void doSomething(int p)variable { The p is immediately initialised for( int i = 0; i < p; i++ ) { cout << * << with endl; the value 7. } }
void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }
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; } }
void doSomething(int p) { program worked correctly. for( int i = 0; i < p; i++ ) the { cout << * << endl; } }
void doSomething(int p) { for( int i = 0; i < p; i++ ) { cout << * << endl; } }
33
Assignment:
b = 4; a = 'w; c = -3.777; int x = 78;
34
Note: the const keyword is also used for method parameters, methods and return values (later)
35
Bitwise:
&, |, ~,^
Comparison:
==, !=, <, >, >=, <=
Shortcuts:
+=, *=, ^=, (etc.)
Logical:
&&, ||, !
Other:
<<, >>, ? :, ->, ., ,
Assignment:
=
36
The language C++ got its name by this operator! Note, that i++ and ++i have different behaviour *+
37
38
39
40
41
Access:
numbers[6] = 2483; cout << The fourth prime is << primes*4+;
42
43
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
45
47
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
Equivalent to:
// something while( condition) { // something }
51
Control Structures
And finally:
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
55
Access:
numbers[6] = 2483; cout << The fourth prime is << primes*4+;
56
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
58
59
The definition of a class: The class keyword, followed by the class name. private attributes. public methods. the ; at the end
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.
This class has two (public) methods. One to set the attribute value and the other to retrieve the attribute value.
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.
This method is an example for a modifier method. It modifies the attribute. The method changes the state of the object.
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
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;
70
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.; } };
class Creature { private: int yearOfBirth; public: // Creature(int year) { yearOfBirth = year; } };
Creature theMilkman(1953);
instantiates a 49 years old milkman.
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
77
But: We can give a class state and behaviour with the keyword static!
78
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