Variables

What is a Variable?

A variable is a typed storage location filled with a value and associated with an identifier. You can think about this like a name given to a memory location.

C++ Data Types

  • int - an integer
  • bool - true or false
  • char - a character
  • float - a decimal value
  • double - like float, but double the size
  • void - represents the lack of a data type

Initializing/Creating a Variable

When we create a variable, we first declare, or define/create a new variable, and initialize, or assign it (set it equal to) some value.

For example, <data_type> variable = value; and std::string name = “Tuffy”;. To describe this C++ statement in words, it declares a new variable called name of type std::string, and initializes it to the value “Tuffy”.

C++ Variables Cheat Sheet

  • “Variable” - a named for a container that stores data of a specific type.
  • “Data type” - the type of data stored inside a variable. ex. int, bool, string, vector, map, classes
  • “Initialize” - create a new variable and set it equal to some value.
  • “Assign” - set a variable equal to some value