Object Oriented Programming Development z By Marc Conrad

Object Oriented Programming Development z By: Marc Conrad University of Luton z Email: Marc. Conrad@luton. ac. uk z Room: D 104 1

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

Historical Notes z. C++ owes most to C. Other ancestors are Simula 67 C++ 1987 and Algol 68. z. First versions of C++ in 1980 under the name “C with classes”. Since 1983 the name C++ is used. z 1990: ANSI/ISO 9899 defines a standard for C z 1998: ISO/IEC 14882 specifies the 3 standard for C++

C++ and C z. C is a subset of C++. Advantages: Existing C libraries can be used, efficient code can be generated. But: C++ has the same caveats and problems as C (e. g. pointer arithmetic, …). z. C++ can be used both as a low level and as a high level language. e h t n o s u. c s o t f c e e p W s a l e v e l 4 high

C++ and Java z. Java is a full object oriented language, all code has to go into classes. z. C++ - in contrast - is a hybrid language, capable both of functional and object oriented programming. So, C++ is more powerful but also more difficult to handle than Java. 5

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

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); int main() { int p = 7; do. Something(p); cout << “I have something done. ” << endl; return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 7

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> The header of the file. Should void do. Something(int p); contain general information as file name, author, description, etc. int main() { int p = 7; do. Something(p); The compiler ignores these cout << “I have something done. ” << endl; lines (see next slide). return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 8

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> This is a C++ comment. void do. Something(int p); Lines starting with // are ignored by the compiler. int main() { int p = 7; Also ignored is text enclosed do. Something(p); by /*done. ” and */ cout << “I have something << endl; return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 9

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); A pre-processor directive. Lines starting with a # are int main() { interpreted by a pre-processor int p = 7; before the compiler processes do. Something(p); cout << “I have something done. ” << endl; the file. return 0; } Other important directives are void do. Something(int p) {#define, #ifdef, #endif, for( int i = 0; i < p; i++#pragma, ){. . . cout << “*” << endl; } } 10

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> In this example we include void do. Something(int p); the file iostream. h into the int main() program. iostream. h defines { classes and objects related to int p = 7; do. Something(p); input and output. } cout << “I have something done. ” << endl; return 0; We need it here because cout is declared there. void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 11

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); Observation: cout is not a keyword of C++ but an identifier defined in the iostream library. int main() { int p = 7; do. Something(p); cout << “I have something done. ” << endl; return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 12

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); int main() A function prototype. { int p = 7; In this line the compiler is told do. Something(p); cout << “I have something done. ” with << endl; about a function the name return 0; do. Something and its signature. } The p) function here has one void do. Something(int { for( int i = 0; i parameter < p; i++ ) { (int) and no return value cout << “*” << endl; (void). } 13 }

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); int main() Also global variables and class { int p = 7; declarations usually come here. do. Something(p); cout << “I have something done. ” << endl; return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 14

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); The main function is the entry point main() of a C++ program. Each C++ { program must have exactly one int p = 7; do. Something(p); main() method. cout << “I have something done. ” << endl; return 0; The return value of main() is an int. This value is passed to the system void do. Something(int p) { which the C++ program. for( int i = 0; i < p; i++invoked ){ } } cout << “*” << endl; } 15

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); The code which implements the main function is enclosed in { and }. int main() { int p = 7; Statements enclosed in { and } are do. Something(p); cout << “I havecalled something done. ” << endl; a block. return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 16

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); We declare a variable p of type int main() { Other important data types of C++ int p = 7; are char, unsigned int, long, do. Something(p); cout << “I haveunsigned somethinglong, done. ” << endl; float, double, bool. return 0; } The variable p is immediately initialised void do. Something(int p) { with the value 7. for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 17

A typical C++ program // File. ID: hello. cpp // Title: The program doing somethingdo. Something() is The function #include <iostream. h>called with the parameter p. void do. Something(int p); int main() { int p = 7; do. Something(p); cout << “I have something done. ” << endl; return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 18

A typical C++ program This statement prints the string // File. ID: hello. cpp // Title: The program doing something “I have something done. ” to the #include <iostream. h>screen. void do. Something(int p); The “stream manipulator” endl outputs a newline and then int main() { “flushes the output buffer”. int p = 7; do. Something(p); cout << “I have something done. ” << endl; return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 19

A typical C++ program Functions with a return value // File. ID: hello. cpp // Title: The program doing whichsomething is not void use the return #include <iostream. h>keyword in order to return their value to the calling function. void do. Something(int p); In the special situation here, the int main() { main() method has no calling int p = 7; function. The value 0 is passed do. Something(p); back todone. ” system cout << “I have something << when endl; the return 0; program is finished. Usually 0 } means that the program worked void do. Something(int p) { correctly. } for( int i = 0; i < p; i++ ) { cout << “*” << endl; } 20

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); int main() { The implementation of the int p = 7; previously defined function do. Something(p); cout << “I have something done. ” << endl; do. Something. return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 21

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); int main() { int p = 7; do. Something(p); cout << “I have something done. ” << endl; return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } } 22

