Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Summary

Section 2.2 First Program in C++: Printing a Line of Text

  • Single-line comments (p. 39) begin with //. You insert comments to document your programs and improve their readability.
  • Comments do not cause the computer to perform any action (p. 40) when the program is run—they're ignored by the compiler and do not cause any machine-language object code to be generated.
  • A preprocessor directive (p. 39) begins with # and is a message to the C++ preprocessor. Preprocessor directives are processed before the program is compiled and don't end with a semicolon.
  • The line #include <iostream> (p. 39) tells the C++ preprocessor to include the contents of the input/output stream header, which contains information necessary to compile programs that use std::cin (p. 43) and std::cout (p. 40) and the stream insertion (<<, p. 40) and stream extraction (>>, p. 43) operators.
  • White space (i.e., blank lines, space characters and tab characters, p. 39) makes programs easier to read. White-space characters outside of literals are ignored by the compiler.
  • C++ programs begin executing at main (p. 39), even if main does not appear first in the program.
  • The keyword int to the left of main indicates that main "returns" an integer value.
  • The body (p. 40) of every function must be contained in braces ({ and }).
  • A string (p. 40) in double quotes is sometimes referred to as a character string, message or string literal. White-space characters in strings are not ignored by the compiler.
  • Every statement (p. 40) must end with a semicolon (also known as the statement terminator).
  • Output and input in C++ are accomplished with streams (p. 40) of characters.
  • The output stream object std::cout—normally connected to the screen—is used to output data. Multiple data items can be output by concatenating stream insertion (<<) operators.
  • The input stream object std::cin—normally connected to the keyboard—is used to input data. Multiple data items can be input by concatenating stream extraction (>>) operators.
  • The notation std::cout specifies that we are using cout from "namespace" std.
  • When a backslash (i.e., an escape character) is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence (p. 41).
  • The newline escape sequence \n (p. 41) moves the cursor to the beginning of the next line on the screen.
  • A message that directs the user to take a specific action is known as a prompt (p. 45).
  • C++ keyword return (p. 41) is one of several means to exit a function.

Section 2.4 Another C++ Program: Adding Integers

  • All variables (p. 43) in a C++ program must be declared before they can be used.
  • A variable name is any valid identifier (p. 44) that is not a keyword. An identifier is a series of characters consisting of letters, digits and underscores ( _ ). Identifiers cannot start with a digit. Identifiers can be any length, but some systems or C++ implementations may impose length restrictions.
  • C++ is case sensitive (p. 44).
  • Most calculations are performed in assignment statements (p. 46).
  • A variable is a location in memory (p. 47) where a value can be stored for use by a program.
  • Variables of type int (p. 44) hold integer values, i.e., whole numbers such as 7, –11, 0, 31914.

Section 2.5 Memory Concepts

  • Every variable stored in the computer's memory has a name, a value, a type and a size.
  • Whenever a new value is placed in a memory location, the process is destructive (p. 47); i.e., the new value replaces the previous value in that location. The previous value is lost.
  • When a value is read from memory, the process is nondestructive (p. 47); i.e., a copy of the value is read, leaving the original value undisturbed in the memory location.
  • The std::endl stream manipulator (p. 46) outputs a newline, then "flushes the output buffer."

Section 2.6 Arithmetic

  • C++ evaluates arithmetic expressions (p. 48) in a precise sequence determined by the rules of operator precedence (p. 48) and associativity (p. 49).
  • Parentheses may be used to group expressions.
  • Integer division (p. 48) yields an integer quotient. Any fractional part in integer division is truncated.
  • The modulus operator, % (p. 48), yields the remainder after integer division.

Section 2.7 Decision Making: Equality and Relational Operators

  • The if statement (p. 51) allows a program to take alternative action based on whether a condition is met. The format for an if statement is

    if ( condition )
       statement;
    

    If the condition is true, the statement in the body of the if is executed. If the condition is not met, i.e., the condition is false, the body statement is skipped.

  • Conditions in if statements are commonly formed by using equality and relational operators (p. 51). The result of using these operators is always the value true or false.
  • The using directive (p. 53)

             using std::cout;
    

    informs the compiler where to find cout (namespace std) and eliminates the need to repeat the std:: prefix. The directive

             using namespace std;
    

    enables the program to use all the names in any included C++ standard library header.

  • + Share This
  • 🔖 Save To Your Account