A reference is a variable name that is a duplicate of an
existing variable. It provides a techniques of creating more than one name to
designate the same variable. The syntax of creating or declaring a reference is:

DataType &ReferenceName = VariableName;

To declare a reference, type the variable’s name preceded
by the same type as the variable it is referring to. Between the data type and
the reference name, type the ampersand operator “&”. To specify what
variable the reference is addressed to, use the assignment operator “=”
followed by the name of the variable. The referred to variable must exist
already. You cannot declare a reference as:

int &Mine;

The compiler wants to know what variable you are referring
to. Here is an example:

If you change the value of the variable, the compiler
updates the value of the reference so that both variable would hold the same
value. In the same way, you can modify the value of the reference, which would
update the value of the referred to variable. To access the reference, do not
use the ampersand operator; just the name of the reference is sufficient to the
compiler. This is illustrated in the following:

Some of the identifiers we have used so far and some others
we will learn further with arrays, pointers, and objects can be simplified and
reduced to a new one-word identifier. C++ allows you to define a new variable
using typedef. Indeed, typedef is not a new or other identifier.
It provides a technique of redefining any of the known identifiers or by naming
those that are combined.

The formula of using the typedef is:

typedef IdentifierNewName;

The typedef word is required to let the compiler know
that you are creating a new identifier. The Identifier is any of those we have
learned so far. It could be an int, an unsigned int, a char, a double, etc. An
example of declaring a typedef is:

typedefint NumberOfStudents;

In this case, NumberOfStudents is just a new name for an
int. It can be used as a new identifier exactly as if you were using an int.
Here is an example that redefines an int identifier:

The compiler you are using shipped with a lot of data types, some of which
are redefined versions of existing C++ data types. Therefore, the identifiers
you will be using in C++ Builder come in three sets. The C++ language has its
own data types that are integers, floating values, etc. Borland C++ Builder has
a list of redefined types in the sysmac.h library. The Microsoft Windows
operating systems ships with the Win32 library that has a set of redefined data
types. To see its list, in the MSDN library, do a search on Data Types [Win32].

In this book, we will interchangeably use any data type we see fit for the
task at hand, without identifying whether it is part of C++, Borland C++
Builder, or Win32. For that reason, from now on, many of our programs will
include the VCL library.

A constant is a value that does not change. There are
various categories of constants you will be using in your programming life:
Those that are mathematically defined (such as numbers); those that are defined
by, and are part of, the C++ language; those defined by the Microsoft Windows
operating system you are using, and those defined by the Borland C++ Builder
compiler or libraries. To make their management easier, these constant values
have been categorized and defined in particular libraries. Throughout this book,
you will be using them.

The algebraic numbers you have been using all the time 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 const PI as 3.14; another programmer can decide that the constant PI
would be 3.14159; yet another programmer would use a more precise value.
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 to display a constant
value in C++. To simply display it using the cout operator, you can use 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.

Practical Learning: Using Constant Values

On the Standard toolbar, click the New button

On the New Items dialog, click Console Wizard and click OK

On the Console Wizard dialog, make sure that the C++ radio button, the
VCL, and the Console Application check boxes are selected:

You can create your own constant that represents anything
you want. The safest technique of using a constant is to give a name. This
allows you to manage it from one standpoint. For example, if you plan to use a
number such as 3.14 that represents PI, you can simply use the constant 3.14.
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 be cumbersome and considered bad
programming. The alternative is to create a variable and assign it the desired
value. That new and defined value is called a constant.

To create a constant, you use the const keyword. The
simplest way you can do this is to type the const keyword followed by a name,
followed by =, followed by the desired value, and terminated by a semi colon.
Here is an example:

const NumberOfDoors = 4;

After creating such a constant, you can display its value
using the cout extractor:

You can also involve such a constant in an operation as we
will learn in the next lesson.

When creating a constant as done above, the compiler assumes
that the constant is of type integer. If you initialize it to a value that is
not an integer, the compiler would try to convert it into an integer. Consider
the following example:

The C language provides a preprocessor used to create
macros. A macro is an action you want the compiler to perform for your program.
The particularity of macros, or most macros, is that the compiler counts on you
to know what you are doing. Many of such macros are created using the #define
preprocessor.

Based on this concept of macros, you can create a constant.
The formula of creating a constant using the define keyword is:

#define ConstantName ConstantValue

