Lecture 3 c++

1,476 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,476
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
58
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Lecture 3 c++

  1. 1. Basic Concepts of OOPs Lecture 2 Object Oriented Programming with C++
  2. 2. A typical C++ program <ul><li>// FileID: hello.cpp </li></ul><ul><li>// Title: The program doing something </li></ul><ul><li>#include <iostream.h> </li></ul><ul><li>void doSomething(int p); </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>int p = 7; </li></ul><ul><li>doSomething(p); </li></ul><ul><li>cout << “I have something done.” << endl; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul><ul><li>void doSomething(int p) { </li></ul><ul><li>for( int i = 0; i < p; i++ ) { </li></ul><ul><li>cout << “*” << endl; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  3. 3. Basics of C++ - data types <ul><li>Some Fundamental data types: </li></ul><ul><li>char characters: ’a’, ’b’, ’ ’, ’’, ’7’ </li></ul><ul><li>int integers: 3, 6883, -5, 0 </li></ul><ul><li>double floating point numbers: 3.14, 7e9 </li></ul><ul><li>bool true or false. </li></ul><ul><li>Also: float, long, unsigned long, short, unsigned char, wchar_t </li></ul>
  4. 4. Basics of C++ - variables <ul><li>Declaring variables in a program: </li></ul><ul><ul><li>char a; </li></ul></ul><ul><ul><li>int b; </li></ul></ul><ul><ul><li>double c; </li></ul></ul><ul><li>Assignment: </li></ul><ul><ul><li>b = 4; a = 'w’; c = -3.777; </li></ul></ul><ul><ul><li>int x = 78; </li></ul></ul>
  5. 5. Basics of C++ - operators <ul><li>Arithmetic operators: </li></ul><ul><ul><li>+, -, *, /, % </li></ul></ul><ul><li>Comparison: </li></ul><ul><ul><li>==, !=, <, >, >=, <= </li></ul></ul><ul><li>Logical: </li></ul><ul><ul><li>&&, ||, ! </li></ul></ul><ul><li>Assignment: </li></ul><ul><ul><li>= </li></ul></ul><ul><li>Bitwise: </li></ul><ul><ul><li>&, |, ~,^ </li></ul></ul><ul><li>Shortcuts: </li></ul><ul><ul><li>+=, *=, ^=, (etc.) </li></ul></ul><ul><li>Other: </li></ul><ul><ul><li><<, >>, ? :, ->, ., , </li></ul></ul>
  6. 6. Basics of C++ - operators <ul><li>The unary operators ++ and --: </li></ul><ul><ul><li>++ increment by 1 </li></ul></ul><ul><ul><li>-- decrement by 1 </li></ul></ul><ul><li>The language C++ got its name by this operator! </li></ul><ul><li>Note, that i++ and ++i have different behaviour […] </li></ul>
  7. 7. Basics of C++ - functions int someFunction(double f, char c) { // … } Name Body Parameter List Return Type Name Parameter List Body Name Parameter List Return Type Body Name Parameter List Return Type
  8. 8. Basics of C++ - functions <ul><li>Please note, that a function is specified by the name and the parameter types. The following functions are all different: </li></ul><ul><ul><li>int exampleFunction(int i, char c); </li></ul></ul><ul><ul><li>int exampleFunction(double f); </li></ul></ul><ul><ul><li>int exampleFunction(); </li></ul></ul><ul><ul><li>int exampleFunction(char c, int i); </li></ul></ul>
  9. 9. Basics of C++ - functions <ul><li>Pass by reference, example: </li></ul><ul><ul><li>void square(int &v) { v = v * v; } </li></ul></ul><ul><li>In contrast, pass by value: </li></ul><ul><ul><li>int square(int v) { return v * v; } </li></ul></ul>The parameter v is not copied.
  10. 10. Basics of C++: I/O <ul><li>For output use cout, e.g. </li></ul><ul><ul><li>cout << “The result is: “ << result << endl; </li></ul></ul><ul><li>For input use cin, e.g. </li></ul><ul><ul><li>cin >> x; </li></ul></ul><ul><li>Note that iostream.h must be included via </li></ul><ul><ul><li>#include <iostream.h> </li></ul></ul>
  11. 11. Basics of C++ - arrays <ul><li>Declaration: </li></ul><ul><ul><li>int numbers[10]; </li></ul></ul><ul><li>Declaration & Initialisation: </li></ul><ul><ul><li>int primes[] = { 2, 3, 5, 7, 11, 13, 17, 19 }; </li></ul></ul><ul><li>Access: </li></ul><ul><ul><li>numbers[6] = 2483; </li></ul></ul><ul><ul><li>cout << “The fourth prime is “ << primes[4]; </li></ul></ul>
  12. 12. Basics of C++ - pointer <ul><li>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 </li></ul><ul><li>Declaration: </li></ul><ul><ul><li>int *p; </li></ul></ul><ul><ul><li>double *aDoublePointer; </li></ul></ul>
  13. 13. Basics of C++ - pointer <ul><li>In C++ pointer and arrays are strongly related ( “ array = pointer + memory” ). </li></ul><ul><ul><li>int primes[] = {2, 3, 5, 7, 11 }; </li></ul></ul><ul><ul><li>int *aPr = primes; </li></ul></ul><ul><ul><li>aPr++; </li></ul></ul><ul><ul><li>cout << “The third prime is “ << *(aPr + 2); </li></ul></ul>The * operator accesses the data on the memory address The * operator accesses the data on the memory address <ul><li>In C++ pointer and arrays are strongly related ( “ array = pointer + memory” ). </li></ul>pointer arithmetic
  14. 14. Control Structures - Decisions <ul><li>The if statement: </li></ul><ul><ul><li>if ( x > 0 ) { </li></ul></ul><ul><ul><li>cout << “positive”; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>else { </li></ul></ul><ul><ul><li>cout << “negative or zero”; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  15. 15. Control Structures - Decisions <ul><li>The switch statement - example: </li></ul><ul><ul><li>int x; </li></ul></ul><ul><ul><li>cout << &quot;Enter choice (1, 2, or 3)&quot;; </li></ul></ul><ul><ul><li>cin >> x; </li></ul></ul><ul><ul><li>switch(x) { </li></ul></ul><ul><ul><li>case 1: doThis(); break; </li></ul></ul><ul><ul><li>case 2: doThat(); break; </li></ul></ul><ul><ul><li>case 3: doSomethingElse(); break; </li></ul></ul><ul><ul><li>default: cout << &quot;Sorry, invalid Input&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  16. 16. Control Structures - Iteration <ul><li>The for loop: </li></ul><ul><ul><li>for(k = 0; k < 10; k++ ) { </li></ul></ul><ul><ul><li>cout << “The square of “ << k << “ is “ </li></ul></ul><ul><ul><li><< k * k << endl; </li></ul></ul><ul><ul><li>} </li></ul></ul>Start condition Action taking place at the end of each iteration Terminating condition
  17. 17. Control Structures - Iteration <ul><li>The while loop: </li></ul><ul><ul><li>while ( condition ) { </li></ul></ul><ul><ul><li>// do something </li></ul></ul><ul><ul><li>} </li></ul></ul>Equivalent to: for( ; condition ; ) { // do something }
  18. 18. Control structures - do … while <ul><li>The do … while loop: </li></ul><ul><ul><li>do { </li></ul></ul><ul><ul><li>// something </li></ul></ul><ul><ul><li>} while( condition); </li></ul></ul>Equivalent to: // something while( condition) { // something }
  19. 19. Control Structures <ul><li>And finally: </li></ul><ul><li>Yes, C++ has a goto. Don’t use it. </li></ul>

×