Notes

Variables in C++

Definition:

  • A variable is a representation of some value or number.
  • In programming, variables can store more than just numbers. They can store characters, strings, or even whole sentences.

Steps to Create and Use a Variable:

  1. Declaration: Specifies the data type and the variable name.
  2. Assignment: Assigns a value to the variable.

Declaration:

  • When declaring a variable, you need to specify its data type (e.g., int, double, char, bool, etc.) and give it a unique identifier (the variable name).

  • Example:

    int x; // Declares a variable 'x' that will store an integer.
     

Assignment:

  • To assign a value to a variable, use the assignment operator = followed by the value.

  • Example:

    x = 5; // Assigns the value 5 to variable x.
     
  • The declaration and assignment can be combined into one step:

    int x = 5; // Declares and assigns the value 5 to x.
     
  • Displaying the Value:

    • Use std::cout to display the value of a variable.

    • Example:

      std::cout << x; // Displays the value of x (which is 5).
       

Example of Working with Variables:

  • Declaration:

    int x; // Declare an integer variable x.
     
  • Assignment:

    x = 5; // Assign the value 5 to x.
     
  • Display:

    std::cout << x; // Output the value of x, which is 5.
     

Different Data Types in C++:

  1. int (Integer): Stores whole numbers (e.g., 5, -10).

    • Example:

      int age = 21;
      int year = 2023;
      int days = 7;
       
    • If you assign a decimal value to an int, the decimal part will be truncated (removed).

      • Example:

        int days = 7.5; // The value is truncated to 7.
         
  2. double (Floating-point number): Stores numbers that include a decimal portion.

    • Example:

      double price = 10.99;
      double gpa = 2.5;
      double temperature = 25.1;
       
    • The double type allows decimals, unlike int.

  3. char (Character): Stores a single character (e.g., ‘A’, ‘b’, ’$’).

    • Example:

      char grade = 'A';
      char initial = 'B';
      char currency = '$';
       
    • Important note: You can only store one character in a char variable. Trying to store more than one character will result in a warning.

      • Example (incorrect):

        char grade = "AB"; // Warning: Overflow in conversion from string to char.
         
  4. bool (Boolean): Stores a value that is either true or false.

    • Example:

      bool student = true; // The student is enrolled.
      bool power = false;  // The power is off.
      bool for_sale = true; // The item is for sale.
       
    • A boolean is useful for situations that have only two states, like “on” or “off,” “yes” or “no.”

  5. string (Sequence of characters): Represents a sequence of characters or text.

    • Example:

      std::string name = "John Doe";
      std::string day = "Friday";
      std::string food = "Pizza";
      std::string address = "123 Fake Street";
       
    • Important note: A string can hold more than one character, even entire sentences or names.

Displaying Variables with Text:

  • You can combine a variable with a string of text using std::cout to display both together.
    • Example:

      std::cout << "Hello, " << name; // Displays "Hello, John Doe"
       
  • Multiple pieces of text can be concatenated and displayed in one statement:
    • Example:

      std::cout << "Hello " << name << ", you are " << age << " years old.";
      // Displays: "Hello John Doe, you are 21 years old."
       

Key Notes on Variables:

  • int stores whole numbers.

  • double stores numbers with decimals.

  • char stores single characters.

  • bool stores true or false values.

  • string stores a sequence of characters (text).

    You can store numbers in strings, but they will be treated as text. For example:

    std::string age = "21"; // This is treated as text, not a number.
     

Assignment for Practice:

  • In the comment section, you are encouraged to post examples of:
    1. An integer variable
    2. A double variable
    3. A character variable
    4. A boolean variable
    5. A string variable

Conclusion:

This lesson introduced variables and their types in C++ programming. We covered how to declare and assign values to variables and how to display them. The key takeaway is understanding how to use different data types (like int, double, char, bool, and string) to store and manipulate different kinds of data in a C++ program.


References