Topic : C++ tutorial for C users
Author : Eric Brasseur
Page : << Previous 5  Next >>
Go to page :


<< endl;

    person a;
    cout << a.name << ", age " << a.age << endl << endl;

    person b ("John");
    cout << b.name << ", age " << b.age << endl << endl;

    b.age = 21;
    cout << b.name << ", age " << b.age << endl << endl;

    person c ("Miki", 45);
    cout << c.name << ", age " << c.age << endl << endl;

    cout << "Bye!" << endl << endl;
}


Here is a short example of an array class definition. A method that is an overload of the [] operator and that outputs a reference (&) is used in order to generate an error if it is tried to access outside the limits of an array:

#include <iostream.h>
#include <stdlib.h>

class array
{
public:
   int size;
   double *data;

   array (int s)
   {
      size = s;
      data = new double [s];
   }

   ~array ()
   {
      delete [] data;
   }

   double &operator [] (int i)
   {
      if (i < 0 || i >= size)
      {
         cerr << endl << "Out of bounds" << endl;
         exit (EXIT_FAILURE);
      }
      else return data [i];
   }
};

void main ()
{
   array t (5);  

   t[0] = 45;                       // OK
   t[4] = t[0] + 6;                 // OK
   cout << t[4] << endl;            // OK

   t[10] = 7;                       // error!
}



17
If you cast an object like a vector, everything will happen all right. For example if vector k contains (4, 7), after the cast m = k the vector m will contain (4, 7) too. Now suppose you're playing with objects like the person class above. If you cast such person object p, r by writing p = r it is necesary that some function does the necessary work to make p be a correct copy of r. Otherwise the result will be catastrophic; a mess of pointers and lost data. The method that will do that job is the COPY CONSTRUCTOR:

#include <iostream.h>
#include <string.h>

class person
{
public:

   char *name;
   int age;

   person (char *n = "no name", int a = 0)
   {
      name = new char[100];
      strcpy (name, n);
      age = a;
   }

   person (person &s)               // The COPY CONSTRUCTOR
   {
      strcpy (name, s.name);
      age = s.age;
   }

   ~person ()
   {
      delete [] name;
   }
};

void main ()
{
   person p;
   cout << p.name << ", age " << p.age << endl << endl;

   person k ("John", 56);
   cout << k.name << ", age " << k.age << endl << endl;

   p = k;
   cout << p.name << ", age " << p.age << endl << endl;

   p = person ("Bob", 10);
   cout << p.name << ", age " << p.age << endl << endl;
}



The copy constructor also allows your program to make copies of instances when doing calculations. It is a key method. During calculations instances are created to hold intermediate results. They are modified, casted and destroyed without you being aware.
In all the examples above the methods are defined inside the class definition. That makes them automatically be inline methods.

18
If a method cannot be inline, if you do not want it to be inline or if you want the class definition contain the minimum of information, then you must just put the prototype of the method inside the class and define the method below the class:


#include <iostream.h>

class vector
{
public:

   double x;
   double y;

   double surface();         // The ; and no {} shows it is a prototype
};

double vector::surface()
{
   double s = 0;

   for (double i = 0; i < x; i++)
   {
      s = s + y;
   }

   return s;
}

void main ()
{
   vector k;

   k.x = 4;
   k.y = 5;

   cout << "Surface: " << k.surface() << endl;
}


19
When a method is applied to an instance, that method may use the instance's variables, modify them... But sometimes it is necessary to know the address of the instance. No problem, the keyword "this" is intended therefore:

#include <iostream.h>
#include <math.h>

class vector
{
public:

   double x;
   double y;

   vector (double a = 0, double b = 0)
   {
      x = a;
      y = b;
   }

   double module()
   {
      return sqrt (x * x + y * y);
   }

   void set_length (double a = 1)
   {
      double length;

      length = this->module();

      x = x / length * a;
      y = y / length * a;
   }
};

void main ()
{
   vector c (3, 5);

   cout << "The module of vector c: " << c.module() << endl;

   c.set_length(2);            // Transforms c in a vector of size 2.

   cout << "The module of vector c: " << c.module() << endl;

   c.set_length();             // Transforms b in an unitary vector.

   cout << "The module of vector c: " << c.module() << endl;
}


20
Of course it is possible to declare arrays of objects:

#include <iostream.h>
#include <math.h>

class vector
{
public:

   double x;
   double y;

   vector (double a = 0, double b = 0)
   {
      x = a;
      y = b;
   }

   double module ()
   {
      return sqrt (x * x + y * y);
   }
};

void main ()
{
   vector s[1000];

   vector t[3] = {vector(4, 5), vector(5, 5), vector(2, 4)};

   s[23] = t[2];

   cout << t[0].module() << endl;
}


21
Here is an example of a full class declaration:

#include <iostream.h>
#include <math.h>

class vector
{
public:

   double x;
   double y;

   vector (double = 0, double = 0);

   vector operator + (vector);
   vector operator - (vector);
   vector operator - ();
   vector operator * (double a);
   double module();
   void set_length (double = 1);
};

vector::vector (double a, double b)
{
   x = a;
   y = b;
}

vector vector::operator + (vector a)
{
   return vector (x + a.x, y + a.y);
}

vector vector::operator - (vector a)
{
   return vector (x - a.x, y - a.y);
}

vector vector::operator - ()
{
   return vector (-x, -y);
}

vector vector::operator * (double a)
{
   return vector (x * a, y * a);
}

double vector::module()
{
   return sqrt (x * x + y * y);
}

void vector::set_length (double a)
{
   double length = this->module();

   x = x / length * a;
   y = y / length * a;
}

ostream& operator << (ostream& o, vector a)
{
   o << "(" << a.x << ", " << a.y << ")";
   return o;
}

void main ()
{
   vector a;
   vector b;
   vector c (3, 5);

   a = c * 3;
   a = b + c;
   c = b - c


Page : << Previous 5  Next >>