OBJECT ORIENTED PROGRAMMING USING C Chapter 2 Language

OBJECT ORIENTED PROGRAMMING USING C++ Chapter 2: Language C++ Lecturer: Nguyen Thi Hien Software Engineering Department E-mail: nguyenthihienqn@gmail. com Home page: hienngong. wordpress. com

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

C++ and C v. 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, …). v. C++ can be used both as a low level and as a high level language. 3 e h t n o s u. c s o t f c e e p W s a l e v e l high

C++ and Java v. Java is a full object oriented language, all code has to go into classes. v. 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. 4

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

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

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 int main() file name, author, description, { etc. int p = 7; } do. Something(p); The compiler ignores cout << “I have something done. ” << these endl; lines (see next slide). return 0; void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } }

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); This is a C++ comment. Lines starting with // are ignored by the compiler. Also ignored is text enclosed by int main() /* and */ { 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; } }

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

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

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

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); A function prototype. In this line the compiler is told about a function with the name do. Something and its signature. int main() The function here has one parameter { (int) and no return value (void). 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; } }

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> Also global variables and class declarations usually come here. 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; } }

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

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; do. Something(p); Statements enclosed in { and } are cout << “I have called something done. ” << endl; a block. return 0; } void do. Something(int p) { for( int i = 0; i < p; i++ ) { cout << “*” << endl; } }

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. Other important data types of C++ are char, unsigned int, long, unsigned long, float, double, bool. int main() The variable p is immediately initialised { with the value 7. 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; } }

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); The function do. Something() is int main() { called with the parameter p. 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; } }

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

A typical C++ program // File. ID: hello. cpp // Title: The program doing something Functions with a return value which is not void use the return keyword in order to return their value to p); the calling function. #include <iostream. h> void do. Something(int 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. 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; } }

A typical C++ program // File. ID: hello. cpp // Title: The program doing something #include <iostream. h> void do. Something(int p); The implementation of the previously defined function do. Something. 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; } }

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

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

Basics of C++ - variables v. Declaring variables in a program: § char a; § int b; § double c; v. Assignment: § b = 4; a = 'w’; c = -3. 777; § int x = 78; 23

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

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

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

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

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

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

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

Basics of C++ - Strings v. There aren’t any strings in C++. 32

Basics of C++ - Strings v. 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 ’ ’ 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. 33

Basics of C++ - pointer v. 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 v. Declaration: § int *p; § double *a. Double. Pointer; 34

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

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

Control Structures - Decisions v. 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"; } 37

Control Structures - Iteration v. 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 38

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

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

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