course content

Course Content

Introduction to C++

Const and Enum (Bonus)Const and Enum (Bonus)

To declare a constant, you must specify the const keyword before the data type of the variable, for example:

If you try to change the value of a constant, the compiler will not allow this to happen and will display the message: "You can't assign to a variable that is const".

This example throws an error. This is done on purpose.

cpp

main.cpp

Declaring constants allows you to guarantee that the data in variables is uncharted.

Sometimes, a variable must take on some specific value. For this, enumerations are used. Enumerations allow you to limit the value of a variable to a certain set of allowed values, for example:

cpp

main.cpp

When trying to initialize a new variable of the StoreSpace type with some unknown value, the compiler will generate an error:

This example throws an error. This is done on purpose.

cpp

main.cpp

But what happens if we declare variables but do not initialize them?

cpp

main.cpp

It is very similar to indexing the array's elements.

cpp

main.cpp

If the value of the first element is not specified, then the value will be 0, the value of the second element (if its value is not specified) will be 1, and so on;
But what if the value of the element in the center of the list is given?

cpp

main.cpp

Section 2.

Chapter 7