Lecture 1
Upcoming SlideShare
Loading in...5
×
 

Lecture 1

on

  • 712 views

Introduction to C++ Programming

Introduction to C++ Programming

Statistics

Views

Total Views
712
Views on SlideShare
712
Embed Views
0

Actions

Likes
0
Downloads
30
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 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

Lecture 1 Lecture 1 Presentation Transcript

    • 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! ”;
    • else
    • cout << “You’d better review the topic again. ”;
    • 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 ”;
    • else // wrong dull++;
    • cout << “Not a Zorro candidate ”;
    • 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 ”;
    • }
    • else {
    • dull++;
    • cout << “Not a Zorro candidate ”; }
    • 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++ &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.
    • 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 !! ”;
    • 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 !! ”;
    • 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 ! &quot;; 9 10 return 0; // indicate that program ended successfully 11 } Unless new line ' ' 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 World ! &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 &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.
    • 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 -*-*-*-