[Phy 405/905 Home Page] [ Lecturer ]

1. Types (part 1)

Last time I tried to sneak in the idea of the many basic types in C++ by talking about the (not so) many types of literals. It's not that the number of types is confusing, it's actually the automatic conversions between them that is both confusing and usually very convenient (when combined with type-safety for function arguments). So, we'll just plunge into the topic, and emerge on the other side with an idea of how things'll be for classes. On the way, we'll mention the difference between declaration and definition of objects, derived types (types built out of the fundamental types) and incidentally show how to declare and define functions the type-safe way.


Fundamental Types

Objects and lvalues

Before we get too far, here's a crucial definition:
object
"a region of storage" (ARM [[section]]3.7)
Objects are the "things" that programs, subroutines, functions, operators ... assign values to. Functions are not objects (though they are a type of type), but together with objects they form the set of:
lvalue
"an expression referring to an object or function" (ARM [[section]]3.7)
I just mention lvalues because they'll come up later in the context of operators. For the moment, note that both objects and functions have definite types. So do literals (that's why I brought them up last time). Let's move on to types.

List of Fundamental types

They are:

Integers - {short int, int, long int} of possibly increasing sizes which may be qualified by {signed,unsigned}, where signed is the default, and an unsigned vs. signed int takes up the same space. The type "int" is supposed to be "natural" for the particular machine. The size in bytes of each type (any type, actually) can be gotten through the sizeof() operator:

cout << sizeof(char)<<"\n"; // outputs '1'
The different sizes are not guranteed to be different, only properly ordered: sizeof(int) <= sizeof(long int).

A short-hand notation (which is a bad habit) is possible, of dropping the "int" for any of the qualified int types: unsigned int -> unsigned, signed long int -> signed long. This is behavior is implemented just to conform to ANSI C code.

Finally, the unsigned int types are often used as multi-bit flags; being unsigned, all n=8*sizeof( whatever int) bits are available for use, and arithmetic is mod 2^n, with no overflow. Good for, for example, data encryption.

Floating point numbers - {double, float, long double}

Characters - {char, signed char, unsigned char}. The type char always signifies the type of character (signed or unsigned) appropriate to the machine for which the program is being compiled. For physics programming you might not have too much to do with the other two types. All three take up the same space

Then there are all the other derived types that are - you guessed it - derived from the fundamental types, including arrays, pointers, constants, ..., classes (our eventual goal), structures,... and functions. You're familiar with arrays, possibly with pointers, but it is curious to think of a function of given argument(s) type and return value type as a type in and of itself; but there you have it, that's what's behind function overloading.


Simple declaration&definition

Now, to put all the above to any practical purpose, - to use an object, for example, a variable of type double, in a program - we must first name it (my_sqrt, or x, or ...). The same is true of functions as well. The same is true of types, templates, and other entities, but we'll concentrate for the moment on {functions,objects}= {lvalues}. Don't forget that objects include not just the fundamental types described above, but all the derived types (excepting functions).

There are two basic steps needed to make a name available for use in a program:

declaration
letting the compiler know of the existence of an object or function with the specified name
definition
(1) of an object: telling the compiler to set aside storage for the named object and possibly initializing its value
(2) of a function: furnishing the code for the function
It is possible to declare an object or function without defining it, and, in fact, one can multiply declare a name. There must, however be one and only one definition of a name in the entire program. For the simple kind of variables we'll start off with (local, automatic), the act of declaration includes definition:
// my_prog.cc
int main()
{
int i; //declaration and definition
int j=4; // declaration and definition and initialization
}
The syntax for objects is quite easy and apparent from this example.

Scope

Does that mean that there can be only one variable named "i" in the entire program? Not quite, because a name has several attributes associated with it, one of which is its scope:
scope
the collection of regions of source code where the name can be used
The available scopes are: {local, function, file, class}. Ignoring the {function,class} scopes for the moment, here's the definition of the other two:
local scope
Defining a block to be the region enclosed by two braces {...}, a name declared within the braces is unknown outside the braces, and basically known inside the braces from the point it gets declared to the enclosing brace. If there's a block nested within the local scope, however, a name may be hidden by declaring another object (of arbitrary type) with that same name.
file scope
If a name is declared outside any block (or class), its scope begins at the point of declaration and continues to the end of the file. It is also hidden, by declaration of the same name within any block in the file, but is accessible via the :: operator
Here's an example:
// program showing scope
#include <iostream.h>

// declare/define some variables with file scope
int j=4.0; // initialization!
int i; // main() which defines its own local scope
int main()
{
i=4; // global i
int i=3; // local scope i, hides previous one
cout <<"j: "<<j<<" global i"<<::i<<" local i "<<i<<'\n';

{ // new local scope
char i= 'd';
cout <<"nested i:"<<i<<" global i:"<<::i<<'\n';
}
}

You should copy this into a file and try it out (and check out the exercises, once I get them written).

[ Phy 405/905 Home Page] [Lecturer ]