DATA MEMBERS

Every class contains the data members. Here is an example:
class Window {
       int X1, Y1, X2, Y2;
       char *name;
       Window *owner;
};
This class contains four integer members which stores coordinates of upper left and lower right corner of window, pointer to the name of a window and pointer to another object of a class Window. That pointer points to the object Window which is owner of observed window (i.e. “it takes care of creating and destructing current window).
All types are allowed during the declaration of a class, but they need to be declared to that point in the program. What this means is that you cannot declare the part of class an object of a class which is currently defining, for example:
class Window {
       int X1, Y1, X2, Y2;
       char *name;
       Window owner; // This is incorrect !!!
};
This recursive declaration defines the class Window which contains object of class Window, which contains object of class Window etc. Obviously this definition is prolonging to infinity so that object couldn’t be defined in final computer memory. Class can contain pointer of reference on element of the same class just like in first example.
IMPORTANT !!!
The class cannot contain itself but it can contain pointer or reference on object of a same class.
Sometimes it is necessary to construct a class which will contain the pointer on another class, while the other class will contain the pointer on the first class. Using the previous example we can create the pointer which will point to another class. As you see we had a class Window and in this class Window we will create the pointer which will point to class Menu but in point Menu we will create the pointer that will point to class Window.
class Window {
       /*this is the declaration of a class called Window*/
       int X1, Y1, X2, Y2;
       char *name;
       Menu *pointerMenu; /*a reference to a declaration in advance*/
};

class Menu {
       /*this is complete definition of class called Window in which predefined name of the class are added details of the class*/
       Window *poninterWindow;
};
In order to class Menu would work properly, it needs to contain the pointer to class Window which possess the pointer to class Menu. As you can see from the code the class is defined just with the name Menu before we even type the structure of the class Window and a Menu. That kind of declaration is called forward declaration.
When we use forward declaration we can only define references and pointers on objects of this class, but not the objects of that class. Of course, in program that follows after complete definition of the class it can be defined the objects of that class. That means if we try to declare the object Menu inside the class Window before the declaration of the class Menu the program will cause an error.

So in order the define to classes with pointers to each other we need to declare the second class before defining and developing structure of first and second class. 
Share on Google Plus