Next: Pointers Up: New Concepts Previous: New Concepts

New Data Types

In FORTRAN we are accustomed to having a few well defined data types such as INTEGER, REAL, DOUBLE PRECISION etc. Those data types exist in C/C++ also(although their names are slightly different). What C++ has added to the language which you are not accustomed to is the ability to create your own data types. This is quite important. It means that you can create a data type called ERP or a data type called QTP.

Now you are probably saying to yourself: ``If I were to create a variable type called ERP it wouldn't be at all like an INTEGER. After all integers have only one value, and ERPs have lots of ADCs and energies, etc., associated with them.'' Well you are exactly right. When you create a new data type it is usually a structure whose pieces are made out of predefined data types. The technical term for such a new data type in C++ is a class. Let us give you an example. Let us define a new data type called erp. It should contain ADCs and an energy. Don't worry exactly about the syntax here; we will explain it in a bit.



class erp  {

public:

  short adc0u;                  // ADC 0 side Unattenuated
  short adc1u;                  // ADC 1 side Unattenuated
                                // Note: short is the same as INTEGER*2

  float energy;                 // ERP Energy
                                // Note: float is the same as REAL
}

Those of you familiar with C programming might recognize this as a structure. So what we have done is to define a data type called erp with three data members: adc0u, adc1u, and energy. The two ADCs are of type short(equivalent to INTEGER*2 in FORTRAN) and the energy is of type float(equivalent to type REAL in FORTRAN). In FORTRAN to define a variable as type INTEGER we would write:


INTEGER INT_VARIABLE

In C++ to define a variable as being of type erp we would write:


erp erp_variable;

The main point here is that the erp type variable is just as valid a variable type as any built-in variable type. Now what if we want to access the ADCs and energy of an erp variable? In C/C++ the way we do this is to write variableName.dataMember. Here is a quick example which should make it clear.



erp erpBox;             //Make a erp variable called erpBox
erpBox.adc0u =  6;      //Set adc0u = 6
erpBox.energy = 10.0;   //Set the energy to 10 Mev.

float energy;           //make an float variable called energy
energy = erpBox.energy; //Set energy = 10 Mev(the value of erpBox.energy)

So as you can see we have done a very useful thing. We have created new variable types which know about our data! This solves the problem in ZEBRA of needing to know offsets and data types. You don't need to know that adc0u is the 5th word, or that the adc0u is a short. If you want the adc you just type erpBox.adc0u.

In fact, this is a main idea of FARFALLA. We have created several data types which describe MACRO data. You can create your own types and use them to make your own custom DSTs.

One thing that we should mention briefly is that in C++ the members of a class don't all have to be data(like adc0u). The members can also be functions. This is an important distinction between C and C++ . They are called data members and member functions respectively. Usually members function perform some sort of operation on the data members of the class. We will give an example of member functions later.

There is a lot more that you can do with classes. They are the heart of what makes C++ so powerful. We will point out some of them in this document. We mention more in the FARFALLA Programming Reference Guide, and a C++ book will tell you even more.



Next: Pointers Up: New Concepts Previous: New Concepts


walter@
Wed Aug 10 11:42:05 PDT 1994