Language: EN

cpp-que-son-constantes

What are constants and how to use them in C++

A constant in C++ is an identifier that we use to represent a value that cannot be modified once set.

Constants are used to avoid the repetition of literal values in the code and improve the clarity and maintainability of the code.

Moreover, using constants makes it easier to update and maintain the code (since if you need to change the value of a constant, you only need to do it in one place).

Syntax of constants

The basic syntax for defining a constant in C++ is as follows:

const type name = value;
  • type: Specifies the data type of the constant.
  • name: Is the unique name given to the constant.
  • value: Is the constant value assigned to the constant.

For example, let’s see how we can define a constant with the value of PI.

const double PI = 3.14159;

std::cout << PI << std::endl; // Prints: 3.14159

PI = 3.5; // ❌ This will give an error, you cannot reassign a constant

Using constants

Accessing constants

Constants are accessed directly using their name:

std::cout << PI << std::endl;

Using in Expressions

Constants can be used in expressions instead of literal values to improve readability:

double area = PI * radius * radius;

Naming convention

It is not mandatory, but it is relatively common to use uppercase names for constants to distinguish them from other variables.

const double PI = 3.14159;

Constants in classes

Constants can also be defined within classes. When defined this way, they are often prefixed with the class name to access them.

class Mathematics {
public:
    static const double PI;
};

// Definition of the constant outside the class
const double Mathematics::PI = 3.14159;

int main() {
    std::cout << Mathematics::PI << std::endl; // Prints: 3.14159
    return 0;
}

constexpr variables

constexpr variables are similar to constants, but their value must be known at compile time. These are especially useful for defining values that will be used in constant expressions.

constexpr double PI = 3.14159;

double calculateArea(double radius) {
    return PI * radius * radius;
}

const variables in Classes

const variables can also be members of a class and must be initialized in the class constructor if not initialized in the declaration.

class Example {
public:
    const int NUMBER;

    Example(int value) : NUMBER(value) {}
};

int main() {
    Example example(42);
    std::cout << example.NUMBER << std::endl; // Prints: 42
    return 0;
}

Practical examples

Definition of mathematical constants

class MathematicalConstants {
public:
    static const double PI;
    static const double EULER;
};

const double MathematicalConstants::PI = 3.14159;
const double MathematicalConstants::EULER = 2.71828;

int main() {
    std::cout << "Pi: " << MathematicalConstants::PI << std::endl;
    std::cout << "Euler: " << MathematicalConstants::EULER << std::endl;
    return 0;
}

Definition of configuration constants

class Configuration {
public:
    static const int LOGIN_ATTEMPTS_LIMIT;
    static const char* DATE_FORMAT;
};

const int Configuration::LOGIN_ATTEMPTS_LIMIT = 3;
const char* Configuration::DATE_FORMAT = "dd/MM/yyyy";

int main() {
    std::cout << "Login attempts limit: " << Configuration::LOGIN_ATTEMPTS_LIMIT << std::endl;
    std::cout << "Date format: " << Configuration::DATE_FORMAT << std::endl;
    return 0;
}