The # symbol and the define keyword are
required; they inform the compiler to consider 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++. To distinguish the constant from other names of variables, it is
sometimes a good idea to write it in uppercase, although it is perfectly normal
to have it in lowercase or any case combination you desire.

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 constant value is a string, include it between
double-quotes. The definition of the constant does not end with a semi-colon.

As you can see, there is no precision as to what type of
constant you are creating when using #define. The truth is that #define
doesn't create a constant. It is simply telling the compiler that the word after
#define will be used to represent whatever follows that ConstantName.
This means that, in reality, what follows ConstantName can be anything;
it can even be an expression. here is an expression:

In reality, the #define technique was implemented in
the C language and C++, as a child of C, simply inherited it from its parent.
The #define routine is still supported and you will encounter it in many
documents. In fact, you are not necessarily discouraged from using it but the
golden rule is to know what you are doing.

Operating System and Compiler Constants

Because constants are highly used in mathematics and other
scenarios, both the C++ language and the Borland C++ Builder compiler are
equipped with various constants. Although you can create your own constants
anytime, you should be aware of these constants and usually use them instead of
creating new ones, unless yours are more reliable.

Some of the constants that are part of the C++ language are
defined in the _lim.h library. Other constants are defined in various
other files such as windows.h, System.hpp, SysUtils.hpp, math.hpp,
etc. For example, the minimum value of the short integer is SHRT_MIN;
in the same way, the maximum short integer is identified by SHRT_MAX.
You can use either of these constants as follows:

Display the content of the C:\Program Files\Borland\Cbuilder\Include
folder

Double-click the _lim.h file to open it.
Since you get the last way, I will not provide.

Namespaces

Definition

A namespace is a section of code, delimited and referred to
using a specific name. A namespace is created to set apart a portion of code
with the goal of reducing, otherwise eliminating, confusion. This is done by
giving a common name to that portion of code so that, when referring to it, only
entities that are part of that section would be referred to.

A namespace is not a variable. It is not
a constant. It is not a class. And it is not a file. It is simply a name
used to refer to a section of code.

Creating a Namespace

The syntax of creating a name space is:

namespace Name { Body }

The creation of a namespace starts with the (required)
namespace keyword followed by a name that would identify the section of code.
The name follows the rules we have been applying to C++ names. A namespace
contains a body; this is where the entities that are part of the namespace would
be declared or defined. The body of the namespace starts with an opening curly
bracket “{” and ends with a closing curly bracket “}”. Here is an
example of a simple namespace:

namespace Mine
{
int a;
}

The entities included in the body of a namespace are referred to as its
members.

Using the Scope Access Operator

To access a member of a namespace, there are various
techniques you can use. The scope access operator “::” used for classes is
the same you can use here. To do this, type the name of the namespace, followed
by the scope access operator “::”, followed by the member you are want to
access. Only the members of a particular namespace are available when using its
name. For example, to access the member “a” of the Mine namespace above, you
can write:

Mine::a;

Once you have access to a namespace member, you can
initialize it or display its value using the cout extractor operator. Here is an
example:

In the same way, you can manipulate the members of a namespace as your
application needs. They can be part of a conditional statement or the can be
involved in any type of arithmetic operation. Here is an example:

When using the scope access operator “::” to call members of a
namespace, each member has to be qualified with the operator. This reduces
confusion as to what variable you are referring to and also allows you to
use the same name in different parts of a program. For example, in a
function, you can have a local variable that holds the same name as a member
of a namespace. When calling such a variable, the “::” operator can set
apart what variable you are referring to. To demonstrate this, a Gender
variable is declared in both a name space and the main() function. The use
of the access operator helps to specify which Gender variable is called at
one time. To test it, change the main() function as follows:

The scope access operator “::”provides a safe mechanism
to access the members of a namespace. If the namespace is very large and the
application needs constant access, this might be a little redundant. Another
technique used to access the members of a namespace involves using two keywords:
using and namespace.

To call a namespace, on the section of the program where you
need to access the members, type:

using namespace NamespaceName;

Both the using and the namespace keywords are required by
the compiler. The NamespaceName represents the name of the namespace
whose member(s) you want to access. Using this technique, the above program can
be written:

In a mixed environment (we are still inside of one function
only) where a local variable holds the same name as the member of a namespace
that is being accessed with the using namespace routine, you will be sensitive
to the calling of the same name variable. When manipulating such a name of a
variable that is present locally in the function as well as in the namespace
that is being accessed, the compiler will require more precision from you. You
will need to specify what name is being called.

