PEG:C++ Lesson 3 for Pascal Users

From PEGWiki
Revision as of 02:58, 15 December 2009 by Jargon (Talk | contribs) (+link to next)

Jump to: navigation, search
← Previous Lesson | Next Lesson →

C++ variables are almost exactly like Pascal variables, so you shouldn't have much trouble adjusting. A variable is declared with a name and a type. We will cover four basic classes of types in this lesson.

First, naming. In standards-compliant C++, variable identifiers can consist of letters, digits, and underscores. The first character must not be a digit. Variable names cannot be reserved words. Just like in Pascal. But C++, unlike Pascal, is case-sensitive. Everything in C++ is case sensitive, not just variable names, but also type names, function names, and reserved words. You may have variables named ab, Ab, aB, and AB (although this is usually extremely poor style), and they are completely different as far as the compiler is concerned. Variable names may be very long in C++; exactly how long depends on your compiler, but you can't exceed the limit without greatly inconveniencing yourself anyway.

A. Integral types

Integral types hold integers. Here are some C++ types, corresponding Pascal types, descriptions of the types, and sizes (in bits):

C++ type           : Pascal type (TP/FP) : range             : bit width
char               : shortint            : -2^7 to 2^7 - 1   : 8
unsigned char      : byte                : 0 to 2^8 - 1      : 8
short              : integer/smallint    : -2^15 to 2^15 - 1 : 16
unsigned short     : word                : 0 to 2^16 - 1     : 16
int                : longint             : -2^31 to 2^31 - 1 : 32
unsigned int       : none/dword          : 0 to 2^32         : 32
long long          : comp/int64          : -2^63 to 2^63 - 1 : 64
unsigned long long : none/qword          : 0 to 2^64         : 64

(Technically, they don't have to be exactly this --- int, for example, can take up 42 bits if the crazy designers of the system so desire. But they will be correct on your computer, on school computers, and on online judges, so for your purposes they are always correct.)

As you can see, the typing system in C++ is pretty simple: chars are 8 bits, shorts are 16, ints are 32, long longs are 64; each type has an unsigned variation; an N-byte signed type ranges from -2^(N-1) to +2^(N-1)-1, and a N-byte unsigned type ranges from 0 to 2^N-1. Most of the time you will be using ints.

B. Floating-point types

There are three floating-point types in C++. If you don't already know, a floating-point type is stored in a fashion similar to scientific notation, except that it's binary: so for example 9 is expressed as (1.001) x 2^(11). So it can be very large or very small when the need arises, but it will remember only the first few significant digits. And it can be positive, negative, or zero.

The C++ type float is the same as the type single in Pascal. It has 24 significant bits of precision (meaning 7-8 decimal digits), and it can be as small as around 10^-38 and as large as around 10^38. In total it takes up 32 bits.

The type double is the same in C++ and Pascal. It occupies 64 bits, can be as small as around 10^-308, and as large as around 10^308, and has 53 bits of precision, or 15-16 decimal digits. Most of the time you are going to want to use double.

The type long double is the same as the type extended in Pascal. It has 65 bits of precision or 19-20 digits, and can be as small as around 10^-4932 or as large as around 10^4932. Use this whenever you need that extra precision. Note that on SPOJ (and possibly some other judges, not including the PEG Judge), long double is the same as double.

There is no C++ type that is the same as the TP real type.

C. The char type

In C++, the type that holds characters (like char in Pascal) is the same as the one that holds signed 8-bit integers. In fact, we can do math directly with characters, so for example we can add 'a' and 3 directly in C++ to get 'd'. This is because a character and its ASCII value are considered equivalent, so 'a' + 3 is the same as 97 + 3, which equals 100, the ASCII code of 'd'. Internally, then, they behave just like integer types. However, when performing I/O, they are treated like actual characters. Character constants are enclosed in single quotes in C++, just as in Pascal.

D. The bool type

This type is like the Pascal boolean. It behaves like an integer type whose size varies depending on the compiler and the environment, but should not be used in arithmetic. It can assume the values true and false (which are usually actually 1 and 0), and it is not a good idea to perform any sort of arithmetic with them --- it is extremely poor style and not standards-compliant. Do not perform I/O with bools. Like in Pascal we can consider the statement 3 > 5 to have a value equal to false --- the bool type serves much the same purpose in C++ as the boolean type in Pascal.

E. Declaring Variables

In C++, we are able to declare variables almost anywhere we want. A variable declaration generally looks like <type> <name>;. For example, int x; declares an int variable called x. For now, as you will be writing all code between the { and } of int main(), all variables will be declared there, and as soon as you declare a variable you can use it wherever you want, but a variable does not exist on any lines before it is declared. So for example:

int main()
     int x;

is OK, but

int main()
     int x;

is not. (It will produce a compile error.)
We can also assign a value to a variable as soon as it is declared: int x = y + z; instructs the compiler to create a new variable x of type int and to initialize it to the sum of y and z.

WARNING: the C++ compiler will NOT automatically initialize simple variables for you. An uninitialized numerical value cannot be depended upon to be zero, and an uninitialized bool cannot be depended upon to be false.