In this video, we'll learn what constants are in c++.
Constants are very much like c++ variables.
They have names.
The rules for naming is the same, they occupy storage,
and they're usually typed.
This means that when you declare a constant,
you usually declare their type just like you do with a variable.
There is one type of constant that is not typed
and I'll talk about that at the end of the video.
The big difference between variables and constants is that unlike variables
the value of c++ constants cannot change once they're declared.
For example, if I declare age to be a constant integer and assign 21 to it,
then the value of age will always remain 21,
and I won't be able to change it.
If I try to change it I'll get a compiler error.
Why would you need constants in a program?
Let's consider a simple application that uses the number of months
in a year throughout the code.
We could certainly use the number 12 everywhere we need it.
However, 12 could mean a lot of things.
And if the program is of significant size, it might not be clear to other programmers
that 12 means the number of months in a year.
So we could create a variable and call it months in a year and assign 12 to it
and now use that name in our code whenever we need to use 12 in that context.
That's a much better solution than hard coding 12. But that's a variable,
which means that it can be changed, even accidentally.
A better solution is to declare months in a year to be a constant integer
and assign 12 to it.
Now we can use a meaningful name in our code that means 12,
and we can't change it even by accident.
C++ gives us several ways to create constants.
We can use literals, declared constants, constant expressions,
enumerated constants and finally, we can use defined constants.
We'll learn about constant expressions and enumerated constants later in this course.
Let's see what literal constants are first.
Literals are the most obvious kind of constant.
We can use literals throughout our code and we often do.
They're used to express specific values. We've already seen examples of literals.
In this slide, you can see that 12, 1.56,
Frank and the character J are examples of literal constants.
For example, the floating point literal constant 1.56
always expresses the value 1.56.
Literal constants also have types based on a naming convention.
You can be explicit with the types of literal constants. For example,
for integer literal constants,
you can add certain suffixes to the integer literal
to specify a different type.
For example, the letters u and l specify unsigned and long respectively.
These can be either lowercase or uppercase.
This slide shows just a few of the combinations.
For floating point numbers, you can use the l and f suffixes as shown in this slide.
We can also have character literal constants in c++.
We haven't used these yet. But they're commonly used within string literals.
They consist of the backslash character followed by another character,
and they're called escape codes. You can see some examples in this slide.
The cout statement will display "Hello" then tab over and display "there".
Then it will go to the beginning of the next line and display "my friend"
and then it will go to the beginning of the next line again.
It's very common to see escape codes embedded in string literals and output statements.
By far, the most common way of declaring constants in c++
is to use declared constants using the "const" keyword.
The syntax is exactly like that of a variable declaration
with the const keyword at the beginning.
You must initialize the declared constant
or the compiler will give you an uninitialized const error message.
What happens if you try to change the value of a constant? You guessed it, you can't.
The compiler will give you an error letting you know that the constant is read-only,
and you can't change it.
The last way of declaring constants in c++
is by using the preprocessor directive pound define. Let's take a look at that.
Defined constants were very commonly used in older c and c++ code.
These constants are defined using the pound defined preprocessor directive.
I'm showing you defined constants because you may run into them since there is so much
c++ legacy code out there.
Here you're telling the preprocessor that wherever it sees the word pi
replace it with 3.1415926.
Think of this as a blind find replace as you might do in a word processor.
The preprocessor will gladly substitute one for the other.
Since the preprocessor doesn't know c++,
it can't type check and this could lead to difficult to find errors.
So please don't use defined constants in modern c++ code.
In the next video, we'll head over to the CodeLite IDE
and see how we can declare and use constants in c++.
First, we'll write a program using literal constants,
then we'll refactor the code to use declared constants.