OOPc using C++

With this blog I want to share my knowledge of OOPs and C++. I Hope this can quench your thirst for learning.Any suggestions and comments are welcome.

ARRAYS, POINTERS, AND REFERENCES

Arrays of objects

Objects are variables and have the same capabilities and attributes as any other type of variables. Therefore, it is perfectly acceptable for objects to be arrayed. The syntax for declaring an array of objects is exactly as that used to declare an array of any other type of variable. Further, arrays of objects are accessed just like arrays of other types of variables.

#include < iostream >
using namespace std;
class samp
 {
               int a;
         public:
              void set_a(int n) {a = n;}
              int get_a( ) { return a; }
};


int main( ) 
{
          samp ob[4];            //array of 4 objects
          int i;
          
         for (i=0; i<4; i++) ob[i].set_a(i);
         for (i=0; i<4; i++) cout << ob[i].get_a( ) << " ";
         cout << "\n";
return 0;
}


If the class type include a constructor, an array of objects can be initialised,


// Initialise an array
#include < iostream >
using namespace std;
class samp 
{
        int a;
        public:
        samp(int n) {a = n; }
         int get_a( ) { return a; }
};


int main( ) 
{
        samp ob[4] = {-1, -2, -3, -4};
        int i;
        for (i=0; i<4; i++) cout << ob[i].get_a( ) << " ";
        cout << "\n"
return 0;
}



You can also have multidimensional arrays of objects. Here an example
,




// Create a two-dimensional array of objects
// ...
class samp
{
         int a;
         public:
         samp(int n) {a = n; }
         int get_a( ) { return a; }
};

int main( )
{
      samp ob[4][2] = {
                                   1, 2,
                                   3, 4,
                                   5, 6,
                                   7, 8 };
     int i;

     for (i=0; i<4; i++)
     {
          cout << ob[i][0].get_a( ) << " ";
          cout << ob[i][1].get_a( ) << "\n";
       }
     cout << "\n";
return 0;
}

This program displays,
1 2
3 4
5 6
7 8
When a constructor uses more than one argument, you must use the alternative format,


// ...
class samp
{
            int a, b;
      public:
          samp(int n, int m) {a = n; b = m; }
          int get_a( ) { return a; }
          int get_b( ) { return b; }
};

int main( )
{
          samp ob[4][2] = {
                                        samp(1, 2), samp(3, 4),
                                        samp(5, 6), samp(7, 8),
                                        samp(9, 10), samp(11, 12),
                                        samp(13, 14), samp(15, 16)
                                       };
// ...
Note you can always the long form of initialisation even if the object takes only one argument. It is just that the short form is more convenient in this case.


Using pointers to objects


As you know, when a pointer is used, the object’s members are referenced using the arrow (- >) operator instead of the dot (.) operator.
Pointer arithmetic using an object pointer is the same as it is for any other data type: it is performed relative to the type of the object. For example, when an object pointer is incremented, it points to the next object. When an object pointer is decremented, it points to the previous object.
// Pointer to objects
// ...
class samp {
int a, b;
public:
samp(int n, int m) {a = n; b = m; }
int get_a( ) { return a; }
int get_b( ) { return b; }
};
int main( ) {
samp ob[4] = {
samp(1, 2),
samp(3, 4),
samp(5, 6),
samp(7, 8)
};
int i;
samp *p;
p = ob; // get starting address of array
for (i=0; i<4; i++) {
cout << p->get_a( ) << " ";
cout << p->get_b( ) << "\n";
p++; // advance to next object
}
// ...