Basic Data Types and Calculations - Functional Notation for Initial Values (
Page 13 of 14 )
An alternative notation for specifying the initial value for a variable when you declare it is called functional notation. The term stems from the fact that you put the initial value between parentheses after the variable name, so it looks like a function call, as you’ll discover later on.
An alternative notation for specifying the initial value for a variable when you declare it is called . The term stems from the fact that you put the initial value between parentheses after the variable name, so it looks like a function call, as you’ll discover later on.
Let’s look at an example. Instead of writing a declaration as
int unlucky = 13;
you have the option to write the statement as
int unlucky(13);
Both statements achieve exactly the same result: they declare the variable
unlucky
as type
int
and give it an initial value of 13.
You can initialize other types of variables using functional notation. For instance, you could declare and initialize a variable to store a character with this statement:
char letter('A');
However, functional notation for initializing variables is primarily used for the initialization of variables of a data type that you’ve defined. In this case, it really does involve calling a function. The initialization of variables of the fundamental types in C++ normally uses the approach you have taken up to now. You’ll have to wait until Chapter 11 to find out about creating your own types and how those kinds of variables get initialized!
Summary
In this chapter, I covered the basics of computation in C++. You learned about most of the fundamental types of data that are provided for in the language. The essentials of what I’ve discussed up to now are as follows:
- Numeric and character constants are called literals.
- You can define integer literals as decimal, hexadecimal, or octal values.
- A floating-point literal must contain a decimal point, or an exponent, or both.
- Named objects in C++, such as variables, can have names that consist of a sequence of letters and digits, the first of which is a letter, and where an underscore is considered to be a letter. Upper- and lowercase letters are distinguished.
- Names that begin with an underscore followed by a capital letter, and names that contain two successive underscores, are reserved for use within the standard library, so you shouldn’t use them for names of your own variables.
- All literals and variables in C++ are of a given type.
- The basic types that can store integers are
short
,
int
, and
long
. These store signed integers by default, but you can also use the type modifier
unsigned
preceding any of these type names to produce a type that occupies the same number of bytes but only stores unsigned integers.
- A variable of type
char
can store a single character and occupies 1 byte. The type
char
may be
signed
or
unsigned
by default, depending on your compiler. You can also use variables of the types
signed char
and
unsigned char
to store integers.
- The type
wchar_t
can store a wide character and occupies either 2 or 4 bytes, depending on your compiler.
- The floating-point data types are
float
,
double
, and
long double
.
- The name and type of a variable appear in a declaration statement ending with a semicolon. A declaration for a variable that results in memory being allocated is also a definition of the variable.
- Variables may be given initial values when they’re declared, and it’s good programming practice to do so.
- You can protect the value of a “variable” of a basic type by using the modifier
const
. The compiler will check for any attempts within the program source file to modify a variable declared as
const
.
- An lvalue is an object or expression that can appear on the left side of an assignment. Non-
const
variables are examples of lvalues.
Although I discussed quite a few basic types in this chapter, don’t be misled into thinking that’s all there are. There are some other basic types, as well as more complex types based on the basic set, as you’ll see, and eventually you’ll be creating original types of your own.