A character is a symbol that displays on your
screen. It can be a letter such as a, b, c, d, e, f, g, h, I, j, k, l,
m, n, o, p, q, r, s, t, u, v, w, x, y, and z. It can also be a letter
from A to Z. It can also be a digit such as 0, 1, 2, 3, 4, 5, 6, 7, 8,
and 9. It can also by a special characters such as ` ~ # $ ! @ % ^ &
* ( { [ ) } ] | \ : ; “ ‘ + - < _ ? > , / =. A character is
really an integer whose value can range from -128 to 127.
To declare a character variable, you use the char
keyword. Here is an example:
using namespace System;
int main()
{
char AlphaLetter;
Console::WriteLine();
return 0;
}
Since a char variable represents one symbol,
to initialize it, enclose the initial value in single quotes. Here are
examples:
using namespace System; int main() { char AlphaLetter = 'd'; char Pick('G'); return 0; }
A character variable can also be declared with the signed
char data type. This type of variable would be an integer whose
value can range from –128 to +127.
One of the limitations of the char data type
is that it can fit only on a single character (limited to 8 bits). To
support a wider range of characters, you can use the Char or the __wchar_t
data types to declare a character variable. In fact, you should use either Char or __wchar_t whenever
you need a character. The Char and the __wchar_t data
types are defined in the System namespace. Here is an example:
using namespace System;
int main()
{
__wchar_t AlphaLetter = 'F';
Console::WriteLine(AlphaLetter);
return 0;
}
This would produce:
F Press any key to continue
Based on their structures, Char and __wchar_t
variable can accommodate letters in many other languages than
Latin-based. When initializing a variable declared with Char or __wchar_t,
to indicate that you are using a Unicode character, you can precede the
quoted value with L. Here is an example:
using namespace System;
int main()
{
Char Letter = L'D';
Console::WriteLine(Letter);
return 0;
}
This would produce:D Press any key to continue
A string is a group of characters considered as one
unit, as opposed to a single character as we introduced above. Unlike
some other languages such as (Object) Pascal or (Visual) Basic, C++
doesn't have a data type that can hold a group of characters as one
entity. The alternative was to "create" and adapt a
string data type in additional library. This problem was partially
solved in a library called Standard Template Library (STL). Therefore,
the STL provides the string data type.
To declare a variable that can hold a string, you
can use the string data type (the string is a class but we will
consider it a data type for simplicity). The value of the string must be
included in double-quotes. Here is an example:
string Course = "Business Mathematics";
The string data type is part of the std
namespace and it is defined in the string library. When using the
string data type, make sure you include the string library
and use the std namespace. To display the value of a
string variable, you can use the cout extractor. Here is an example:
#include <string> #include <iostream> using namespace std; using namespace System; int main() { string Course = "Business Mathematics"; cout << Course; Console::WriteLine(); return 0; }
This would produce:
Business Mathematics Press any key to continue
The integers we have used so far don't allow decimal
values. C++ provides floating identifier values that would solve this
problem. To declare a variable that involves a decimal value, you can
use the float keyword. Here is an example:
using namespace System; int main() { float Fraction = 12.35; Console::WriteLine(Fraction); Console::WriteLine(); return 0; }
A float variable can hold a number that
ranges from 3.4 x 10-38 to 3.4 x 1038. Besides, or
instead of, float, you can use the Single data type to declare a
variable that holds a simple decimal value. The Single data type
is defined in the System namespace.
One of the limitations of the float data type is
that it provides less precision than required sometimes (in fact, the
Microsoft C++ compiler, always
displays a warning when you use the float data type to declare a
variable). The alternative is to use the double keyword or the Double
data type to declare a variable that would hold decimal numbers. The Double
data type is defined in the System namespace.
A double-precision variable declared with double or
Double can hold a decimal or fractional number that ranges from 1.7 x 10-308
to 1.7 x 10308. It is used for numbers that and/or are very
large.
using namespace System; int main() { System::Double Fraction = 12.35; Console::WriteLine(Fraction); return 0; }
This would produce:
12.35 Press any key to continue
You may have found out that when you declare and initialize
a float or Single variable, the compiler generates a warning. Consider the
following example:
using namespace System; int main() { float Fraction = 12.35; Console::WriteLine(Fraction); Console::WriteLine(); return 0; }
When compiled, you would get the following warning:
.\Exercise.cpp(5) : warning C4305: 'initializing' : truncation from 'double' to 'float'
Although the program would compile fine. By default, when
the compiler sees a value such as 12.35, for the sake of precision, it tends to
store it as a double value. If you really want the variable to be treated as a
float or Single, add the f or F to its right. Here is an example:
using namespace System;
int main()
{
float Fraction = 12.35f;
Console::WriteLine(Fraction);
Console::WriteLine();
return 0;
}
This time, the compiler will not generate a warning.
Remember that you can use f (lowercase) or F (uppercase).
The data types we have used so far may have names we
are not familiar with. C++ allows you to customize the name of a data type
to a name you are more familiar with. You would not (and are not allowed
to) create a new data type, you would only "redefine" the name
of an existing data type.
To customize the name of a data type, you can use
the typedef keyword. The formula used is:
typedef KnownDataType NewName;
The typedef keyword is required to let the
compiler know that you are creating a new data type. The data type must
be one that exists already, such as those we have reviewed so far. It
could be an int, an unsigned int, an Int32, a char,
a double, etc. An example of using a typedef is:
using namespace System;
int main()
{
typedef unsigned int PositiveNumber;
return 0;
}
In this case, PositiveNumber is just a new
name for an unsigned int. It can be used as a new data type
exactly as if you were using an unsigned int. Here are examples:
using namespace System; int main() { typedef unsigned int PositiveNumber; typedef double Salary; PositiveNumber Gender = 1; Salary WeeklySalary = 1450.88; Console::Write("Gender: "); Console::WriteLine(Gender); Console::Write("Salary: "); Console::WriteLine(WeeklySalary); return 0; }
This would produce:
Gender: 1 Salary: 1450.88 Press any key to continue . . .
A reference is a variable that is a duplicate of an
existing variable. It provides a technique 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:
using namespace System; int main() { int Number = 228; int &Nbr = Number; Console::WriteLine(Number); Console::WriteLine(Nbr); return 0; }
The ampersand operator between the data type and the
reference can assume one of three positions as follows:
int& Nbr; int & Nbr; int &Nbr;
As long as the & symbol is between a valid data
type and a variable name, the compiler knows that the variable name (in
this case Nbr) is a reference.
Once a reference has been initialized, it holds the
same value as the variable it is pointing to. You can then display the
value of the variable using either of both:
This would produce:
Number: 228 Its reference: 228 Press any key to continue
If you change the value of the variable, the
compiler updates the value of the reference so that both variables 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:
This would produce:
Number: 228 Its reference: 228 Number: -15008 Its reference: -15008 Number: 28114 Its reference: 28114 Press any key to continue
It is very important to remember the relationship
that a variable in the stack has with its value: A variable in the stack
contains its own value. Whenever you access this variable, it gives you
copy of its value and retains the original. Once you have that value,
you can do what you want with it and this would not have any impact on
the variable itself or its value. For example, if you assign such a
value to another variable, that other variable receives its own copy.
Let's illustrate with the following program:
#include <iostream> using namespace std; using namespace System; int main() { // TODO: Please replace the sample code below with your own. int Number1 = 255; int Number2 = Number1; Console::WriteLine(Number1); Console::WriteLine(Number2); Console::WriteLine(); return 0; }
This would produce:
255 255 Press any key to continue
Notice that when the value of the first variable has
been assigned to the second variable both variables hold the same value.
With another assignment, you can change the value of the second variable
and the first would keep its value:
#include <iostream>
using namespace std;
using namespace System;
int main()
{
// TODO: Please replace the sample code below with your own.
int Number1 = 255;
int Number2 = Number1;
Console::WriteLine(Number1);
Console::WriteLine(Number2);
Console::WriteLine();
Number2 = -48;
Console::WriteLine(Number1);
Console::WriteLine(Number2);
Console::WriteLine();
return 0;
}
This would produce:
255 255 255 -48 Press any key to continue
This comparison is important because its sets apart
the references as we saw above. This will be even more important with
pointers in the next lesson.
If you create a C++ reference as done above, the variable is
stored in the stack. When the program ends, the compiler reclaims the memory the
variable was using. The C++/CLI language provides another way to declare such a
reference. In this case, the CLR garbage collector would be able to reclaim the
memory that the variable was using. This type of variable is called a tracking reference.
To create a tracking reference, use the percent operator
"%" instead of the ampersand. Here is an example:
int main()
{
int % PropertyValue;
return 0;
}
Like a native reference, in order to use a tracking
reference, you must initialize it with a variable that has been declared and
initialized appropriately. Here is an example:
using namespace System;
int main()
{
double Value = 468550;
double % PropertyValue = Value;
Console::Write("Property Value = $");
Console::WriteLine(Value);
Console::Write("Property Value = $");
Console::WriteLine(PropertyValue);
return 0;
}
After creating a tracking reference, if you change the value
of the variable, the value of the tracking reference would be changed also. If
you update the value of the tracking reference, the value of the variable
would be updated also.
|
Data Types
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment