Chapter  2 Introduction to C++ Programming C++, How to Program Deitel  &  Deitel
Learn C++ by Examples <ul><li>Five examples demonstrate </li></ul><ul><ul><li>How to display messages </li></ul></ul><ul><...
Important Parts of a C++ program <ul><li>Comments:  //, /* …. */ </li></ul><ul><li>Preprocessor directives  :  #include </...
Comments <ul><li>Explain programs to other programmers </li></ul><ul><ul><li>Improve program readability </li></ul></ul><u...
Preprocessor Directives <ul><li>Preprocessor directives </li></ul><ul><ul><li>Processed by preprocessor before compiling <...
Function  main <ul><li>A part of every C++ program </li></ul><ul><ul><li>Exactly  one  function in a program must be  main...
Statements <ul><li>Instruct the program to perform an action </li></ul><ul><li>All statements end with a semicolon ( ; ) <...
return  Statement <ul><li>One of several means to exit a function </li></ul><ul><li>When used at the end of  main </li></u...
Output Statement (1) <ul><li>std::cout << “Welcome to C++!n”; </li></ul><ul><ul><li>std::cout   </li></ul></ul><ul><ul><ul...
Output Statement (2) <ul><li>Escape character  :   backslash :  &quot;  &quot; </li></ul><ul><li>Escape sequence :  A char...
Good Programming Practices <ul><li>Add comments </li></ul><ul><ul><li>Every program should begin with a comment that descr...
Run C++ Program <ul><li>Save the program with the right extension </li></ul><ul><ul><li>programOne.cpp </li></ul></ul><ul>...
Modifying Our First C++ Program <ul><li>Print text on one line using multiple statements  </li></ul><ul><ul><li>Each strea...
Modifying Our First C++ Program <ul><li>Print text on several lines using a single statement . </li></ul><ul><ul><li>Each ...
Variable <ul><li>Location in memory where value can be stored </li></ul><ul><li>Common data types (fundamental, primitive ...
Declaration of Variables (1) <ul><li>Declare variables with name and data type  before  they are  use d. </li></ul><ul><li...
Declaration of Variables (2) <ul><li>Can Declare each variable on a separate line. </li></ul><ul><ul><li>int integer1; </l...
Assign Value to Variables <ul><li>Assignment operator  = </li></ul><ul><ul><li>Assigns value on  right  to variable on  le...
Input stream object <ul><li>std::cin  from  <iostream> </li></ul><ul><ul><li>Usually connected to keyboard </li></ul></ul>...
Constant variables <ul><li>Declared using the  const  qualifier </li></ul><ul><li>Also called named constants or read-only...
Arithmetic (1) <ul><li>Arithmetic operators </li></ul><ul><ul><li>+ :  Addition </li></ul></ul><ul><ul><li>-  :  Subtracti...
Arithmetic operators
Arithmetic (2) <ul><li>Straight-line form </li></ul><ul><ul><li>Required for arithmetic expressions in C++ </li></ul></ul>...
Rules of operator precedence <ul><li>Operators in parentheses evaluated first </li></ul><ul><ul><li>Nested/embedded parent...
Precedence of Arithmetic Operators
Performing Arithmetic Calculation in C++ Program <ul><li>Adding two integers </li></ul><ul><ul><li>int number1, number2, s...
Question <ul><li>Question : </li></ul><ul><li>int integerOne = 1; </li></ul><ul><ul><li>double doubleOne = 2.5; </li></ul>...
Output Statement With Expression <ul><li>Variables or expressions could be inserted into output statement, its value is pr...
End of Line <ul><li>Stream manipulator  std::endl </li></ul><ul><ul><li>Outputs a newline </li></ul></ul><ul><ul><li>Flush...
Memory Concepts <ul><li>Variable names </li></ul><ul><ul><li>Correspond to actual locations in computer's memory </li></ul...
Fig. 2.6   | Memory location showing the name and value of variable number1.
Fig. 2.7   | Memory locations after storing values for number1 and number2.
Fig. 2.8   | Memory locations after calculating and storing the sum of number1 and number2.
Type Sizes and Ranges <ul><li>The size and range of any data type is compiler and architecture dependent.  </li></ul><ul><...
Reference <ul><li>Reproduced from the Cyber Classroom for C++, How to Program , 5/e by Deitel & Deitel.  </li></ul><ul><li...
Upcoming SlideShare
Loading in …5
×

Chapter2

579 views

Published on

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

No Downloads
Views
Total views
579
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
54
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Chapter2

  1. 1. Chapter 2 Introduction to C++ Programming C++, How to Program Deitel & Deitel
  2. 2. Learn C++ by Examples <ul><li>Five examples demonstrate </li></ul><ul><ul><li>How to display messages </li></ul></ul><ul><ul><li>How to obtain information from the user </li></ul></ul><ul><ul><li>How to perform arithmetic calculations </li></ul></ul>
  3. 3. Important Parts of a C++ program <ul><li>Comments: //, /* …. */ </li></ul><ul><li>Preprocessor directives : #include </li></ul><ul><li>Function main </li></ul><ul><ul><li>Body of the function </li></ul></ul><ul><ul><li>Return statement </li></ul></ul><ul><ul><li>Other statements </li></ul></ul>
  4. 4. Comments <ul><li>Explain programs to other programmers </li></ul><ul><ul><li>Improve program readability </li></ul></ul><ul><li>Ignored by compiler </li></ul><ul><li>Single-line comment </li></ul><ul><ul><li>Begin with // </li></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>// allows program to output data to the screen . </li></ul></ul></ul><ul><li>Multi-line comment </li></ul><ul><ul><li>Start with /* </li></ul></ul><ul><ul><li>End with */ </li></ul></ul>
  5. 5. Preprocessor Directives <ul><li>Preprocessor directives </li></ul><ul><ul><li>Processed by preprocessor before compiling </li></ul></ul><ul><ul><li>Begin with # </li></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>#include <iostream> </li></ul></ul></ul><ul><ul><ul><ul><li>Tells preprocessor to include the input/output stream header file <iostream> </li></ul></ul></ul></ul><ul><li>White space s </li></ul><ul><ul><li>Blank lines, space characters and tabs </li></ul></ul><ul><ul><li>Delimiter, used to make programs easier to read </li></ul></ul><ul><ul><li>Extra spaces are ignored by the compiler </li></ul></ul>
  6. 6. Function main <ul><li>A part of every C++ program </li></ul><ul><ul><li>Exactly one function in a program must be main </li></ul></ul><ul><ul><li>main is a Keyword. </li></ul></ul><ul><ul><ul><li>Keyword : A word in code that is reserved by C++ for a specific use. </li></ul></ul></ul><ul><li>Header of function main : int main( ) </li></ul><ul><li>Body is delimited by braces ( { } ) </li></ul>
  7. 7. Statements <ul><li>Instruct the program to perform an action </li></ul><ul><li>All statements end with a semicolon ( ; ) </li></ul><ul><li>Examples : </li></ul><ul><ul><li>return 0; </li></ul></ul><ul><ul><li>std::cout << “Welcome to C++!n ”; </li></ul></ul>
  8. 8. return Statement <ul><li>One of several means to exit a function </li></ul><ul><li>When used at the end of main </li></ul><ul><ul><li>The value 0 indicates the program terminated successfully </li></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>return 0; </li></ul></ul></ul>
  9. 9. Output Statement (1) <ul><li>std::cout << “Welcome to C++!n”; </li></ul><ul><ul><li>std::cout </li></ul></ul><ul><ul><ul><li>Standard output stream object . </li></ul></ul></ul><ul><ul><ul><li>Defined in input/output stream header file <iostream> </li></ul></ul></ul><ul><ul><ul><li>We are using a name ( cout ) that belongs to “namespace” std . </li></ul></ul></ul><ul><ul><ul><li>Norm ally outputs to computer screen . </li></ul></ul></ul><ul><ul><li>Stream insertion operator << </li></ul></ul><ul><ul><ul><li>Value to right (right operand) inserted into left operand . </li></ul></ul></ul><ul><ul><ul><li>The string of characters contained between “ ” after the operator << shows on computer screen. </li></ul></ul></ul>
  10. 10. Output Statement (2) <ul><li>Escape character : backslash : &quot; &quot; </li></ul><ul><li>Escape sequence : A character preceded by backslash ( ) </li></ul><ul><ul><li>Indicates “special” character output </li></ul></ul><ul><ul><li>Example s : </li></ul></ul><ul><ul><ul><li>&quot;n&quot; </li></ul></ul></ul><ul><ul><ul><ul><li>Newline. Cursor moves to beginning of next line on the screen </li></ul></ul></ul></ul><ul><ul><ul><li>“ t” </li></ul></ul></ul><ul><ul><ul><ul><li>Horizontal tab. Move the screen cursor to the next tab stop. </li></ul></ul></ul></ul>
  11. 11. Good Programming Practices <ul><li>Add comments </li></ul><ul><ul><li>Every program should begin with a comment that describes the purpose of the program, author, date and time. </li></ul></ul><ul><li>Use good indentation </li></ul><ul><ul><li>Indent the entire body of each function one level within the braces that delimit the body of the function. This makes a program’s functional structure stand out and helps make the program easier to read. </li></ul></ul>
  12. 12. Run C++ Program <ul><li>Save the program with the right extension </li></ul><ul><ul><li>programOne.cpp </li></ul></ul><ul><li>Send the program in the right directory </li></ul><ul><li>Compiling the program: </li></ul><ul><ul><li>g++ programOne.cpp –o programOne.out </li></ul></ul><ul><li>Run the executable file </li></ul><ul><ul><li>./programOne.out </li></ul></ul>
  13. 13. Modifying Our First C++ Program <ul><li>Print text on one line using multiple statements </li></ul><ul><ul><li>Each stream insertion resumes printing where the previous one stopped </li></ul></ul><ul><ul><li>Statements: </li></ul></ul><ul><ul><li>Std::cout << “Welcome ”; </li></ul></ul><ul><ul><li>Std::cout << “to C++!n”; </li></ul></ul>
  14. 14. Modifying Our First C++ Program <ul><li>Print text on several lines using a single statement . </li></ul><ul><ul><li>Each newline escape sequence positions the cursor to the beginning of the next line </li></ul></ul><ul><ul><li>Two newline characters back to back outputs a blank line </li></ul></ul><ul><ul><li>Example statement : </li></ul></ul><ul><ul><li>Std::cout << “Welcome n to nn C++! n ”; </li></ul></ul>
  15. 15. Variable <ul><li>Location in memory where value can be stored </li></ul><ul><li>Common data types (fundamental, primitive or built-in) </li></ul><ul><ul><li>int – integer numbers : 1, 2, 4,…. </li></ul></ul><ul><ul><li>char – characters : ‘a’, ‘c’, … </li></ul></ul><ul><ul><li>float, double – floating point numbers: 2.5, 4.96 </li></ul></ul><ul><li>The value of a variable could be changed while the program is running. </li></ul>
  16. 16. Declaration of Variables (1) <ul><li>Declare variables with name and data type before they are use d. </li></ul><ul><li>A variable name is any valid identifier that is not a keyword. </li></ul><ul><ul><li>Series of characters - letters, digits, underscores ( _ ) </li></ul></ul><ul><ul><li>Cannot begin with digit </li></ul></ul><ul><ul><li>Case sensitive </li></ul></ul><ul><ul><li>Choosing meaningful identifiers helps make a program self-documenting . </li></ul></ul>
  17. 17. Declaration of Variables (2) <ul><li>Can Declare each variable on a separate line. </li></ul><ul><ul><li>int integer1; </li></ul></ul><ul><ul><li>int integer2; </li></ul></ul><ul><ul><li>int sum; </li></ul></ul><ul><li>Can declare several variables of same type in one declaration . </li></ul><ul><ul><li>Comma-separated list </li></ul></ul><ul><ul><li>int integer1, integer2, sum; </li></ul></ul>
  18. 18. Assign Value to Variables <ul><li>Assignment operator = </li></ul><ul><ul><li>Assigns value on right to variable on left </li></ul></ul><ul><ul><li>Binary operator (two operands) </li></ul></ul><ul><li>Assign a value after declaration </li></ul><ul><ul><li>int integer1; //declaration </li></ul></ul><ul><ul><li>integer1 = 10; //assignment </li></ul></ul><ul><li>Declare and assign a value at the same time. </li></ul><ul><ul><li>int integer2 = 20; </li></ul></ul>
  19. 19. Input stream object <ul><li>std::cin from <iostream> </li></ul><ul><ul><li>Usually connected to keyboard </li></ul></ul><ul><ul><li>Stream extraction operator >> </li></ul></ul><ul><ul><ul><li>Waits for user to input value, press Enter (Return) key </li></ul></ul></ul><ul><ul><ul><li>Stores value in variable to right of operator </li></ul></ul></ul><ul><ul><ul><ul><li>Converts value to variable data type </li></ul></ul></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>int number1; </li></ul></ul></ul><ul><ul><ul><li>std::cin >> number1; </li></ul></ul></ul><ul><ul><ul><ul><li>Reads an integer typed at the keyboard </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Stores the integer in variable number1 </li></ul></ul></ul></ul>
  20. 20. Constant variables <ul><li>Declared using the const qualifier </li></ul><ul><li>Also called named constants or read-only variables </li></ul><ul><li>Must be initialized with a constant expression when they are declared and cannot be modified thereafter </li></ul><ul><li>Example: const int size = 5; </li></ul>
  21. 21. Arithmetic (1) <ul><li>Arithmetic operators </li></ul><ul><ul><li>+ : Addition </li></ul></ul><ul><ul><li>- : Subtraction </li></ul></ul><ul><ul><li>* : Multiplication </li></ul></ul><ul><ul><li>/ : Division </li></ul></ul><ul><ul><ul><li>Integer division truncates remainder </li></ul></ul></ul><ul><ul><ul><ul><li>7 / 5 evaluates to 1 </li></ul></ul></ul></ul><ul><ul><li>% : Modulus operator returns remainder </li></ul></ul><ul><ul><ul><li>7 % 5 evaluates to 2 </li></ul></ul></ul><ul><ul><ul><li>Attempting to use the modulus operator ( % ) with </li></ul></ul></ul><ul><ul><ul><li>non - integer operands is a compilation error. </li></ul></ul></ul>
  22. 22. Arithmetic operators
  23. 23. Arithmetic (2) <ul><li>Straight-line form </li></ul><ul><ul><li>Required for arithmetic expressions in C++ </li></ul></ul><ul><ul><li>All constants, variables and operators appear in a straight line </li></ul></ul><ul><li>Grouping sub - expressions </li></ul><ul><ul><li>Parentheses are used in C++ expressions to group sub - expressions </li></ul></ul><ul><ul><ul><li>Same manner as in algebraic expressions </li></ul></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>a * ( b + c ) </li></ul></ul></ul><ul><ul><ul><ul><li>Multiple a times the quantity b + c </li></ul></ul></ul></ul>
  24. 24. Rules of operator precedence <ul><li>Operators in parentheses evaluated first </li></ul><ul><ul><li>Nested/embedded parentheses </li></ul></ul><ul><ul><ul><li>Operators in innermost pair first </li></ul></ul></ul><ul><li>Multiplication, division, modulus applied next </li></ul><ul><ul><li>Operators applied from left to right </li></ul></ul><ul><li>Addition, subtraction applied last </li></ul><ul><ul><li>Operators applied from left to right </li></ul></ul>
  25. 25. Precedence of Arithmetic Operators
  26. 26. Performing Arithmetic Calculation in C++ Program <ul><li>Adding two integers </li></ul><ul><ul><li>int number1, number2, sum; </li></ul></ul><ul><ul><li>std::cin >> number1; </li></ul></ul><ul><ul><li>std::cin >> number2; </li></ul></ul><ul><ul><li>sum = number 1 + number 2; </li></ul></ul><ul><ul><ul><li>Add the values of variable1 and variable2 </li></ul></ul></ul><ul><ul><ul><li>Store result in sum </li></ul></ul></ul>
  27. 27. Question <ul><li>Question : </li></ul><ul><li>int integerOne = 1; </li></ul><ul><ul><li>double doubleOne = 2.5; </li></ul></ul><ul><ul><li>int sumOne = integerOne + doubleOne; //A </li></ul></ul><ul><ul><li>double sumTwo = integerOne + doubleOne; //B </li></ul></ul><ul><li>Which statement is correct , A or B ? </li></ul>
  28. 28. Output Statement With Expression <ul><li>Variables or expressions could be inserted into output statement, its value is printed out. </li></ul><ul><li>Long output statement could be broken into multiple lines. </li></ul><ul><li>Example: </li></ul><ul><li>std::cout << “sum is ” << sum </li></ul><ul><li> << “. bye-bye! n”; </li></ul><ul><li>std::cout << “sum is ” </li></ul><ul><li> << number1 + number2 ; </li></ul>
  29. 29. End of Line <ul><li>Stream manipulator std::endl </li></ul><ul><ul><li>Outputs a newline </li></ul></ul><ul><ul><li>Flushes the output buffer </li></ul></ul>
  30. 30. Memory Concepts <ul><li>Variable names </li></ul><ul><ul><li>Correspond to actual locations in computer's memory </li></ul></ul><ul><ul><ul><li>Every variable has name, type, size and value </li></ul></ul></ul><ul><ul><li>When new value placed into variable, overwrites old value </li></ul></ul><ul><ul><ul><li>Writing to memory is destructive </li></ul></ul></ul><ul><ul><li>Reading variables from memory nondestructive </li></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>sum = number1 + number2; </li></ul></ul></ul><ul><ul><ul><ul><li>Value of sum is overwritten </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Values of number1 and number2 remain intact </li></ul></ul></ul></ul>
  31. 31. Fig. 2.6 | Memory location showing the name and value of variable number1.
  32. 32. Fig. 2.7 | Memory locations after storing values for number1 and number2.
  33. 33. Fig. 2.8 | Memory locations after calculating and storing the sum of number1 and number2.
  34. 34. Type Sizes and Ranges <ul><li>The size and range of any data type is compiler and architecture dependent. </li></ul><ul><li>M any architectures implement data types of a standard size. ints and floats are often 32-bit, chars 8-bit, and doubles are usually 64-bit. </li></ul>
  35. 35. Reference <ul><li>Reproduced from the Cyber Classroom for C++, How to Program , 5/e by Deitel & Deitel. </li></ul><ul><li>Reproduced by permission of Pearson Education, Inc. </li></ul>

×