Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c
constant

What are constant variables and how they are used in C++?

Behzad Ahmad

In programming, we use variables extensively as a place to store data.

A variable is a scalar memory location where you can store data values. At declaring or initializing a variable, you have to give that variable a unique name so that you can access that variable.

You can also update or alter the value of a variable anywhere in your program according to your needs. However, sometimes, you don’t want your variable to be updated. In this case, the concept of constant variables is introduced.

A constant variable is one whose value cannot be updated or altered anywhere in your program. A constant variable must be initialized at its declaration.

To declare a constant variable in C++, the keyword const is written before the variable’s data type. Constant variables can be declared for any data types, such as int, double, char, or string.

Take a look at this constant variable declaration and initialization:

const int sum = 100;

Now, the integer variable above contains only the assigned value and a new value cannot be assigned to it.

However, initializing constant variables in a class during object-oriented programming is a bit different.

Example

Consider an Employee class, which contains a private constant variable named empID, which stores the unique ID of the employee and cannot be changed thereafter. As such, it can be declared as a constant variable.

class Employee{
      private:
           const int empID;
};

Now, we know that a constant must be initialized right at its declaration; therefore, this constant variable must be initialized at the same time as an object of this class.

We can take a value from the user as a parameter in the constructor and then store that value in the constant variable.

However, we cannot assign any value to this constant variable in the body of the constructor, as a constant variable is not a modifiable value. The only option is to use the initializer list to initialize the constant variable. Below is the demonstration of this:

class Employee{
      private:
           const int empID;
      public:
           Employee(int id): empID(id){

      }
      ~Employee(){
      }
};

Now, through the initializer list, the value which the parameter id holds is assigned to our constant variable empID.

Code

The following examples show how to use constant variables and what happens when we try to change their values.

#include <iostream>
using namespace std;

int main() {
  const double pi = 3.14;

  cout << "Area of Circle with Radius 4 = " << pi*4*4;
  
  return 0;
}
Example 1: Normal use
#include <iostream>
using namespace std;

int main() {
  const string name = "Rebecca";
  
  name = "Raven";
  cout << "Name is: " << name;
  return 0;
}
Example 2: Modification to a constant variable producing an error

RELATED TAGS

c
constant

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring