The data types we used in the previous lessons for our variables were directly
recognized by the C++ compiler. For this reason they are referred to as built-in
types. Those data types are convenient in most scenarios. Sometimes you will
need to expand on these types. The C++ language provides you great flexibility
on re-defining these existing types or re-defining new ones.
In the previous lessons, we used some of C++ keywords to identify the data types.
Examples were int, short, double, unsigned int, or char.
The C++ language allows you to create
a new name for a data type without changing the meaning of that data type. This
allows you to name a data type with a name that is easier for you. This
technique doesn't change anything about the data type, it only gives it a new
name you can use in your program.
To re-define the name of an existing data type, you use the
typedef keyword. The formula to follow is:
typedef KnownDataType NewName;
The typedef keyword is required to let the
compiler know that you are redefining an existing data type. The
DataType is any of those we have learned so far. It could be an int,
an
unsigned int, a char, a double, etc. While a data
type can be made of one word (such as short) or more than one word (such
as unsigned int), the new name you are creating for the data type must be
in one word.
An example of renaming a data type is:
typedef int NumberOfStudents;
In this case, NumberOfStudents is just a new name for an int. It can be
used as a new data type exactly as if you were using an int. Here is an
example that redefines an
int data type and uses it in a program:
|
#include <iostream>
using namespace std;
int main()
{
typedef int NumberOfStudents;
NumberOfStudents Grade1, Grade2;
cout << "Enter the number of students.\n";
cout << "Grade 1: ";
cin >> Grade1;
cout << "Grade 2: ";
cin >> Grade2;
cout << "\nNumber of students:";
cout << "\n1st Grade: " << Grade1;
cout << "\n2nd Grade: " << Grade2 << "\n\n";
return 0;
}
Here are examples of creating new
names of existing data types using typedef:
typedef unsigned int UINT;
typedef unsigned char UCHAR;
typedef unsigned long ULONG;
typedef long double LONGDOUBLE;
|
A constant is a value that does not change. There are various categories of constants you will be using in your programs.
For example, the algebraic numbers you are aware of are constants because they never change. Examples of constant numbers are 12, 0, 1505, or 88146. Therefore, any number you can think of is a constant.
Every letter of the alphabet is a constant and is always the same. Examples of constant letters are d, n, c.
Some characters on your keyboard represent symbols that are neither letters nor digits. These are constants too. Examples are #, &, |, !.
Some values would be constant by default, but their constancy sometimes depends on the programmer. For example, one programmer can define a constant PI as
3.14. Another programmer can decide that the constant PI would be 3.14159. Therefore, you will be defining your own constant values as you see fit for the goal you are trying to achieve.
There are two main techniques you use a constant value in C++. To simply display it using the
cout << extractor, you can type its value on the right side of the << symbols. You can also define it first using an appropriate name, and then using that name to display the constant.
Here are two examples |
#include <iostream>
using namespace std;
int main()
{
cout << 28;
cout << "\nStudent Age: " << 14;
cout << "\n\n";
return 0;
}
To create your own constant value, you can give it a name. This allows you to manage it from one standpoint. Imagine you want to use 3.14 in various sections of the program. If you decide to change the number from 3.14 to 3.14159 or another value, you would have to find every mention of 3.14; this can lead to a programming error. The alternative is to declare a variable and assign it the desired value. The new and defined value is called a constant.
|
Macro Definition of a Constant |
|
There are two main techniques used to create constant values. The old way, which was used with the C language, widely used in documentation and help files consists of using the
define keyword. The syntax of creating a constant using the define keyword is:
#define ConstantName ConstantValue
The # symbol and the define keyword are required; they inform the compiler that the following name represents a constant. The
ConstantName represents a valid name for the desired constant. The name follows the same rules we learned for defining names in C++. The
ConstantValue can be a character, an integer, a floating-point value, or an expression. If the constant value is an integer or a floating-point value, you can type it. If the value is a character, include it between single-quotes.
If the value is a word or a sentence, include it in double-quotes. Because
#define is referred to as a macro, the definition doesn't end with a semi-colon.
Here are examples:
#define AGE 12 // AGE represents the constant integer 12
#define ANSWER ‘y’
#define MAXSTUDENTS 35
#define PI 3.14159 // PI represents 3.14159
After defining the macro, the last value can be used
in a program as a constant. For example, you can display it using cout.
Here is an example:
#include <iostream>
using namespace std;
#define EmploymentStatus "Full Time"
int main()
{
cout << "Status: " << EmploymentStatus << endl;
return 0;
}
This would produce:
Status: Full Time
|
Another technique of creating a constant consists of using the
const keyword. The formula used with the const keyword is:
const ConstantName = ConstantValue;
Or
const DataType ConstantName = Value;
The const keyword is required to inform the compiler that you are creating a constant.
The
ConstantName specifies the name of the constant value; it follows the rules we have applied for the names of variables.
The
DataType, although optional, is used to let the compiler know the kind of
value the constant is. If you don't specify a data type, the compiler would assume that the constant is an integer. Therefore, make it a habit to always specify the data type when creating a constant using the
const keyword. The data type can be any of those we have used so
far.
Use the assignment operator to assign the desired constant to the name.
Examples of creating constants with the const keyword are:
const float PI = 3.14159;
const unsigned int MaxStudents = 42;
const string Country = “New Zealand”;
const double Distance = 1678212;
After creating the constant, you can use its name in
the program to access its value. As we will learn in the next lesson, you
can involve it in an operation. You can also display its value using cout.
Here is an example:
#include <iostream>
using namespace std;
int main()
{
const double PI = 3.14159;
cout << "PI = " << PI << endl;
return 0;
}
This would produce:
PI = 3.14159
|
To assist you with constants, the C++ language ships
with many of them so you can use them directly in you code. Those constants have names they can be recognized with. For example, the minimum value of the
short integer is called SHRT_MIN. In the same way, the maximum
short integer is identified by
SHRT_MAX. You can use either of these constants as follows: |
#include <iostream>
#include <climits>
using namespace std;
int main()
{
cout << "The minimum signed character: " << SCHAR_MIN << "\n";
cout << "The maximum signed character: " << SCHAR_MAX << "\n";
cout << "The minimum short integer is: " << SHRT_MIN << "\n";
cout << "The maximum short integer is: " << SHRT_MAX << "\n\n";
return 0;
}
This would produce:
The minimum signed character: -128
The maximum signed character: 127
The minimum short integer is: -32768
The maximum short integer is: 32767
According to the C++ Standard, the integral constants are defined in the climits library but they can be defined as one compiler chooses to. For example, in Borland C++ Builder, they are in the
_lim.h library. In the KDevelop and Microsoft C++ compilers they are in the
limits.h file.
The integer constants defined in the climits library are: |
CHAR_BIT |
INT_MAX |
LONG_MAX |
SCHAR_MAX |
SHRT_MAX |
CHAR_MAX |
INT_MIN |
LONG_MIN |
SCHAR_MIN |
SHRT_MIN |
CHAR_MIN |
UINT_MAX |
ULONG_MAX |
UCHAR_MAX |
USHRT_MAX |
|
|
|
|
MB_LEN_MAX |
|
In the same way, C++ provides constant double-precision numbers in the cfloat library. These constants are: |
DBL_DIG |
FLT_DIG |
LDBL_DIG |
DBL_EPSILON |
FLT_EPSILON |
LDBL_EPSILON |
DBL_MANT_DIG |
FLT _MANT_DIG |
LDBL_MANT_DIG |
DBL_MAX |
FLT _MAX |
LDBL_MAX |
DBL_MAX_10_EXP |
FLT_MAX_10_EXP |
LDBL_MAX_10_EXP |
DBL_MAX_EXP |
FLT _MAX_EXP |
LDBL_MAX_EXP |
DBL_MIN |
FLT _MIN |
LDBL_MIN |
DBL_MIN_10_EXP |
FLT_MIN_10_EXP |
LDBL_MIN_10_EXP |
DBL_MIN_EXP |
FLT _MIN_EXP |
LDBL_MIN_EXP |
|
FLT_RADIX |
|
|
The C++ Standard also defines a constant as NULL. This constant is used to designate that a pointer does not hold a valid value. The NULL constant is defined in the
cstddef library. |
|
|