Practical Learning: The using namespace Keywords

To use the using and namespace keywords, change the main() function as
follows:

There are many solutions you can use to resolve this issue. The best and
safest technique is to use different names; that is, when declaring your
variables, use names that are not part of the namespace you are trying to
access. The problem is that it is very unrealistic to know all names that
are used by C++, by the operating system, and by the Visual Component
Library. The other solution is to qualify the member of a namespace whenever
you call it. Therefore change the above program as follows:

Various namespaces can be part of the same file and the same
application. You can create each namespace and specify its own members in its
delimiting curly brackets. With various namespaces on the same file or
application, you can have the same variables in different namespaces. Here is an
example of two namespaces:

Using the scope access operator like that, you can perform
any operation on any member of one namespace applied to a member of another
namespace.

We saw earlier that the using namespace routine allows
accessing the members of a namespace. This technique is based on the scope of
the routine. After typing it, if the name of a variable appears under a using
namespace, the compiler would need to reconcile or identify it; if the name of
such a variable is not recognized as part of the namespace that is being
accessed, the program would not compile. For example, here is an example that
uses two using namespace routines:

The above program would not compile because the compiler
does not understand what Discount is being referred to in the second Discount
call: is it InterestAndDiscount::Discount or BuyAndSell::Discount?

If you want to use different namespaces with the using
namespace routine, each namespace will have to control its scope. One
solution would be to create a “physical” scope for each namespace. Here is
an example:

Before creating a “physical” scope, we saw that the
compiler is able to point out what problem occurred at compilation time.
Fortunately, the compiler is able to explicitly designate what problem it
encountered. In this case there is a conflict in name resolution: two namespaces
have a member of the same name.

The solution, which is commonly used, is to qualify the
variable that is causing the conflict. You can qualify only the second Discount
call because the compiler will associate the first Discount call with the first
using namespace. The safest way is to qualify both calls to the Discount
variable, as follows:

Nesting a namespace is the ability to include a namespace
inside (as part of the body) of another namespace. To do this, create the
intended namespace as a member of the parent namespace. The nested namespace
should have its own name and its own body. Here is an example:

To access a member of a nested namespace, first call its
parent, type the :: operator, type the name of the nested namespace, followed by
the :: operator, then type the name of the variable you are trying to access.
Here is an example:

Following the same logic, you can have as many namespaces
and many nested namespaces in your application as you desire. If you nest a
namespace, you can use as many :: operators to qualify each member of the nested
namespace you want. Here is an example:

Otherwise, you can create a using namespace for each
namespace and make sure that each one of them controls its scope. As long as you
are using the scope access operator to identify the variable that is being
accessed inside of a using namespace, you can call the member of any namespace
in any scope, provided you qualify it.

When a namespace is nested, a first using namespace can be
used to nvoque the parent namespace. You can use a scope access operator
“::” with a using namespace to further refine the members of a sub-namespace
as follows:

The C++ Standard provides a namespace called std. The
std namespace includes a series of libraries that you will routinely and
regularly use in your programs. In the implementation of the Borland C++ Builder
compiler, the std namespace contains almost all possible libraries you
will need when creating your console applications.

The following libraries are part of the std namespace:

algorithm

iomanip

list

ostream

streambuf

bitset

ios

locale

queue

string

complex

iosfwd

map

set

typeinfo

deque

iostream

memory

sstream

utility

exception

istream

new

stack

valarray

fstream

iterator

numeric

stdexcept

vector

functional

limits

The following additional libraries can be used to include C header files into
a C++ program:

cassert

cios646

csetjmp

cstdio

ctime

cctype

climits

csignal

cstdlib

cwchar

cerrno

clocale

cstdarg

cstring

cwctype

cfloat

cmath

cstddef

Therefore, whenever you need to use a library that is part
of the std namespace, instead of typing a library with its file
extension, as in iostream.h, you can just type the name of the library as
in iostream. Then, on the second line, type using namespace std;. As an example, instead of typing

#include <iostream.h>

You can type:

#include <iostream>
using namespace std;

Because this second technique is conform with the C++ Standard, we will use it
whenever we need one of its libraries.

Built-In Namespaces

The Borland C++ Builder compiler is equipped with many
namespaces. In fact, unlike most other compilers, (almost) everything in C++
Builder is included in a a certain namespace. most of the time, you will not be
concerned with this fact but some of the classes you will need when doing
graphical programming require explicit call to a namespace.