CIS 150 Variables

Objectives

  • Use variables and data types in C++
  • Define and use constants in C++
  • Recognize the more common C++ reserved keywords
  • Choose a reasonable data type for a specific variable
  • Choose a valid meaningful name for a variable
  • Convert between numeric data types using type casting

Variables

  • Variables are memory locations where values are stored
  • The naming rules for variables are:
    • must start with letter or '_' (underscore)
    • can only contain letters, digits, and '_' (underscore)
    • case-sensitive
    • by convention, variable names are all lowercase. If the name is multi-word, then words after the first start with an uppercase letter. This is called camel-casing (eg. hourlyRate)
    • may not be a C++ reserved word
    • special rules apply to identifiers that start with an underscore or contain a double underscore
  • Declaring a variable requires a data type and a name: int num;
  • Initializing while declaring: int num = 7;
  • Letting the compiler infer the data type: auto num = 7;

Data types

  • C++ has many data types available, but we will use a select few.
  • int: used for integer values, such as 1, -5, 1786, 0
  • long: same as int but with more range
  • float: used for floating- point values, such as: 3.14159, 42, 0, -4.3
  • double: same as float but with more precision and range
  • char: individual characters, such as 'a', '3', '?'
  • Note: '3' (a char) is not the same as 3 (an int)
  • bool: stores Boolean values (True, False)
  • string: Used for storing multi-character sequences such as names. This is not a primitive (built-in) data type. You have to include the string library and specifiy that you are using std::string (or namespace std)

Constants

  • The old way of using constants was to #define them before main(): #define PI 3.1415926
  • With the old #define, every place PI was used in the source code it would be replaced with 3.1415926, but the programmer was not restricted to just numbers for replacement characters. This is discouraged now.
  • Modern way to define a constant: const double PI = 3.1415926;
  • The modern way declares the constant along with other variables.
  • Constants are just variables whose value can not be changed after initialization.
  • By convention, constant names are all uppercase. Underscores are used to separate words in multi-word names.

Keywords in C++ (reserved)

Converting between data types

  • C++ will automatically convert numbers in mixed expressions to the wider data type when encountered. For example, in 3 + 4.7, the 3 will be converted to a double beofre the addition is done.
  • The programmer can also specify a conversion. Conversion to an int involves truncation rather than rounding.
  • For the following examples, assume: double n = 8.9;
  • C-like type cast: (int) n // evaluates to 8
  • functional notation type cast: int(n) // evaluates to 8
  • C++ type cast: static_cast<int>(n) // evaluates to 8
  • You can cast to other data types than the examples above.