AU/MITM/1.6 By Mohammed A. Saleh
The course runs from 13 th  April – 01 st  May Total contact hours = 30 hrs Lectures will always be conducted from  5 PM – 7 PM, unless stated otherwise. Venue : Lab 4 (Block A, 4 th  Floor )
Coursework: Test 1 - 20 marks Test 2  - 20 marks Assignment - 10 marks Project - 10 marks   Total  60 marks Final Exam - 40 marks
➩ Module 1 Problem solving with computers problem solving techniques – Structured programming sequence structure – Selection Structure – Loop Structure – Advantages ➩ Module 2 Fundamentals of C++ - Operators and Expressions – Data Input and Output – Control Structures – Storage Classes – Arrays and Strings
➩ Module 3 Functions and Pointers – Structures and Unions – Files ➩ Module 4 Principles of OOPS – Tokens, expressions and control structures, functions – classes and objects, constructors and destructors, operator overloading and type conversion
➩ Module 5 Inheritance – Pointers – Virtual Functions – Polymorphism – Managing console I/O operations - Files
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. For example if a user presses Ctrl + C, the computer copies the currently selected area to the clipboard. Programs that do not make decisions are necessarily pretty boring.
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.
Simple programming exercises can be solved by just writing code to implement the desired problem. Complex programs, however can be difficult to write and impossible to debug if not implemented using a structured design process
Programs could be written in in terms of three control structures: Sequence structure Selection structure  Loop/ Repetition structure
Built into C++. Unless directed otherwise the computer executes C++ statements one after another in the order in which they are written. Basic blocks Decision Start  Stop   Process
Basic structure Figure 1.0: SEQUENCE structure
C++ provides three types of selection structures: The IF statement ( single-selection structure )  When a C++ program must choose whether to take a particular action, you usually implement the choice with an IF statement. ‘ If you have a Captain cookie, you get a free cookie.’ (ordinary English)
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. The syntax for the if statement: if ( test-condition )  statement
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 . Figure 2.0: The structure of IF statement
if ( grade >= 60 ) cout << “ Passed”; The example above determines the condition ‘student’s grade is greater than or equal to 60’ is  true  or  false .
The IF/ ELSE statement ( double-selection ) Lets a program decide which of two statements or blocks is execute. It’s an invaluable statement for creating alternative courses of action. ‘ If you have a Captain Cookie card, you get a Cookie Plus Plus, else you just get an Ordinary Cookie.’
The syntax for the IF/ELSE statement: if ( test-condition )   statement1 else  statement2 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.
Code Fragment:  if (answer == 1492)  cout << “That’s right!\n”;  else  cout << “You’d better review the topic    again.\n”; 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.
Figure 2.0: The structure of IF statement
Formatting IF/ ELSE statements 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. C++ does not automatically consider everything between  if  and  else  a block, so you have to use braces to make the statement a block.
The following code produces a compiler error: if (ch == ‘Z’)  zorro++;  // if ends here cout << “Another Zorro candidate\n”;  else  // wrong dull++; cout << “Not a Zorro candidate\n”;
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. You need to add braces to the code to executed the way you want
After adding braces if (ch == ‘Z’) { // if true block  zorro++;  cout << “Another Zorro candidate\n”; }  else {  dull++; cout << “Not a Zorro candidate\n”; }
The SWITCH statement 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.
The syntax for the switch statement: switch (integer-expression)  {  case label1 : statement (s)  case label2: statement (s)  ...  default  : statement (s) }
The switch statement acts a routing device, it tells the computer which line of code to execute next. 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
Figure 3.0: The structure of SWITCH statement
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. To make execution stop at the end of a particular group of statements, you must use the  break  statement
A  repetition structure  allows the programmer to specify that an action is to be repeated while some condition remains true. C++ provides three types of selection structures: The FOR Loop The WHILE Loop The DO … WHILE Loop
The WHILE Loop This a looping structure that executes a statement after a test-condition holds true. The syntax for the WHILE loop: while ( test-condition )  body  A program evaluates the  test-condition  expression. If it true, the program executes the statement(s) in the body.
Figure 4.0: The structure of a WHILE loop
1. Comments 2. Load  <iostream> 3.  main 3.1 Print  &quot;Welcome to C++\n&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!\n&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 !\n&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.
Printing a line of text cout - Standard output stream object - “Connected” to the screen << - Stream insertion operator -  Value to the right of the operator inserted into output stream - cout << “ Hello World !! \n”;
Printing a line of text \ - Escape character - Indicates that a “special” character is to be output << - Stream insertion operator -  Value to the right of the operator inserted into output stream - cout << “ Hello World !! \n”;
There are multiple ways of to print text
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 !\n&quot;; 9 10   return  0;  // indicate that program ended successfully 11 } Unless new line  '\n'  is specified, the text continues on the same line.
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\n\n World !\n&quot;; 8 9   return  0;  // indicate that program ended successfully 10 } Multiple lines can be printed with one statement.
Adding Two Integers Variable Location in memory where a value can be stored for use by a program Must be declared with a name and a data type before they can be used Some common data types are: int  - integer numbers char  - characters double  - floating point numbers
Example:  int myvariable; Declares a variable named  myvariable  of type  int Example:  int variable1, variable2; Declares two variables, each of type  int
Load  <iostream> 2.  main 2.1 Initialize variables  integer1 ,  integer2 , and  sum 2.2 Print  &quot;Enter first integer&quot; 2.2.1 Get input 2.3 Print  &quot;Enter second integer&quot; 2.3.1 Get input 2.4 Add variables and put result into  sum 2.5 Print  &quot;Sum is&quot; 2.5.1 Output  sum 2.6 exit ( return 0 ) Program Output 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\n&quot;;  // prompt 10   cin >> integer1;  // read an integer 11   cout << &quot;Enter second integer\n&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.
Adding Two Integers >> (Stream extraction operator) 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 The user types a value, then presses the  Enter  (Return) key to send the data to the computer Example: int myVariable; std::cin >> myVariable; Waits for user input, then stores input in  myVariable
Adding Two Integers =  (assignment operator) Assigns value to a variable Binary operator (has two operands) Example: sum = variable1 + variable2;
-*-*-*- THE END -*-*-*-

Lecture 1

  • 1.
  • 2.
    The course runsfrom 13 th April – 01 st May Total contact hours = 30 hrs Lectures will always be conducted from 5 PM – 7 PM, unless stated otherwise. Venue : Lab 4 (Block A, 4 th Floor )
  • 3.
    Coursework: Test 1- 20 marks Test 2 - 20 marks Assignment - 10 marks Project - 10 marks Total 60 marks Final Exam - 40 marks
  • 4.
    ➩ Module 1Problem solving with computers problem solving techniques – Structured programming sequence structure – Selection Structure – Loop Structure – Advantages ➩ Module 2 Fundamentals of C++ - Operators and Expressions – Data Input and Output – Control Structures – Storage Classes – Arrays and Strings
  • 5.
    ➩ Module 3Functions and Pointers – Structures and Unions – Files ➩ Module 4 Principles of OOPS – Tokens, expressions and control structures, functions – classes and objects, constructors and destructors, operator overloading and type conversion
  • 6.
    ➩ Module 5Inheritance – Pointers – Virtual Functions – Polymorphism – Managing console I/O operations - Files
  • 7.
    Decisions play amajor 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. For example if a user presses Ctrl + C, the computer copies the currently selected area to the clipboard. Programs that do not make decisions are necessarily pretty boring.
  • 8.
    For a computerprogram to decide what action to take, it uses flow-control commands. This is based on the results of the C++ logical operator.
  • 9.
    Simple programming exercisescan be solved by just writing code to implement the desired problem. Complex programs, however can be difficult to write and impossible to debug if not implemented using a structured design process
  • 10.
    Programs could bewritten in in terms of three control structures: Sequence structure Selection structure Loop/ Repetition structure
  • 11.
    Built into C++.Unless directed otherwise the computer executes C++ statements one after another in the order in which they are written. Basic blocks Decision Start Stop Process
  • 12.
    Basic structure Figure1.0: SEQUENCE structure
  • 13.
    C++ provides threetypes of selection structures: The IF statement ( single-selection structure ) When a C++ program must choose whether to take a particular action, you usually implement the choice with an IF statement. ‘ If you have a Captain cookie, you get a free cookie.’ (ordinary English)
  • 14.
    The IF statementdirects 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. The syntax for the if statement: if ( test-condition ) statement
  • 15.
    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 . Figure 2.0: The structure of IF statement
  • 16.
    if ( grade>= 60 ) cout << “ Passed”; The example above determines the condition ‘student’s grade is greater than or equal to 60’ is true or false .
  • 17.
    The IF/ ELSEstatement ( double-selection ) Lets a program decide which of two statements or blocks is execute. It’s an invaluable statement for creating alternative courses of action. ‘ If you have a Captain Cookie card, you get a Cookie Plus Plus, else you just get an Ordinary Cookie.’
  • 18.
    The syntax forthe IF/ELSE statement: if ( test-condition ) statement1 else statement2 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.
  • 19.
    Code Fragment: if (answer == 1492) cout << “That’s right!\n”; else cout << “You’d better review the topic again.\n”; 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.
  • 20.
    Figure 2.0: Thestructure of IF statement
  • 21.
    Formatting IF/ ELSEstatements 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. C++ does not automatically consider everything between if and else a block, so you have to use braces to make the statement a block.
  • 22.
    The following codeproduces a compiler error: if (ch == ‘Z’) zorro++; // if ends here cout << “Another Zorro candidate\n”; else // wrong dull++; cout << “Not a Zorro candidate\n”;
  • 23.
    Seen as asimple 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. You need to add braces to the code to executed the way you want
  • 24.
    After adding bracesif (ch == ‘Z’) { // if true block zorro++; cout << “Another Zorro candidate\n”; } else { dull++; cout << “Not a Zorro candidate\n”; }
  • 25.
    The SWITCH statementSuppose 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.
  • 26.
    The syntax forthe switch statement: switch (integer-expression) { case label1 : statement (s) case label2: statement (s) ... default : statement (s) }
  • 27.
    The switch statementacts a routing device, it tells the computer which line of code to execute next. 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
  • 28.
    Figure 3.0: Thestructure of SWITCH statement
  • 29.
    Each C++ caselabel 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. To make execution stop at the end of a particular group of statements, you must use the break statement
  • 30.
    A repetitionstructure allows the programmer to specify that an action is to be repeated while some condition remains true. C++ provides three types of selection structures: The FOR Loop The WHILE Loop The DO … WHILE Loop
  • 31.
    The WHILE LoopThis a looping structure that executes a statement after a test-condition holds true. The syntax for the WHILE loop: while ( test-condition ) body A program evaluates the test-condition expression. If it true, the program executes the statement(s) in the body.
  • 32.
    Figure 4.0: Thestructure of a WHILE loop
  • 33.
    1. Comments 2.Load <iostream> 3. main 3.1 Print &quot;Welcome to C++\n&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!\n&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 !\n&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.
    Printing a lineof text cout - Standard output stream object - “Connected” to the screen << - Stream insertion operator - Value to the right of the operator inserted into output stream - cout << “ Hello World !! \n”;
  • 35.
    Printing a lineof text \ - Escape character - Indicates that a “special” character is to be output << - Stream insertion operator - Value to the right of the operator inserted into output stream - cout << “ Hello World !! \n”;
  • 36.
    There are multipleways of to print text
  • 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 !\n&quot;; 9 10 return 0; // indicate that program ended successfully 11 } Unless new line '\n' is specified, the text continues on the same line.
  • 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\n\n World !\n&quot;; 8 9 return 0; // indicate that program ended successfully 10 } Multiple lines can be printed with one statement.
  • 39.
    Adding Two IntegersVariable Location in memory where a value can be stored for use by a program Must be declared with a name and a data type before they can be used Some common data types are: int - integer numbers char - characters double - floating point numbers
  • 40.
    Example: intmyvariable; Declares a variable named myvariable of type int Example: int variable1, variable2; Declares two variables, each of type int
  • 41.
    Load <iostream>2. main 2.1 Initialize variables integer1 , integer2 , and sum 2.2 Print &quot;Enter first integer&quot; 2.2.1 Get input 2.3 Print &quot;Enter second integer&quot; 2.3.1 Get input 2.4 Add variables and put result into sum 2.5 Print &quot;Sum is&quot; 2.5.1 Output sum 2.6 exit ( return 0 ) Program Output 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\n&quot;; // prompt 10 cin >> integer1; // read an integer 11 cout << &quot;Enter second integer\n&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.
    Adding Two Integers>> (Stream extraction operator) 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 The user types a value, then presses the Enter (Return) key to send the data to the computer Example: int myVariable; std::cin >> myVariable; Waits for user input, then stores input in myVariable
  • 43.
    Adding Two Integers= (assignment operator) Assigns value to a variable Binary operator (has two operands) Example: sum = variable1 + variable2;
  • 44.

Editor's Notes

  • #2 09/22/09 Programming Languages C Plus Plus
  • #3 09/22/09 Programming Languages C Plus Plus
  • #4 09/22/09 Programming Languages C Plus Plus
  • #14 The IF statement selects or ignores a single action. 09/22/09 Programming Languages C Plus Plus
  • #15 09/22/09 Programming Languages C Plus Plus
  • #16 09/22/09 Programming Languages C Plus Plus
  • #17 09/22/09 Programming Languages C Plus Plus
  • #18 09/22/09 Programming Languages C Plus Plus
  • #19 09/22/09 Programming Languages C Plus Plus
  • #20 09/22/09 Programming Languages C Plus Plus
  • #21 09/22/09 Programming Languages C Plus Plus
  • #22 09/22/09 Programming Languages C Plus Plus
  • #23 09/22/09 Programming Languages C Plus Plus
  • #24 09/22/09 Programming Languages C Plus Plus
  • #25 09/22/09 Programming Languages C Plus Plus
  • #26 09/22/09 Programming Languages C Plus Plus
  • #27 09/22/09 Programming Languages C Plus Plus
  • #28 09/22/09 Programming Languages C Plus Plus
  • #29 Most often, labels are simple int or char constants, such as 1or ‘q’ 09/22/09 Programming Languages C Plus Plus
  • #30 Most often, labels are simple int or char constants, such as 1or ‘q’ 09/22/09 Programming Languages C Plus Plus
  • #31 09/22/09 Programming Languages C Plus Plus
  • #32 09/22/09 Programming Languages C Plus Plus
  • #33 09/22/09 Programming Languages C Plus Plus
  • #34 09/22/09 Programming Languages C Plus Plus
  • #35 09/22/09 Programming Languages C Plus Plus
  • #36 09/22/09 Programming Languages C Plus Plus
  • #37 09/22/09 Programming Languages C Plus Plus
  • #40 09/22/09 Programming Languages C Plus Plus
  • #41 09/22/09 Programming Languages C Plus Plus
  • #43 09/22/09 Programming Languages C Plus Plus
  • #44 09/22/09 Programming Languages C Plus Plus
  • #45 09/22/09 Programming Languages C Plus Plus