First Steps: Typing in C++

Overview

Typing in C++. Using header files.

The Lesson

Run ROOT 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. Yet another 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.

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:-

.print num which should produce:- (Int_t)3 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 header file iostream.h. 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 by typing:-
  #include <xxxxxx.h>

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:-

rootlogon.C 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:-
{
  G__loadfile("xxxxxx.h");
}
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:- Rint.Logon: rootlogon.C Alternatively, and probably better, as ROOT looks in your home directory as well put .rootrc and rootlogon.C there and make the entry:- Rint.Logon: ~/rootlogon.C Now you don't have to worry if you switch working directories. See 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 for and if

Summary


Go Back to the The First Steps Top Page


If you have any comments about this page please send them to Nick West