C++ Syntax: class

Description

The class statement introduces a new class. Classes effectively are user defined data types that extend the C++ language. These data types can be used with the same syntax as built-in types. They can be used to define new data types (e.g. pointer to class, array of class etc.), in expressions and passed and returned from functions.

The syntax of the class statement will be illustrated using the Track class that is used as the model in most of the OO topics in this crib. A description of a class is usually organised into 2 files: a header file and an implementation file, Track.h and Track.cxx in this case.

Track.h

Header files contain sufficient information to allow the compiler to create class objects and to send them messages. If you want to use objects of a class, you have to include its header.

The Track class has the following (incomplete) header:-

class Track : public TObject { 

public:

// Constructors and Destructors
  Track::Track();
  Track(Float_t mass, Float_t energy =0.);
  ~Track();

// Getters and Setters
  Float_t GetEnergy() {
    return fEnergy;
  }
  void  Track::SetEnergy( Float_t energy ) {
    fEnergy = energy;
    fMomentum = sqrt( fEnergy*fEnergy - fMass*fMass );
  }

// Operations
virtual void Propagate() = 0;

private:

// Data
  Float_t fEnergy;
  Float_t fMass;
  Float_t fMomentum;

};
These statements are analysed in the table below:-

class Track
Declare Track class. For a basic discussion of a class see the OO topic Objects & Classes.
             : public TObject
This section is optional. The colon is followed by a comma separated list of classes from which the Track class inherits. The access specifier, public in this case, sets a lower level of security on the embedded TObject members. See OO topic Inheritance
                                {  
The opening curly brace of the compound statement that defines the class.

public:
This access specifier means that the following are visible to users of the class. Its normal to start with the public section - its the part that user's of an object need to know about. See the OO topic Private & Public.

// Constructors and Destructors
These member functions are used to create and destroy objects. See the OO topic Constructors & Destructors.

  Track::Track();
This is the default constructor i.e. the one to be used if no arguments are available. Every class has to have a default constructor. The body of this function is held in Track.cxx.

  Track(Float_t mass, Float_t energy =0.);
This constructor will be called when a two argument constructor is required. It can also be used when only a one argument constructor is required and will then have energy set to zero. The body of this function is held in Track.cxx. Track has two constructors, a situation that is discussed in the OO topic Overloading.

  ~Track();
This is the Track destructor. Destructors have an important role to play when an object creates further objects during its lifetime. The body of this function is held in Track.cxx. See the OO topic Constructors & Destructors.

// Getters and Setters
Getters and setters allow a class designer to allow controlled access to parts of the object.

  
  Float_t GetEnergy() {
    return fEnergy;
  }
The complete function is supplied in the header. This is often the case with simple functions and allows the compiler to replace function calls with in- line code. See OO topic Private & Public.

  void  Track::SetEnergy( Float_t energy ) { 
    fEnergy = energy;
    fMomentum 
     = sqrt( fEnergy*fEnergy - fMass*fMass );  
  }
Again the compiler can in-line the code. This also demonstrates how Setters can be used to ensure the object remains internally self-consistent. See the OO topic: Objects & Classes.

// Operations
Here is where the more substantial member functions are defined.

virtual                       
virtual means that calls to this function go via a virtual function address table so that, when embedded in another object, that object can intercept calls to this function. See OO topic Virtual Functions & Polymorphism.
        void Propagate()      
                          = 0;
This means that the function has no body. It must be supplied by an inheriting class before objects can be created. See OO topic Interfaces & Abstract Classes.

private:
From now on, all members are private and are not visible by users of the object or classes that inherit from this class. The alternative: protected would allow inheriting classes access but still deny it to users. See OO topic Private & Public.

// Data
  Float_t fEnergy;
  Float_t fMass;
  Float_t fMomentum;
The object's data members. Object encapsulation dictates that all data members should be private.

};
The end of the compound statement that defines the class. It is followed by a semi-colon which is needed because it is possible, if unusual, to define a class and a object of that class in a single statement:-
class MyClass {...} MyObject;

Track.cxx

Implementation files contain the function definitions i.e. complete with the function body, as compared with header files that only contain their declarations i.e. the names and the types of the function arguments and return values. The one exception, as explained above, is that very simple functions may be supplied in full in the header to allow the compiler to in-line them to improve performance.

The Track class has the following (incomplete) implementation file:-

#include "Track.h"

   Track::Track(): fMass(0.), fMomentum(0.), fEnergy(0.) {}

   Track::Track(Float_t mass, Float_t energy): fMass(mass) {
    SetEnergy(energy);
  }

   Track::~Track() {};
These statements are analysed in the table below:-

#include "Track.h"
The class header file.

   Track::                                                 
:: is the scoping operator, it means that what follows belongs to the Track class. See the Headers and Implementation Files section of the OO topic Constructors & Destructors.
          Track()                                          
This is the default (empty argument list) constructor. See the Default Constructor section of the OO topic Constructors & Destructors.
                 : fMass(0.), fMomentum(0.), fEnergy(0.)   
This section is optional. The colon is followed by a comma separated member initialiser list. See the Member Initialisation section of the OO topic Constructors & Destructors.
                                                         {}
An empty function body. As fMass, fMomentum, fEnergy have all been set to zero by the member initialiser list, there is nothing left to do!

   Track::Track(Float_t mass, Float_t energy)               
A second Track constructor. In the header the second argument is shown as optional: Float_t energy =0. This must not be repeated here, even if it is consistent; the default value can only be supplied once.
                                             : fMass(mass)  
Initialise fMass to the value mass.
                                                           {
The opening curly brace of the compound statement that is the function body.

    SetEnergy(energy);
Call the object's SetEnergy member function. Internal calls like this are discussed in the Preparing an Object for Action section of the OO topic Objects & Classes.

  }
The closing curly brace of the function body.

  Track::~Track() {};
The destructor. In this case it has nothing to do. See the OO topic Constructors & Destructors.


Go Back to the The C++ Crib Top Page


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