Const keyword in C++
This section will discuss the const keyword in the C++ programming language. It is the const keywords used to define the constant value that cannot change during program execution. It means once we declare a variable as the constant in a program, the variable’s value will be fixed and never be changed. If we try to change the value of the const type variable, it shows an error message in the program.
Use const keywords with different parameters:
- Use const variable
- Use const with pointers
- Use const pointer with variables
- Use const with function arguments
- Use const with class member functions
- Use const with class data members
- Use const with class objects
1. Const variable
It is a const variable used to define the variable values that never be changed during the execution of a program. And if we try to modify the value, it throws an error.
Example: Program to use the const keyword in C++
Let’s create a program to demonstrate the use of the const keyword in the C++ programming language.
It shows the compile-time error because we update the assigned value of the num 25 by 10.
Example: Let’s create a simple program to demonstrate the use of the const keyword in the C++ programming language.
The sum of the x and y is: 42
In the above program, we declared and assigned the const variables x and y at initial. And then, store the result of two const variables to the ‘z’ variable to print the sum.
Note: While the declaration of the const variable in the C++ programming, we need to assign the value of the defined variables at the same time; else, it shows the compile-time error.
2. Constant pointer
To create a const pointer, we need to use the const keyword before the pointer’s name. We cannot change the address of the const pointer after its initialization, which means the pointer will always point to the same address once the pointer is initialized as the const pointer.
Example: Program to demonstrate the constant pointer using the const keyword
Let’s consider an example to use the const keyword with the constant pointer in the C++ programming language.
The value of x: 15 The value of ptr: 15
In the above program, pointer ptr pointing to the address of int variable ‘x’ and ptr variable cannot be changed their address once it initialized, but the pointer ptr can change the value of x.
3. Pointer to constant variable
It means the pointer points to the value of a const variable that cannot change.
Declaration of the pointer to the constant variable:
Here, x is a pointer that point to a const integer type variable, and we can also declare a pointer to the constant variable as,
In this case, y is a pointer to point a char type const variable.
Example: Program to use the const keyword with a pointer to constant variable
The initial value of ptr: 7 The value of x: 7 The value of y: 10 The value of ptr: 10
In the above program, pointer ptr points to the const int (x) variable, and the value of the int (x) variable can never change.
4. Constant function Arguments
We can declare the function arguments as the constant argument using the const keyword. And if the value of function arguments is declared const, it does not allow changing its value.
In the above syntax, return_type is represented whether the function will return a value or not. The fun_name() function contains a const argument whose value can never be changed once it defines in the program.
Example: Let’s consider an example to use the const keyword with function arguments in the C++ programming language.
The value of num: 5
In the above program, the num is a constant argument, and we cannot update the num value. If we update the value of the num variable, it returns the compile-time error.
5. Const Member function of class
- A const is a constant member function of a class that never changes any class data members, and it also does not call any non-const function.
- It is also known as the read-only function.
- We can create a constant member function of a class by adding the const keyword after the name of the member function.
In the above syntax, mem_fun() is a member function of a class, and the const keyword is used after the name of the member function to make it constant.
Example: Program to use the const keyword with the member function of class
Let’s consider an example to define the const keyword with the member function of the class.
The above code throws a compilation error because the fun() function is a const member function of class ABC, and we are trying to assign a value to its data member ‘x’ that returns an error.
6. Constant Data Members of class
Data members are like the variable that is declared inside a class, but once the data member is initialized, it never changes, not even in the constructor or destructor. The constant data member is initialized using the const keyword before the data type inside the class. The const data members cannot be assigned the values during its declaration; however, they can assign the constructor values.
Example: Program to use the const keyword with the Data members of the class
The value of y: 10 The value of constant data member 'A' is: 10
In the above program, the object obj of the ABC class invokes the ABC constructor to print the value of y, and then it prints the value of the const data member ‘A’ is 10. But when the ‘obj.A’ assigns a new value of ‘A’ data member, it shows a compile-time error because A’s value is constant and cannot be changed.
7. Constant Objects
When we create an object using the const keyword, the value of data members can never change till the life of the object in a program. The const objects are also known as the read-only objects.
The const is a keyword used before the class’ object’s name in the above syntax to make it a constant object.
Example: Let’s create a program to use the constant objects in the C++ programming language.
The value of A: 10
In the above program, we assign the value of A is 10, the compiler prints “The value of A: 10”, and when we assign the value of A to 20, the object of the class returns the compile time error in the program.
Program to find the area of a circle using the const keyword
Let’s consider an example to print the area of a circle using the const keyword in the C++ programming language.
Input the radius of a circle: 5 The area of a circle is: 78.5