0
<ul><li>AU/MITM/1.6 </li></ul><ul><li>By Mohammed A. Saleh </li></ul>
<ul><li>The course runs from 13 th  April – 01 st  May </li></ul><ul><li>Total contact hours = 30 hrs </li></ul><ul><li>Le...
<ul><li>Coursework: </li></ul><ul><li>Test 1 - 20 marks </li></ul><ul><li>Test 2  - 20 marks </li></ul><ul><li>Assignment ...
<ul><li>➩ Module 1 </li></ul><ul><li>Problem solving with computers problem solving techniques – Structured programming se...
<ul><li>➩ Module 3 </li></ul><ul><li>Functions and Pointers – Structures and Unions – Files </li></ul><ul><li>➩ Module 4 <...
<ul><li>➩ Module 5 </li></ul><ul><li>Inheritance – Pointers – Virtual Functions – Polymorphism – Managing console I/O oper...
<ul><li>Decisions play a major role in providing problem solving techniques. Computer programs are all about making decisi...
<ul><li>For a computer program to decide what action to take, it uses flow-control commands. This is based on the results ...
<ul><li>Simple programming exercises can be solved by just writing code to implement the desired problem. </li></ul><ul><l...
<ul><li>Programs could be written in in terms of three control structures: </li></ul><ul><li>Sequence structure </li></ul>...
<ul><li>Built into C++. Unless directed otherwise the computer executes C++ statements one after another in the order in w...
<ul><li>Basic structure </li></ul><ul><li>Figure 1.0: SEQUENCE structure </li></ul>
<ul><li>C++ provides three types of selection structures: </li></ul><ul><li>The IF statement ( single-selection structure ...
<ul><li>The IF statement directs a program to execute a statement or statement block if a test condition is true and to sk...
<ul><li>A  true  test-condition causes the program to execute statement, which can be a single statement or a block. A  fa...
<ul><li>if ( grade >= 60 ) </li></ul><ul><li>cout << “ Passed”; </li></ul><ul><li>The example above determines the conditi...
<ul><li>The IF/ ELSE statement ( double-selection ) </li></ul><ul><li>Lets a program decide which of two statements or blo...
<ul><li>The syntax for the IF/ELSE statement: </li></ul><ul><li>if ( test-condition ) </li></ul><ul><li>  statement1 </li>...
<ul><li>Code Fragment:  </li></ul><ul><li>if (answer == 1492)  </li></ul><ul><li>cout << “That’s right!
”;  </li></ul><ul>...
<ul><li>Figure 2.0: The structure of IF statement </li></ul>
<ul><li>Formatting IF/ ELSE statements </li></ul><ul><li>The two alternatives in an  if else  statement must be single sta...
<ul><li>The following code produces a compiler error: </li></ul><ul><li>if (ch == ‘Z’)  </li></ul><ul><li>zorro++;  // if ...
<ul><li>Seen as a simple if statement that ends with the zorro++; statement. Then there is a cout statement. So far, so go...
<ul><li>After adding braces </li></ul><ul><li>if (ch == ‘Z’) </li></ul><ul><li>{ // if true block  </li></ul><ul><li>zorro...
<ul><li>The SWITCH statement </li></ul><ul><li>Suppose you create a screen menu that asks the user to select one of five c...
<ul><li>The syntax for the switch statement: </li></ul><ul><li>switch (integer-expression)  </li></ul><ul><li>{  </li></ul...
<ul><li>The switch statement acts a routing device, it tells the computer which line of code to execute next. </li></ul><u...
<ul><li>Figure 3.0: The structure of SWITCH statement </li></ul>
<ul><li>Each C++ case label functions only as a line label, not as a boundary between choices. That is, after a program ju...
<ul><li>A  repetition structure  allows the programmer to specify that an action is to be repeated while some condition re...
<ul><li>The WHILE Loop </li></ul><ul><li>This a looping structure that executes a statement after a test-condition holds t...
<ul><li>Figure 4.0: The structure of a WHILE loop </li></ul>
1. Comments 2. Load  <iostream> 3.  main 3.1 Print  &quot;Welcome to C++
&quot; 3.2 exit ( return 0 ) Program Output Hello...
<ul><li>Printing a line of text </li></ul><ul><li>cout </li></ul><ul><li>- Standard output stream object </li></ul><ul><li...
<ul><li>Printing a line of text </li></ul><ul><li> </li></ul><ul><li>- Escape character </li></ul><ul><li>- Indicates that...
<ul><li>There are multiple ways of to print text </li></ul>
1. Load  <iostream> 2.  main 2.1 Print  &quot;Welcome&quot; 2.2 Print  &quot;to C++!&quot; 2.3 newline 2.4 exit ( return 0...
1. Load  <iostream> 2.  main 2.1 Print  &quot;Welcome&quot; 2.2 newline 2.3 Print  &quot;to&quot; 2.4 newline 2.5 newline ...
<ul><li>Adding Two Integers </li></ul><ul><li>Variable </li></ul><ul><ul><li>Location in memory where a value can be store...
<ul><ul><li>Example:  int myvariable; </li></ul></ul><ul><ul><ul><li>Declares a variable named  myvariable  of type  int <...
<ul><li>Load  <iostream> </li></ul><ul><li>2.  main </li></ul><ul><li>2.1 Initialize variables  integer1 ,  integer2 , and...
<ul><li>Adding Two Integers </li></ul><ul><li>>> (Stream extraction operator) </li></ul><ul><ul><li>When used with  cin , ...
<ul><li>Adding Two Integers </li></ul><ul><li>=  (assignment operator) </li></ul><ul><ul><li>Assigns value to a variable <...
<ul><li>-*-*-*- THE END -*-*-*- </li></ul>
Upcoming SlideShare
Loading in...5
×

Lecture 1

1,303

Published on

Introduction to C++ Programming

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,303
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
58
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • The IF statement selects or ignores a single action. 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • Most often, labels are simple int or char constants, such as 1or ‘q’ 09/22/09 Programming Languages C Plus Plus
  • Most often, labels are simple int or char constants, such as 1or ‘q’ 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • Transcript of "Lecture 1"

    1. 1. <ul><li>AU/MITM/1.6 </li></ul><ul><li>By Mohammed A. Saleh </li></ul>
    2. 2. <ul><li>The course runs from 13 th April – 01 st May </li></ul><ul><li>Total contact hours = 30 hrs </li></ul><ul><li>Lectures will always be conducted from </li></ul><ul><li>5 PM – 7 PM, unless stated otherwise. </li></ul><ul><li>Venue : Lab 4 (Block A, 4 th Floor ) </li></ul>
    3. 3. <ul><li>Coursework: </li></ul><ul><li>Test 1 - 20 marks </li></ul><ul><li>Test 2 - 20 marks </li></ul><ul><li>Assignment - 10 marks </li></ul><ul><li>Project - 10 marks </li></ul><ul><li> Total 60 marks </li></ul><ul><li>Final Exam - 40 marks </li></ul>
    4. 4. <ul><li>➩ Module 1 </li></ul><ul><li>Problem solving with computers problem solving techniques – Structured programming sequence structure – Selection Structure – Loop Structure – Advantages </li></ul><ul><li>➩ Module 2 </li></ul><ul><li>Fundamentals of C++ - Operators and Expressions – Data Input and Output – Control Structures – Storage Classes – Arrays and Strings </li></ul>
    5. 5. <ul><li>➩ Module 3 </li></ul><ul><li>Functions and Pointers – Structures and Unions – Files </li></ul><ul><li>➩ Module 4 </li></ul><ul><li>Principles of OOPS – Tokens, expressions and control structures, functions – classes and objects, constructors and destructors, operator overloading and type conversion </li></ul>
    6. 6. <ul><li>➩ Module 5 </li></ul><ul><li>Inheritance – Pointers – Virtual Functions – Polymorphism – Managing console I/O operations - Files </li></ul>
    7. 7. <ul><li>Decisions play a major role in providing problem solving techniques. Computer programs are all about making decisions. If a user presses a key the computer responds to the command. </li></ul><ul><li>For example if a user presses Ctrl + C, the computer copies the currently selected area to the clipboard. </li></ul><ul><li>Programs that do not make decisions are necessarily pretty boring. </li></ul>
    8. 8. <ul><li>For a computer program to decide what action to take, it uses flow-control commands. This is based on the results of the C++ logical operator. </li></ul>
    9. 9. <ul><li>Simple programming exercises can be solved by just writing code to implement the desired problem. </li></ul><ul><li>Complex programs, however can be difficult to write and impossible to debug if not implemented using a structured design process </li></ul>
    10. 10. <ul><li>Programs could be written in in terms of three control structures: </li></ul><ul><li>Sequence structure </li></ul><ul><li>Selection structure </li></ul><ul><li>Loop/ Repetition structure </li></ul>
    11. 11. <ul><li>Built into C++. Unless directed otherwise the computer executes C++ statements one after another in the order in which they are written. </li></ul><ul><li>Basic blocks Decision </li></ul><ul><li>Start </li></ul><ul><li>Stop Process </li></ul>
    12. 12. <ul><li>Basic structure </li></ul><ul><li>Figure 1.0: SEQUENCE structure </li></ul>
    13. 13. <ul><li>C++ provides three types of selection structures: </li></ul><ul><li>The IF statement ( single-selection structure ) </li></ul><ul><li>When a C++ program must choose whether to take a particular action, you usually implement the choice with an IF statement. </li></ul><ul><li>‘ If you have a Captain cookie, you get a free cookie.’ (ordinary English) </li></ul><ul><li> </li></ul>
    14. 14. <ul><li>The IF statement directs a program to execute a statement or statement block if a test condition is true and to skip that statement or block if the condition is false. Thus, an if statement lets a program decide whether a particular statement should be executed. </li></ul><ul><li>The syntax for the if statement: </li></ul><ul><li>if ( test-condition ) </li></ul><ul><li>statement </li></ul>
    15. 15. <ul><li>A true test-condition causes the program to execute statement, which can be a single statement or a block. A false test-condition causes the program to skip statement . </li></ul><ul><li>Figure 2.0: The structure of IF statement </li></ul>
    16. 16. <ul><li>if ( grade >= 60 ) </li></ul><ul><li>cout << “ Passed”; </li></ul><ul><li>The example above determines the condition ‘student’s grade is greater than or equal to 60’ is true or false . </li></ul>
    17. 17. <ul><li>The IF/ ELSE statement ( double-selection ) </li></ul><ul><li>Lets a program decide which of two statements or blocks is execute. It’s an invaluable statement for creating alternative courses of action. </li></ul><ul><li>‘ If you have a Captain Cookie card, you get a Cookie Plus Plus, else you just get an Ordinary Cookie.’ </li></ul>
    18. 18. <ul><li>The syntax for the IF/ELSE statement: </li></ul><ul><li>if ( test-condition ) </li></ul><ul><li> statement1 </li></ul><ul><li>else </li></ul><ul><li>statement2 </li></ul><ul><li>If test-condition is true , the program executes statement1 and skips over statement2 . Otherwise, when test-condition is false, the program skips statement1 and executes statement2 instead. </li></ul>
    19. 19. <ul><li>Code Fragment: </li></ul><ul><li>if (answer == 1492) </li></ul><ul><li>cout << “That’s right! ”; </li></ul><ul><li>else </li></ul><ul><li>cout << “You’d better review the topic again. ”; </li></ul><ul><li>Prints the first message if answer is 1492 and prints the second message otherwise. Each statement can be either a single statement or a statement block delimited by braces. </li></ul>
    20. 20. <ul><li>Figure 2.0: The structure of IF statement </li></ul>
    21. 21. <ul><li>Formatting IF/ ELSE statements </li></ul><ul><li>The two alternatives in an if else statement must be single statements. If you need more than one statement, you must use braces to collect them into a single block statement. </li></ul><ul><li>C++ does not automatically consider everything between if and else a block, so you have to use braces to make the statement a block. </li></ul>
    22. 22. <ul><li>The following code produces a compiler error: </li></ul><ul><li>if (ch == ‘Z’) </li></ul><ul><li>zorro++; // if ends here </li></ul><ul><li>cout << “Another Zorro candidate ”; </li></ul><ul><li>else // wrong dull++; </li></ul><ul><li>cout << “Not a Zorro candidate ”; </li></ul>
    23. 23. <ul><li>Seen as a simple if statement that ends with the zorro++; statement. Then there is a cout statement. So far, so good. But then there is what the compiler perceives as an unattached else, and that is flagged as a syntax error. </li></ul><ul><li>You need to add braces to the code to executed the way you want </li></ul>
    24. 24. <ul><li>After adding braces </li></ul><ul><li>if (ch == ‘Z’) </li></ul><ul><li>{ // if true block </li></ul><ul><li>zorro++; </li></ul><ul><li>cout << “Another Zorro candidate ”; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>dull++; </li></ul><ul><li>cout << “Not a Zorro candidate ”; } </li></ul>
    25. 25. <ul><li>The SWITCH statement </li></ul><ul><li>Suppose you create a screen menu that asks the user to select one of five choices—for example, Cheap, Moderate, Expensive, Extravagant, and Excessive. The C++ switch statement more easily handles selecting a choice from an extended list. </li></ul>
    26. 26. <ul><li>The syntax for the switch statement: </li></ul><ul><li>switch (integer-expression) </li></ul><ul><li>{ </li></ul><ul><li>case label1 : statement (s) </li></ul><ul><li>case label2: statement (s) </li></ul><ul><li>... </li></ul><ul><li>default : statement (s) </li></ul><ul><li>} </li></ul>
    27. 27. <ul><li>The switch statement acts a routing device, it tells the computer which line of code to execute next. </li></ul><ul><li>On reaching a switch statement, a program jumps to the line labeled with the value corresponding to the value of integer-expression, for example, if integer- expression has the value 4, the program goes to the line that has a case 4:label </li></ul>
    28. 28. <ul><li>Figure 3.0: The structure of SWITCH statement </li></ul>
    29. 29. <ul><li>Each C++ case label functions only as a line label, not as a boundary between choices. That is, after a program jumps to a particular line in a switch , it then sequentially executes all the statements following that line in the switch. </li></ul><ul><li>To make execution stop at the end of a particular group of statements, you must use the break statement </li></ul>
    30. 30. <ul><li>A repetition structure allows the programmer to specify that an action is to be repeated while some condition remains true. </li></ul><ul><li>C++ provides three types of selection structures: </li></ul><ul><li>The FOR Loop </li></ul><ul><li>The WHILE Loop </li></ul><ul><li>The DO … WHILE Loop </li></ul>
    31. 31. <ul><li>The WHILE Loop </li></ul><ul><li>This a looping structure that executes a statement after a test-condition holds true. </li></ul><ul><li>The syntax for the WHILE loop: </li></ul><ul><li>while ( test-condition ) </li></ul><ul><li>body </li></ul><ul><li>A program evaluates the test-condition expression. If it true, the program executes the statement(s) in the body. </li></ul>
    32. 32. <ul><li>Figure 4.0: The structure of a WHILE loop </li></ul>
    33. 33. 1. Comments 2. Load <iostream> 3. main 3.1 Print &quot;Welcome to C++ &quot; 3.2 exit ( return 0 ) Program Output Hello World ! 1 // firstcpp.cpp 2 // A first program in C++ 3 #include <iostream> 4 5 int main() 6 { 7 cout << ”Hello World! &quot;; 8 9 return 0; // indicate that program ended successfully 10 } preprocessor directive Message to the C++ preprocessor. Lines beginning with # are preprocessor directives. #include <iostream> tells the preprocessor to include the contents of the file <iostream> , which includes input/output operations (such as printing to the screen). Comments Written between /* and */ or following a // . Improve program readability and do not cause the computer to perform any action. C++ programs contain one or more functions, one of which must be main Parenthesis are used to indicate a function int means that main &quot;returns&quot; an integer value. A left brace { begins the body of every function and a right brace } ends it. Prints the string of characters contained between the quotation marks. The entire line, including std::cout , the << operator , the string ”Hello World ! &quot; and the semicolon ( ; ), is called a statement . All statements must end with a semicolon. return is a way to exit a function from a function. return 0 , in this case, means that the program terminated normally.
    34. 34. <ul><li>Printing a line of text </li></ul><ul><li>cout </li></ul><ul><li>- Standard output stream object </li></ul><ul><li>- “Connected” to the screen </li></ul><ul><li><< </li></ul><ul><li>- Stream insertion operator </li></ul><ul><ul><li>- Value to the right of the operator inserted into output stream </li></ul></ul><ul><ul><li>- cout << “ Hello World !! ”; </li></ul></ul>
    35. 35. <ul><li>Printing a line of text </li></ul><ul><li> </li></ul><ul><li>- Escape character </li></ul><ul><li>- Indicates that a “special” character is to be output </li></ul><ul><li><< </li></ul><ul><li>- Stream insertion operator </li></ul><ul><ul><li>- Value to the right of the operator inserted into output stream </li></ul></ul><ul><ul><li>- cout << “ Hello World !! ”; </li></ul></ul>
    36. 36. <ul><li>There are multiple ways of to print text </li></ul>
    37. 37. 1. Load <iostream> 2. main 2.1 Print &quot;Welcome&quot; 2.2 Print &quot;to C++!&quot; 2.3 newline 2.4 exit ( return 0 ) Program Output Hello World ! 1 // firstcpp.cpp 2 // Printing a line with multiple statements 3 #include <iostream> 4 5 int main() 6 { 7 std::cout << ”Hello &quot;; 8 std::cout << ”World ! &quot;; 9 10 return 0; // indicate that program ended successfully 11 } Unless new line ' ' is specified, the text continues on the same line.
    38. 38. 1. Load <iostream> 2. main 2.1 Print &quot;Welcome&quot; 2.2 newline 2.3 Print &quot;to&quot; 2.4 newline 2.5 newline 2.6 Print &quot;C++!&quot; 2.7 newline 2.8 exit ( return 0 ) Program Output Hello   World ! 1 // firstcpp.cpp 2 // Printing multiple lines with a single statement 3 #include <iostream> 4 5 int main() 6 { 7 std::cout << ”Hello World ! &quot;; 8 9 return 0; // indicate that program ended successfully 10 } Multiple lines can be printed with one statement.
    39. 39. <ul><li>Adding Two Integers </li></ul><ul><li>Variable </li></ul><ul><ul><li>Location in memory where a value can be stored for use by a program </li></ul></ul><ul><ul><li>Must be declared with a name and a data type before they can be used </li></ul></ul><ul><ul><li>Some common data types are: </li></ul></ul><ul><ul><ul><li>int - integer numbers </li></ul></ul></ul><ul><ul><ul><li>char - characters </li></ul></ul></ul><ul><ul><ul><li>double - floating point numbers </li></ul></ul></ul>
    40. 40. <ul><ul><li>Example: int myvariable; </li></ul></ul><ul><ul><ul><li>Declares a variable named myvariable of type int </li></ul></ul></ul><ul><ul><li>Example: int variable1, variable2; </li></ul></ul><ul><ul><ul><li>Declares two variables, each of type int </li></ul></ul></ul>
    41. 41. <ul><li>Load <iostream> </li></ul><ul><li>2. main </li></ul><ul><li>2.1 Initialize variables integer1 , integer2 , and sum </li></ul><ul><li>2.2 Print &quot;Enter first integer&quot; </li></ul><ul><li>2.2.1 Get input </li></ul><ul><li>2.3 Print &quot;Enter second integer&quot; </li></ul><ul><li>2.3.1 Get input </li></ul><ul><li>2.4 Add variables and put result into sum </li></ul><ul><li>2.5 Print &quot;Sum is&quot; </li></ul><ul><li>2.5.1 Output sum </li></ul><ul><li>2.6 exit ( return 0 ) </li></ul><ul><li>Program Output </li></ul>Enter first integer 45 Enter second integer 72 Sum is 117 1 // Add.cpp 2 // Addition program 3 #include <iostream> 4 5 int main() 6 { 7 int integer1, integer2, sum; // declaration 8 9 cout << &quot;Enter first integer &quot;; // prompt 10 cin >> integer1; // read an integer 11 cout << &quot;Enter second integer &quot;; // prompt 12 cin >> integer2; // read an integer 13 sum = integer1 + integer2; // assignment of sum 14 cout << &quot;Sum is &quot; << sum << endl; // print sum 15 16 return 0; // indicate that program ended successfully 17 } Notice how cin is used to get user input. Variables can be output using cout << variableName . endl flushes the buffer and prints a newline.
    42. 42. <ul><li>Adding Two Integers </li></ul><ul><li>>> (Stream extraction operator) </li></ul><ul><ul><li>When used with cin , waits for the user to input a value and stores the value in the variable to the right of the operator </li></ul></ul><ul><ul><li>The user types a value, then presses the Enter (Return) key to send the data to the computer </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><ul><li>int myVariable; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>std::cin >> myVariable; </li></ul></ul></ul></ul><ul><ul><ul><li>Waits for user input, then stores input in myVariable </li></ul></ul></ul>
    43. 43. <ul><li>Adding Two Integers </li></ul><ul><li>= (assignment operator) </li></ul><ul><ul><li>Assigns value to a variable </li></ul></ul><ul><ul><li>Binary operator (has two operands) </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><ul><li>sum = variable1 + variable2; </li></ul></ul></ul></ul>
    44. 44. <ul><li>-*-*-*- THE END -*-*-*- </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×