Constants
As we have seen by now, we can create different kinds of variables with different kinds of values. We can also add new values to the already defined variables by simply using the operator “=”:
int a = 3; // "a" is equal to 3
a = 4; // This is OK, "a" gets assigned with a new value 4
These kinds of variables are called non-constant variables. If there are non-constant, there should also exist constant variables? Correct! A constant variable is simply a variable which must be defined (when declaring a constant you must also initialize it) and we cannot change its value in its entire lifespan in the code!
int a = 5; // non-constant integer with value 5
const double pi = 3.1415926535; // constant double
a = 7; // OK!
pi = 3.2; // Error! You cannot change the value of a const
const double e; //Error! const variable must be initialized
Table of contents
- Loops, Pointers and Functions
- Classes and Objects
- Operator Overloading
- Class Inheritance
- Exceptions
- Namespaces
- Templates
- Standard Library
- Additional Problems