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


+ a + (b - a) * 7;
   c = -c;

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

   cout << "The content of vector a: " << a << endl;
   cout << "The oposite of vector a: " << -a << endl;

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

   a = vector (56, -3);
   b = vector (7, c.y);

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

   cout << "The content of vector b: " << b << endl;

   double k;
   k = vector(1, 1).module();  // k will contain 1.4142.
   cout << "k contains: " << k << endl;
}



It is also possible to define the sum of vectors without mentioning it inside the vector class definition. Then it will not be a method of the class vector. Just a function that uses vectors:

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



In the example above of a full class definition, the multiplication of a vector by a double is defined. Suppose we want the multiplication of a double by a vector be defined too. Then we must write an isolated function outside the class:


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



Of course the keywords new and delete work for class instances too. What's more, new automatically calls the constructor in order to initialize the objects, and delete automatically calls the destructor before deallocating the zone of memory the instance variables take:


#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);
   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 (a * x, a * y);
}

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

void vector::set_length (double a)
{
   vector &the_vector = *this;

   double length = the_vector.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 c (3, 5);

   vector *r;                  // r is a pointer to a vector.

   r = new vector;             // new allocates the memory necessary
   cout << *r << endl;         // to hold a vectors' variable,
                               // calls the constructor who will
                               // initialize it to 0, 0. Then finally
                               // new returns the address of the vector.

   r->x = 94;
   r->y = 345;
   cout << *r << endl;

   *r = vector (94, 343);
   cout << *r << endl;

   *r = *r - c;
   r->set_length(3);
   cout << *r << endl;

   *r = (-c * 3  +  -*r * 4) * 5;
   cout << *r << endl;

   delete r;                   // Calls the vector destructor then
                               // frees the memory.

   r = &c;                     // r points towards vector c
   cout << *r << endl;

   r = new vector (78, 345);   // Creates a new vector.
   cout << *r << endl;         // The constructor will initialise
                               // the vector's x and y at 78 and 345

   cout << "x component of r: " << r->x << endl;
   cout << "x component of r: " << (*r).x << endl;

   delete r;

   r = new vector[4];          // creates an array of 4 vectors

   r[3] = vector (4, 5);
   cout << r[3].module() << endl;

   delete [] r;                // deletes the array

   int n = 5;
   r = new vector[n];          // Cute!

   r[1] = vector (432, 3);
   cout << r[1] << endl;

   delete [] r;

}


22
A class' variable can be declared static. Then only one instance of that variable exists, shared by all instances of the class:


#include <iostream.h>

class vector
{
public:

   double x;
   double y;
   static int count;

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

   ~vector()
   {
      count = count - 1;
   }
};

void main ()
{
   vector::count = 0;

   cout << "Number of vectors:" << endl;

   vector a;
   cout << vector::count << endl;

   vector b;
   cout << vector::count  << endl;

   vector *r, *u;

   r = new vector;
   cout << vector::count << endl;

   u = new vector;
   cout << a.count << endl;

   delete r;
   cout << vector::count << endl;

   delete u;
   cout << b.count << endl;
}


23
A class variable can also be constant. That's just like static, except it is alocated a value inside the class declaration and that value may not be modified:



#include <iostream.h>

class vector
{
public:

   double x;
   double y;
   const double pi = 3.1415927;

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

   double cilinder_volume ()
   {
      return x * x / 4 * pi * y;
   }
};

void main(void)
{
   cout << "The value of pi: " << vector::pi << endl << endl;

   vector k (3, 4);

   cout << "Result: " << k.cilinder_volume() << endl;
}


24
A class can be DERIVED from another class. The new class INHERITS the variables and methods of the BASE CLASS. Additional variables and/or methods can be added:

#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);
   }

  


Page : << Previous 6  Next >>