CpSc 146 : Introduction to C++
Variables, types

Elements of a simple C++ program


Programming Style


Program Format


Variable declaration

  1. Variables must be declared before they can be used.
  2. Type followed by name of variable (identifier)
    Types are reserved words such as int
  3. Identifiers
    • Consist of letters, digits and _
    • Begin with letter or _ (not good idea)
    • Case sensitive (a1 and A1 are different)
    • Cannot be a keyword or reserved word
    • No limit to length. For compatibility use 31 characters
    • Use meaningful names
    • All caps indicates a constant
    • Beginning with _ indicates a system defined variable
  4. Declarations
    • Integer declaration
      int length, width;
      char shirt_size
    • Placement of variable declaration : almost anywhere
    • For now, place them immediately after {
    • Results in storage allocation from RAM

Types can be:


DEEP DIVE on types
Types are extremely important in non-scripting languages such as C++ and Java
Characters and Strings
Numbers
Assignment statement
Initializations

Named Constants