This part will cover the const keyword in C++ programming. The const keyword is utilized to specify a value that remains constant throughout program execution. This implies that after declaring a variable as const, its value remains unchanged. Any attempt to modify the value of a const variable will result in an error message being displayed in the program.
Use const keywords with different parameters:
- Use const variable
- Use const with pointers
- Use conscpp tutorialer 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 constant variable utilized to assign values that remain unchanged throughout the program's execution. Any attempt to alter this value will result in an error being thrown.
Syntax
const data_type variable_name;
Example: Program to use the const keyword in C++
Let's develop a program illustrating the utilization of the const keyword in the C++ programming language.
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
// declare the value of the const
const int num = 25;
num = num + 10;
return 0;
}
Output
It displays a compile-time error as we attempt to modify the assigned value of the variable "num" from 25 to 10.
Let's develop a basic program to showcase the utilization of the const keyword in the C++ programming language.
/* create a program to use the const keyword with different data types in the C++. */
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
// declare variables
const int x = 20;
const int y = 25;
int z;
// add the value of x and y
z = x + y;
cout << " The sum of the x and y is: " << z << endl;
return 0;
}
Output
The sum of the x and y is: 42
In the preceding code snippet, we initialized and assigned constant variables x and y. Subsequently, we stored the sum of these two constant variables in the variable 'z' in order to display the result.
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. Constancpp tutorialer
To generate a conscpp tutorialer, the const keyword must precede the pointer's identifier. Modifying the conscpp tutorialer's address post-initialization is prohibited, ensuring the pointer remains fixed on the same address throughout its lifecycle as the conscpp tutorialer.
Example: Illustration of implementing const correctness in a C++ tutorial by utilizing the const keyword
Let's explore an illustration demonstrating the utilization of the const keyword with the constancpp tutorialer in the C++ programming language.
#include <iostream>
using namespace std;
int main ()
{
// declaration of the integer variables
int x = 10, y = 20;
// use const keyword to make constancpp tutorialer
int* const ptr = &x; // const integer ptr variable point address to the variable x
// ptr = &y; // now ptr cannot changed their address
*ptr = 15; // ptr can only change the value
cout << " The value of x: " << x << endl;
cout << " The value of ptr: " << *ptr << endl;
return 0;
}
Output
The value of x: 15
The value of ptr: 15
In the aforementioned code snippet, the pointer named ptr is directed towards the memory location of the integer variable 'x'. After initialization, the address stored in the ptr variable remains fixed and cannot be altered. However, the content of variable x can be modified by dereferencing the pointer ptr.
3. Pointer to constant variable
It signifies that the pointer is directed towards the value of a constant variable that remains unalterable.
Declaration of the pointer to the constant variable: ```
const datatype variablename;
const int* x;
Here, x is a pointer that points to a variable of integer type that is constant, and we can also define a pointer to the constant variable as,
char const* y;
In this scenario, y is a pointer that references a constant variable of type char.
Example: Demonstration of utilizing the const keyword with a pointer to a constant variable
include <iostream>
using namespace std;
int main
{
// declare integer variable
int x = 7, y = 10;
const int *ptr = &x; // here x become constant variable
cout << " \n The initial value of ptr:" << *ptr;
cout << " \n The value of x: " <<x;
// *ptr = 15; It is invalid; we cannot directly assign a value to the ptr variable
ptr = &y; // here ptr variable pointing to the non const address 'y'
cout << " \n The value of y: " <<y;
cout << " \n The value of ptr:" << *ptr;
return 0;
}
Output
The initial value of ptr: 7
The value of x: 7
The value of y: 10
The value of ptr: 10
In the provided code snippet, the pointer named ptr is referencing the constant integer (x) variable, ensuring that the value of the integer (x) variable remains immutable.
### 4. Constant function Arguments
We have the option to define function parameters as constant arguments by employing the const keyword. When function arguments are specified as const, they are immutable and cannot be altered.
Syntax
returntype funname (const int x)
{
}
In the provided syntax, the return_type signifies whether the function will produce an output or not. The fun_name() function includes a const parameter that retains a constant value throughout the program's execution.
Example: Let's explore a scenario where we utilize the const keyword with function parameters in the C++ programming language.
include <iostream>
using namespace std;
// create an integer Test function contains an argument num
int Test (const int num)
{
// if we change the value of the const argument, it thwrows an error.
// num = num + 10;
cout << " The value of num: " << num << endl;
return 0;
}
int main
{
// call function
Test(5);
}
Output
The value of num: 5
In the program mentioned earlier, the variable "num" is a constant parameter, preventing any modifications to its value. Any attempt to alter the value of the "num" variable will result in a 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.
Syntax
returntype memfun const
{
}
In the given syntax, mem_fun() represents a member function belonging to a class, with the const keyword placed after the function name to designate it as constant.
Example: Implementation of the const keyword in a class's member function
Let's explore an illustration to explain the const keyword within a class's member function.
class ABC
{
// define the access specifier
public:
// declare int variables
int A;
// declare member function as constant using const keyword
void fun const
{
A = 0; // it shows compile time error
}
};
int main
{
ABC obj;
obj.fun;
return 0;
}
Output
The code mentioned above results in a compilation error due to the fact that the fun() method is a const member function within the ABC class, and an attempt is being made to modify the data member 'x', leading to an error.
### 6. Constant Data Members of class
Data members are similar to variables declared within a class, but once initialized, they remain unchanged, even within the constructor or destructor. Constant data members are initialized by using the const keyword before the data type within the class. These const data members cannot be assigned values during declaration; however, they can be assigned values in the constructor.
Example: Implementing the const keyword with the class's data members
/ create a program to demonstrate the data member using the const keyword in C++. /
include <iostream>
using namespace std;
// create a class ABC
class ABC
{
public:
// use const keyword to declare const data member
const int A;
// create class constructor
ABC ( int y) : A(y)
{
cout << " The value of y: " << y << endl;
}
};
int main
{
ABC obj( 10); // here 'obj' is the object of class ABC
cout << " The value of constant data member 'A' is: " << obj.A << endl;
// obj.A = 5; // it shows an error.
// cout << obj.A << endl;
return 0;
}
Output
The value of y: 10
The value of constant data member 'A' is: 10
In the preceding code, the instance obj belonging to the ABC class triggers the ABC constructor to display the y value, followed by the output of the constant data member 'A' as 10. However, attempting to assign a different value to the 'A' data member using 'obj.A' results in a compile-time error due to the immutability of A's value.
### 7. Constant Objects
When an object is instantiated with the const keyword, the data members retain a fixed value throughout the object's lifespan within the program. These const objects are commonly referred to as read-only objects.
Syntax
const classname objname;
The keyword const is utilized before the name of the object in the class to designate it as a constant object.
Example: We will now develop a program to implement constant objects in the C++ programming language.
include <iostream>
using namespace std;
class ABC
{
public:
// define data member
int A;
// create constructor of the class ABC
ABC
{
A = 10; // define a value to A
}
};
int main
{
// declare a constant object
const ABC obj;
cout << " The value of A: " << obj.A << endl;
// obj.A = 20; // It returns a compile time error
return 0;
}
Output
The value of A: 10
In the given code snippet, we initialize the variable A with a value of 10. Subsequently, the compiler displays the message "The value of A: 10". However, upon changing the value of A to 20, an error occurs during compilation within the class object.
### Program to find the area of a circle using the const keyword
Let's explore a demonstration to display the area of a circle using the const keyword in the C++ programming language.
include <iostream>
using namespace std;
int main
{
// declare variables
int r;
const double PI = 3.14;
double result;
cout << " Input the radius of a circle: " << endl;
cin >> r;
result = PI r r;
cout << " The area of a circle is: " << result;
}
Output
Input the radius of a circle:
5
The area of a circle is: 78.5