Constants in C++

Why don’t mathematicians like to change constants?

Because they’re always set in their ways!

A constant in C++ is a value that cannot be changed once it is set. It is like a label or a name for a specific value, and the value itself cannot be altered. For example, if you have a constant called “PI” and set it to 3.14, you cannot change the value of PI to any other number later on in your program. Constants are used to make code more readable and to prevent accidental changes to important values.

Here’s an example of how you would declare a constant in C++:

const double PI = 3.14;

In this example, we are creating a constant called PI, and giving it the value of 3.14. The “const” keyword tells the compiler that this value should be treated as a constant, and the “double” keyword specifies the data type (in this case, a decimal number) of the constant.

It’s important to note that once you have assigned a value to a constant variable, you cannot change the value again.

PI = 3.15 // this will give you an error

Also, you can use the const keyword in function argument as well, making it so that the argument passed to the function cannot be changed within the function.

void print(const int x) {
  x = x + 1; // this will give an error
  cout << x << endl;
}

Defining an integer constant:

const int DAYS_IN_WEEK = 7;

In this example, we are defining a constant called DAYS_IN_WEEK and setting its value to 7. We can then use this constant in our program to represent the number of days in a week, instead of using the literal value 7.

Defining a string constant:

const string COMPANY_NAME = "Foam Robotics";

This creates a constant called COMPANY_NAME, and assigns it the string “Acme Inc.”. This constant can be used in the program to represent the name of the company instead of using the string “Foam Robotics” throughout the code.

Using a constant in an equation:

const double PI = 3.14;
double area = PI * radius * radius;

In this example, we are using the constant PI to calculate the area of a circle. We are using the constant instead of the literal value 3.14, so if we ever need to change the value of PI, we can do it in one place and it will update throughout the program.

Using a constant in a function argument:

const int MAX_ATTEMPTS = 3;
void login(const string& username, const string& password, const int attempts = MAX_ATTEMPTS) {
    // code to handle login
}

In this example, we are using the constant MAX_ATTEMPTS as the default value for the “attempts” argument in the “login” function. If the caller of the function does not provide a value for “attempts”, it will automatically be set to MAX_ATTEMPTS.

Using a constant to define an array size:

const int SIZE = 10;
int myArray[SIZE];

In this example, we are using the constant SIZE to define the size of the “myArray” array. This allows us to easily change the size of the array by changing the value of the constant, and it makes the code more readable because the purpose of the constant is clear.

#define is another way to define constants in C++, it is called a “preprocessor directive” because it is processed by the preprocessor before the code is compiled. The preprocessor is a separate step in the compilation process that is responsible for handling things like macros and constants.

The syntax for using #define is slightly different from using const:

#define PI 3.14

This defines a constant called PI with the value 3.14, just like before. The difference is that with #define the constant is replaced by its value throughout the code, while with const the constant is stored as a variable with a fixed value that can be accessed like any other variable.

Here’s an example of how you would use the constant defined with #define in some code:

double area = PI * radius * radius;

In this example, the preprocessor will replace PI with 3.14 before the code is compiled, so the code that the compiler sees is equivalent to:

double area = 3.14 * radius * radius;

It’s worth noting that constants defined with #define are global, which means that they can be accessed from anywhere in the code, and there is no data type associated with them, so you can’t use #define to define constants of a certain data type. Also, #define does not distinguish between uppercase and lowercase letters, so PI and pi are the same.

Another important thing to note is that, constants defined with #define are not true constants, they are just text substitution, and they don’t have memory associated with them. unlike const which are actual variables and have memory associated with them.

In general, it is recommended to use const instead of #define when defining constants, because const is more flexible and less error-prone. However, #define is still commonly used in C++ because it is more efficient and it is also backward compatible with C.

In C++, an enum (short for “enumeration”) is a way to define a set of named integer constants. It allows you to define a new data type with a set of named values, which can make your code more readable and easier to understand.

Here’s an example of how you might use an enum to define a set of named constants for the days of the week:

enum class Weekdays {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

n this example, we’ve defined an enum called Weekdays that has 7 named constants: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday. Each of these constants is assigned an integer value starting from 0. So, Monday is assigned 0, Tuesday is assigned 1, and so on.

You can use the constants defined in an enum just like you would use any other variable, like this:

Weekdays today = Weekdays::Monday;
if (today == Weekdays::Monday) {
    // do something
}

Enums in C++11 and later allows you to specify the underlying type of the constants, such as:

enum class Weekdays : int {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

This declares that the enumeration is of type int.

You can also assign values to the constants, like this:

enum class Weekdays {
    Monday = 1,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

In C++, an enum (short for “enumeration”) is a way to define a set of named integer constants. It allows you to define a new data type with a set of named values, which can make your code more readable and easier to understand.

Here’s an example of how you might use an enum to define a set of named constants for the days of the week:

enum class Weekdays {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

In this example, we’ve defined an enum called Weekdays that has 7 named constants: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday. Each of these constants is assigned an integer value starting from 0. So, Monday is assigned 0, Tuesday is assigned 1, and so on.

You can use the constants defined in an enum just like you would use any other variable, like this:

Weekdays today = Weekdays::Monday;
if (today == Weekdays::Monday) {
    // do something
}

Enums in C++11 and later allows you to specify the underlying type of the constants, such as:

enum class Weekdays : int {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};
This declares that the enumeration is of type int.

You can also assign values to the constants, like this:

enum class Weekdays {
    Monday = 1,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};

This sets the value of Monday to 1, and the rest of the constants are assigned values incrementing by 1.

It’s important to note that, enum is not a keyword in C++, it’s a keyword in C. However, when you use enum class or enum struct it becomes a keyword in C++.

In general, enum is a good way to define a set of named constants that are related to each other. It makes your code more readable and less error-prone, because you can use the named constants instead of integer literals. It also allows you to give your constants meaningful names, making your code more self-documenting.

This is all for now!

Peace!