# Adding a class to ROOT

You can extend ROOT with your own classes. When defining a class, it inherits from TObject .

## Defining a class

The definition of a class requires the following steps:

• Inheriting from TObject .

• Integrating the class to ROOT.

• Calling the ClassImp macro.

• Providing a constructor.

### Inheriting from TObject

The TObject class provides the default behaviour and protocols for the objects in the ROOT system. The TObject class is the primary interface to classes providing object I/O (writing the class into a ROOT file), error handling, inspection, introspection, and drawing.

Therefore, your class should inherit from TObject .

### Integrating the class to ROOT

The ClassVersionID is used by the ROOT I/O system. It is written on the output stream and during reading. You can check this ClassVersionID during reading and take appropriate action depending on the value of ClassVersionID. Every time you change the data members of a class, increase its ClassVersionID by 1.
Set ClassVersionID >= 1.
Set ClassVersionID = 0 in case you do not need object I/O.

Example

In the TLine.h file:

### Calling the ClassImp macro

Call the ClassImp macro to give your class run time type identification (RTTI) and full I/O capabilities. In addition, you can generate properly documentation for your class using THtml .

Example

In the Tline.cxx file:

### Constructors

ROOT requires for every class to have one of the following constructors:

• Default constructor
A constructor with zero parameters or with one or more parameters all with default values.

• I/O constructor
A constructor with exactly one parameter which type is a pointer to one of the types marked as an ioctortype.

The default constructor or I/O constructor is called whenever an object is being read from a ROOT database.

Example for a class