Chapter Review
A constant is a value that may not be changed during the program’s execution. C++ supports two types of constants: named constants, and literals.
A named constant is a constant value that is associated with an identifier. A Literal constant is a constant value not associated with an identifier.
A variable whose value can not be changed is called a constant variable. The const keyword can be used to make a variable constant. Constant variables must be initialized. Avoid using const
when passing by value or returning by value.
A type qualifier is a keyword that is applied to a type that modifies how that type behaves. As of C++23, C++ only supports const
and volatile
as type qualifiers.
A constant expression is an expression that can be evaluated at compile-time. An expression that is not a constant expression is sometimes called a runtime expression.
A compile-time constant is a constant whose value is known at compile-time. A runtime constant is a constant whose initialization value isn’t known until runtime.
A constexpr variable must be a compile-time constant, and initialized with a constant expression. Function parameters cannot be constexpr.
Literals are values inserted directly into the code. Literals have types, and literal suffixes can be used to change the type of a literal from the default type.
A magic number is a literal (usually a number) that either has an unclear meaning or may need to be changed later. Don’t use magic numbers in your code. Instead, use symbolic constants.
In everyday life, we count using decimal numbers, which have 10 digits. Computers use binary, which only has 2 digits. C++ also supports octal (base 8) and hexadecimal (base 16). These are all examples of numeral systems, which are collections of symbols (digits) used to represent numbers.
A string is a collection of sequential characters that is used to represent text (such as names, words, and sentences). String literals are always placed between double quotes. String literals in C++ are C-style strings, which have a strange type that is hard to work with.
std::string offers an easy and safe way to deal with text strings. std::string lives in the <string> header. std::string
is expensive to initialize (or assign to) and copy.
std::string_view provides read-only access to an existing string (a C-style string literal, a std::string, or a char array) without making a copy. A std::string_view
that is viewing a string that has been destroyed is sometimes called a dangling view. When a std::string
is modified, all views into that std::string
are invalidated, meaning those views are now invalid. Using an invalidated view (other than to revalidate it) will produce undefined behavior.
Because C-style string literals exist for the entire program, it is okay to set a std::string_view
to a C-style string literal, and even return such a std::string_view
from a function.
A substring is a contiguous sequence of characters within an existing string.
Quiz time
Question #1
Why are named constants often a better choice than literal constants?
Why are const/constexpr variables usually a better choice than #defined symbolic constants?
Question #2
Find 3 issues in the following code:
#include <cstdint> // for std::uint8_t
#include <iostream>
int main()
{
std::cout << "How old are you?\n";
std::uint8_t age{};
std::cin >> age;
std::cout << "Allowed to drive a car in Texas: ";
if (age >= 16)
std::cout << "Yes";
else
std::cout << "No";
std::cout << '.\n';
return 0;
}
Sample desired output:
How old are you? 6 Allowed to drive a car in Texas: No
How old are you? 19 Allowed to drive a car in Texas: Yes
Question #3
What are the primary differences between std::string
and std::string_view
?
What can go wrong when using a std::string_view
?
Question #4
Write a program that asks for the name and age of two people, then prints which person is older.
Here is the sample output from one run of the program:
Enter the name of person #1: John Bacon Enter the age of John Bacon: 37 Enter the name of person #2: David Jenkins Enter the age of David Jenkins: 44 David Jenkins (age 44) is older than John Bacon (age 37).
Question #5
In the solution to the above quiz, why can’t variable age1
in main
be constexpr?