Examples of the ‘const’ Statement in C++

In the examples below, we show how useful the feature of using a C++ constant can be. When you use const in a program in C++, for example a global constant, the value in it cannot be changed while the program is running. It can only be changed manually by you the programmer.

So if you want a value to stay unchanged throughout the execution of a program, use the “const” statement in front of a declaration of a variable like the example below:


const int speed_of_light = 186,000; // miles per second

 

Consider the following C++ example:


#include <iostream>

using namespace std;

const int speed_of_light = 186000; // miles per second

int main()

{

	cout << speed_of_light;

	getchar(); // Just To Prevent Screen from Disappearing

	cin.get(); // Just To Prevent Screen from Disappearing

	return 0;

}

Output:

Everything looks normal.

 

Now consider changing the value of “speed_of_light” to 186.


int main()

{

	speed_of_light = 186;

	cout << speed_of_light;

	getchar(); // Just To Prevent Screen from Disappearing

	cin.get(); // Just To Prevent Screen from Disappearing

return 0;

}

This is not allowed because of the “const” attribute of “speed_of_light”. So the compiler will not run this program and gives you a compiler error, like below:

So the reason for using the “const” modifier is to make it hard for you as the programmer to mistakenly add code that tries to change the value of that protected variable.

 

The const Pointer in C++ using Functions

Now let’s take a look at functions that use “Call by Reference”, where the reference of variables are passed to the function. You might want that function to NOT modify the actual variable. Use “const” in front of the variable you do not want to be modified. Take a look at the example below using const pointers (Note the ‘ * ‘ in front of the pointer argument b, and the ‘&’ in front of the num2 variable):


void CallFunction(const int * b)

{

	if (*b==20)

	{

		cout << "the value of b is: " << *b;

	}

}

int main()

{

	int num2 = 20;

	CallFunction(& num2);

	return 0;

}

Output:

You see that you can do comparisons on the variable ‘b’ in the function. But you cannot change the value of ‘b‘ because of the “const” modifier.

Now let’s see what happens if the value of ‘b‘ is changed in the program below:


void CallFunction(const int * b)

{

	*b = *b + 20; // NOT allowed because b is constant and cannot be modified.

	if (*b==40)

	{

		cout << "the value of b is: " << *b;

	}

}

So in this function we tried to do some work on the pointer variable ‘b‘ but this code will not be compiled and will give a compile time error like you see below.

So the bottom line with const in C++ function variables is that you can work the variable in your code, but NOT change the value. Its that simple.

 

You might want to continue and read…

C++ Part 8 – Functions

C++ Part 7 – Pointers

C++ Part 6 – Arrays & Strings

C++ Part 5 – “switch and loops”

C++ Part 4 – else if Statement in C++

C++ Part 3 – Conditional “if” Statement

C++ Part 2 – The Basics of C++

C++ Part 1 – Installing Visual C++ Express

 

More C / C++ and Dev Programming Tutorials:

Share this tutorial with your circle of friends:

Leave a Reply

Your email address will not be published. Required fields are marked *