How to define constants in C++?

C++Object Oriented ProgrammingProgramming

You can define constants in C++ by adding the const qualifier before the declaration of the variable. 

Example

#include<iostream>
using namespace std;

int main() {
   const int x = 9;
   x = 0;
   return 0;
}

This will define the constant variable x. But it will throw an error as we are trying to rewrite the value of a constant.

raja
Published on 16-Feb-2018 07:07:43
Advertisements