Setting up your first class

The division I did already in the simple examplatory class Ratio can be followed more closely in the first "realistic" case of an n-dimensional vector class. There, the class definition with the methods can be found in an ["vecNd.H"-file], the realization in the ["vecNd.C"-file], and some test routines are to be found in ["Main.C"-file]. To compile and run the code, please use the ["Makefile"].
There are a couple of things to be noted and explained. Let us start with the H-file:
  • The full file is surrounded by the following lines:
    #ifndefine vecNd_h
    #define vecNd_h
    body of the file
    #endif

    The idea is that wherever in the code you need the class vecNd, wherevere you wish to instantiate such an object, you have to include the vecNd.H file to define this particular class and to make all methods etc. known to the code.
    However, C++ copes really bad with multiple definitions of one and the same object. You are allowed to definee the full class only once. This is, what the syntax above does. Wherever the vecNd.H file is called via
    #include "vecNd.H"

    it is checked wether a marker vecNd_h has been defined already. If this is not the case, it will be defined, and the rest of the class definition will be done.
  • The class vecNd includes the Standard library math.h to have access to functions like squareroot (sqrt) and the like. It also includes the iostream.h. The main syntax difference between files from the Standard library and your own class definitions is exemplified below:
    #include < math.h>
    #include "vecNd.H"

  • You see two constructors, both named vecNd, bit with different arguments. Constructors (as well as destructors, see below) must have the class name as name. However, the two construcotrs here are either with parameters or with another vecNd (passed by reference) as argument. The latter is called "copy constructor" for obvious reasons, check the C-file.
  • There's a thing called destructor, denoted by ~vecNd(). If you check in the C-file, you will see that this destructor deletes the memebers of the vector array. Note that this is urgently needed, whenever you allocate memory within your constructor, i.e. whenever you use the command "new". The reason is that all classes are defined to some extend within the framework of a method, a class, a function etc. . Whenever the flow of the executable program leaves this framework all intrinsic variables are deleted automatically. However, for arrays allocated via "new" this does not hold true and you have to delete them "by hand". Copnsequently, you have to define a specific "delete method" for classes that incorporate such arrays. This method is called destructor.
  • You will see that some methods are realized within the H-file and with the prefix inline . Of course, the realization is outside the class definition. The keyword inline has the effect that the corresponding executable code is included withinhg the larger executable instead of some look-up within the class definition plus additional links. This is for reasons of efficiency. Basically, inlines should be short, to ensure that the executable chunk of code related to them are shorter than the lookup procedure. Let me stress that inlines have to be in H-files.

    [prev] [top]