by shervin daneshpajouh legend legend legend legend
play

By Shervin Daneshpajouh Legend Legend Legend Legend Software - PowerPoint PPT Presentation

By Shervin Daneshpajouh Legend Legend Legend Legend Software Engineering Observation g g Performance tip Portability tip Good programming practice Common programming error Error prevention tip Error prevention tip 3 Shervin Daneshpajouh


  1. By Shervin Daneshpajouh

  2. Legend Legend Legend Legend Software Engineering Observation g g Performance tip Portability tip Good programming practice Common programming error Error ‐ prevention tip Error prevention tip 3 Shervin Daneshpajouh

  3. 1 // Fig. 1.2: fig01_02.cpp Single-line comments. 2 // A first program in C++. 3 #include <iostream> Preprocessor directive to Preprocessor directive to 4 include input/output stream 5 // function main begins program execution header file <iostream> . 6 int main() Left brace { begins g 7 7 { { function body. 8 std::cout << "Welcome to C++!\n"; 9 Statements end with a 10 return 0; // indicate that program ended successfully semicolon ; . 11 11 Stream insertion operator. 12 } // end function main Name cout belongs to Welcome to C++! namespace std . p Function main appears Corresponding right brace } ends function body. exactly once in every C++ program.. Keyword return is one of several means to exit function; value 0 indicates function; value 0 indicates program terminated successfully. 4

  4. Good Programming Practice Good Programming Practice Good Programming Practice Good Programming Practice • Every program should begin with a comment that describes y p g g the purpose of the program, author, date and time. • Use blank lines and space characters to enhance program readability. • Some programmers prefer to declare each variable on a separate line. This format allows for easy insertion of a p y descriptive comment next to each declaration. 5 Shervin Daneshpajouh

  5. Common Programming Error Common Programming Error Common Programming Error Common Programming Error • Omitting the semicolon at the end of a C++ statement is a g syntax error. • preprocessor directives do not end in a semicolon. • The syntax of a programming language specifies the rules for creating a proper program in that language. A syntax error occurs when the compiler encounters code that violates C++'s occurs when the compiler encounters code that violates C s language rules (i.e., its syntax). • Syntax errors are also called compiler errors , compile ‐ time errors or compilation errors . il i • You will be unable to execute your program until you correct all the syntax errors in it the syntax errors in it. • Some compilation errors are not syntax errors. 6 Shervin Daneshpajouh

  6. A A Simple Program: Simple Program: Printing a Line of Text Printing a Line of Text f Standard output stream object � std::cout � “Connected” to screen � << � Stream insertion operator � Value to right (right operand) inserted into output stream Namespace p � std:: specifies using name that belongs to “namespace” std � std:: removed through use of using statements g g Escape characters � \ � Indicates “special” character output � Indicates special character output 7

  7. A A Simple Program: Simple Program: Printing a Line of Text Printing a Line of Text f E Escape Sequence S Description D i ti \n Newline. Position the screen cursor to the beginning of the next line. \t Horizontal tab. Move the screen cursor to the next tab stop. \r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the beg g o e cu e e; do o dv ce o e next line. \a Alert. Sound the system bell. \\ Backslash. Used to print a backslash character. \" Double quote. Used to print a double quote character. 8

  8. 1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements. 3 #include <iostream> 4 Multiple stream insertion 5 // function main begins program execution statements produce one 6 int main() line of output. 7 7 { { 8 std::cout << "Welcome "; 9 std::cout << "to C++!\n"; 10 11 11 return 0; return 0; // indicate that program ended successfully // indicate that program ended successfully 12 13 } // end function main Welcome to C++! Welcome to C++! 9

  9. 1 // Fig. 1.5: fig01_05.cpp 2 // Printing multiple lines with a single statement 3 #include <iostream> 4 5 // function main begins program execution Using newline characters 6 int main() to print on multiple lines. 7 7 { { 8 std::cout << "Welcome\nto\n\nC++!\n"; 9 10 return 0; // indicate that program ended successfully 11 11 12 } // end function main Welcome to to C++! 10

  10. Another Another Simple Program: Simple Program: Adding Two Integers Adding Two Integers dd dd Variables � Location in memory where value can be stored � Common data types � int ‐ integer numbers g � char ‐ characters � double ‐ floating point numbers � Declare variables with name and data type before use yp int integer1; int integer2; int sum; � Can declare several variables of same type in one declaration � Comma ‐ separated list int integer1, integer2, sum; g , g , ; 11

  11. Another Another Simple Program: Simple Program: Adding Two Integers Adding Two Integers dd dd Variables � Variable names � Valid identifier � Series of characters (letters, digits, underscores) � Series of characters (letters digits underscores) � Cannot begin with digit � Case sensitive • Portability Tip P t bilit Ti • C++ allows identifiers of any length, but your C++ implementation may impose some restrictions on the length of identifiers. Use identifiers of 31 characters or fewer to ensure portability. 12

  12. Good Programming Practice Good Programming Practice • Choosing meaningful identifiers helps make a program self ‐ g g p p g documenting a person can understand the program simply by reading it rather than having to refer to manuals or comments. • Avoid using abbreviations in identifiers. This promotes program readability. readability. • Avoid identifiers that begin with underscores and double g underscores, because C++ compilers may use names like that for their own purposes internally. This will prevent names you choose from being confused with names the compiler s choose choose from being confused with names the compiler s choose . 13 Shervin Daneshpajouh

  13. Another Simple Program: Another Simple Program: Adding Two Integers Adding Two Integers dd dd Input stream object � >> (stream extraction operator) � Used with std::cin � Waits for user to input value, then press Enter (Return) key Waits for user to input value then press Enter (Return) key � Stores value in variable to right of operator � Converts value to variable data type = (assignment operator) � Assigns value to variable � Binary operator (two operands) Binary operator (two operands) � Example: sum = variable1 + variable2; 14

  14. 1 // Fig. 1.6: fig01_06.cpp 2 // Addition program. 3 #include <iostream> 4 5 // function main begins program execution Declare integer variables. 6 int main() 7 { Use stream extraction operator with Use stream extraction operator with 8 8 int integer1; // first number to be input by user int integer1; // first number to be input by user standard input stream to obtain user 9 int integer2; // second number to be input by user 10 int sum; // variable in which sum will be stored input. 11 12 12 std::cout << "Enter first integer\n"; // prompt std::cout << Enter first integer\n ; // prompt Stream manipulator S i l 13 std::cin >> integer1; // read an integer std::endl outputs a 14 newline, then “flushes 15 std::cout << "Enter second integer\n"; // prompt output buffer.” p 16 16 std::cin >> integer2; std::cin integer2; // read an integer // read an integer 17 18 sum = integer1 + integer2; // assign result to sum Concatenating, chaining or 19 cascading stream insertion 20 std::cout << "Sum is " << sum << std::endl; // print sum operations. operations 21 22 return 0; // indicate that program ended successfully 23 Calculations can be performed in output statements: 24 } // end function main alternative for lines 18 and 20: l i f li 18 d 20 std::cout << "Sum is " << integer1 + integer2 << std::endl; 15

  15. Enter first integer fig01_06.cpp fig01_06.cpp 45 output (1 of 1) output (1 of 1) Enter second integer 72 Sum is 117 16

  16. Memory Memory Concepts Memory Memory Concepts Concepts Concepts Variable names � Correspond to actual locations in computer's memory � Every variable has name, type, size and value � When new value placed into variable, overwrites previous value � Reading variables from memory nondestructive � Reading variables from memory nondestructive 17

  17. Memory Memory Concepts Memory Memory Concepts Concepts Concepts 45 integer1 std::cin >> integer1; g � Assume user entered 45 45 integer1 std::cin >> integer2; 72 integer2 � Assume user entered 72 45 integer1 72 integer2 g sum = integer1 + integer2; sum = integer1 + integer2; 117 sum 18

  18. Arithmetic Arithmetic Arithmetic Arithmetic Arithmetic calculations � * � Multiplication � / / � Division � Integer division truncates remainder g � 7 / 5 evaluates to 1 � % � Modulus operator returns remainder � Modulus operator returns remainder � 7 % 5 evaluates to 2 19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend