1 / 41

Chapter 2: Language C++

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. C++ owes most to C. Other ancestors are Simula67 and Algol68.

Télécharger la présentation

Chapter 2: Language C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. C++ and C • 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,…). • C++ can be used both as a low level and as a high level language. We focus on the high level aspects.

  4. C++ and Java • Java is a full object oriented language, all code has to go into classes. • 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: • Extensive analysis of an example program. • Data types, operators, functions and I/O. • Arrays, strings, pointers • Control structures.

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

  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 header of the file. Should contain general information as file name, author, description, etc. The compiler ignores these lines (see next slide).

  8. A typical C++ program This is a C++ comment. Lines starting with // are ignored by the compiler. Also ignored is text enclosed by /* and */ // 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; } }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  23. 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;

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

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

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

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

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

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

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

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

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

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

  34. 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;

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

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

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

  38. Control Structures - Iteration • The for loop: for(k = 0; k < 10; k++ ) { cout << “The square of “ << k << “ is “ << k * k << endl; } Terminating condition Start condition Action taking place atthe end of each iteration

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

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

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

More Related