First Steps: Typing in C++
Typing in C++. Using header files.
and when you have a command prompt enter:-
Int_t num = 3;
This defines a 4-byte integer called num and initialises it with the value
3. In FORTRAN you would need two statements: INTEGER and DATA.
Another difference compared with FORTRAN is that everything is
case sensitive! The type Int_t is not the same as int_t, and the
variable num is not the same as Num.
difference compared with FORTRAN is that new variables can be defined
throughout the code, not just before the first executable statement.
Its worth mentioning a couple of important
points about programming style here:-
These points aim to localise temporary variables, and minimise the
chance that they hold undefined data; both good sources of bugs.
- Define variables as late as possible in the code; don't put them
all at the top.
- If possible initialise them as you define them.
Having defined a variable, the next thing to do is to print it out. You
can do that with the CINT Command .print, for example:-
which should produce:-
showing the type and value of num. But how do you do it in C++? If
you already know a little C++, or have been browsing this Companion,
then you might try something like:-
cout << "num is " << num << endl;
which should result in:-
num is 3
At one time this would have failed; you would have been told that cout
had not been defined. This is
another example of the way C++ differs from FORTRAN. In FORTRAN the I/O
system is built-in to the language, in C++ the I/O is an add-on,
although standardised, part of the language .Before you can use it you
must declare it using the
To save you time ROOT
has a number of standard headers preloaded and this includes iostream.h,
as well as other popular ones such as:-
So for now, you don't have a problem, but if you find you need a header
that is not preloaded, say xxxxxx.h then you can load it
- string.h - for character string handling
- time.h - for basic time operations
- math.h - for standard maths functions such as sin and sqrt
It can get a bit tedious having to remember to include headers like this
each time you run ROOT. Fortunately there is a file, called:-
that, if present, is processed when ROOT starts. If you know you will
want particular headers then simply create this file in the current
directory containing something like this:-
If that does not work then you probably don't have a
.rootrc file set up. Create one in the current directory containing
the single line:-
Alternatively, and probably better, as ROOT looks in your home directory
as well put .rootrc and rootlogon.C there and make the entry:-
Now you don't have to worry if you switch working directories.
Starting an Interactive Session
for further information about .rootrc and rootlogon.C
You now have enough to try typing in a few simple C++ statements.
Look at the C++ Syntax section of the
The C++ Crib.
At this stage avoid control statements such as
- C++ statements can be typed directly into ROOT.
- Before using those parts of the language that are not built-in,
include the appropriate header file. For example, before doing I/O to
the terminal using cin and cout, include:-
Add this to rootlogon.C if you want to load the header every time ROOT
- The CINT Command .print can be used to examine the type and value
of a variable.
Go Back to the
The First Steps Top Page
If you have any comments about this page please send them to