Basics of C++ - data types z. Some Fundamental data types: z char z int z double z bool characters: ’a’, ’b’, ’n’, ’ ’, ’ 7’ integers: 3, 6883, -5, 0 floating point numbers: 3. 14, 7 e 9 true or false. z Also: float, long, unsigned long, short, unsigned char, wchar_t 23

Basics of C++ - variables z. Declaring variables in a program: ychar a; yint b; ydouble c; z. Assignment: yb = 4; a = 'w’; c = -3. 777; yint x = 78; 24

Basics of C++ - variables z. Constants: yconst double PI=3. 1415926; yconst int MAXBUFFER=20; z. Note: the const keyword is also used for method parameters, methods and return values (later) 25

Basics of C++ - operators z Arithmetic operators: y+, -, *, /, % z Comparison: y==, !=, <, >, >=, <= z Logical: y&&, ||, ! z Bitwise: y&, |, ~, ^ z Shortcuts: y+=, *=, ^=, (etc. ) z Other: y<<, >>, ? : , ->, . , , z Assignment: y= 26

Basics of C++ - operators z. The unary operators ++ and --: y++ y-- increment by 1 decrement by 1 z. The language C++ got its name by this operator! z. Note, that i++ and ++i have different behaviour […] 27

Basics of C++ - functions Name int some. Function(double f, char c) { // … Body } Parameter List Return Type 28

Basics of C++ - functions z. Please note, that a function is specified by the name and the parameter types. The following functions are all different: yint example. Function(int i, char c); example. Function(double f); example. Function(char c, int i); 29

Basics of C++ - functions z. Pass by reference, example: yvoid square(int &v) { v = v * v; } The parameter v is not copied. z. In contrast, pass by value: yint square(int v) { return v * v; } 30

Basics of C++: I/O z. For output use cout, e. g. ycout << “The result is: “ << result << endl; z. For input use cin, e. g. ycin >> x; z. Note that iostream. h must be included via y#include <iostream. h> 31
![Basics of C++ - arrays z. Declaration: yint numbers[10]; z. Declaration & Initialisation: yint Basics of C++ - arrays z. Declaration: yint numbers[10]; z. Declaration & Initialisation: yint](http://slidetodoc.com/presentation_image_h2/7811626ff3763fa4c478cdcd541ca81c/image-32.jpg)
Basics of C++ - arrays z. Declaration: yint numbers[10]; z. Declaration & Initialisation: yint primes[] = { 2, 3, 5, 7, 11, 13, 17, 19 }; z. Access: ynumbers[6] = 2483; ycout << “The fourth prime is “ << primes[4]; 32

Basics of C++ - Strings z. There aren’t any strings in C++. 33

Basics of C++ - Strings z. There aren’t any strings in C++. x. O. k. , that’s only half of the truth. In fact, by convention, strings are represented in C++ as ’ ’ terminated arrays of characters. In addition, the file string. h declares useful string manipulating functions, e. g. strcpy, strlen which deal with ’ ’ terminated character arrays. 34

Basics of C++ - pointer z. 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 z. Declaration: yint *p; ydouble *a. Double. Pointer; 35

Basics of C++ - pointer z. In C++ pointer and arrays are strongly related (“array = pointer + memory”). yint primes[] = {2, 3, 5, 7, 11 }; pointer arithmetic yint *a. Pr = primes; ya. Pr++; ycout << “The third prime is “ << *(a. Pr + 2); The * operator accesses the data on the memory address 36

Control Structures Decisions z. The if statement: if ( x > 0 ) { cout << “positive”; } else { cout << “negative or zero”; } 37

Control Structures Decisions z. The switch statement - example: int x; cout << "Enter choice (1, 2, or 3)"; cin >> x; switch(x) { case 1: do. This(); break; case 2: do. That(); break; case 3: do. Something. Else(); break; default: cout << "Sorry, invalid Input"; } 38

Control Structures Iteration z. The for loop: Start condition Terminating 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 39

Control Structures Iteration z. The while loop: while ( condition ) { // do something } Equivalent to: for( ; condition ; ) { // do something } 40

Control structures do … while z. The do … while loop: do { // something } while( condition); Equivalent to: // something while( condition) { // something } 41

Control Structures z. And finally: z. Yes, C++ has a goto. Don’t use it. 42
- Slides: 42