Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - static class members: constructors http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   Static Class Members: Constructors
   A  constructor is a special class member function which  provides  for
   the  initialization of class objects. It is a user  supplied  function
   which is distinguished by having the same name as the class itself. If
   a  constructor  is supplied for a class, the compiler will  invoke  it
   automatically whenever a class object is created, either by definition
   or through the new operator.

   A  constructor  may not specify a return type or explicitly  return  a
   value. In all other respects a constructor behaves as a normal  member
   function. In particular, constructors can have their names overloaded.
   This allows multiple constructors to be defined to so that a class can
   be initialized in  a number of different ways.

   A  constructor  does  not allocate space for the  class  object  being
   created;  this  is done by the compiler in the same way as  for  basic
   data  types. A constructor merely initializes this allocated space.  A
   constructor may, however, dynamically allocate space for assignment to
   pointers  within  the  object,  and this is  in  fact  a  very  common
   situation.

   The following class definition declares several constructors:
   // Vector - a bounds checked array type

   class Vector {
   public:
   Vector();
   // create a array with a default size
   Vector(int n);
   // create a size n array
   Vector(vector& v);
   // initialization with a Vector
   Vector(int a[], int n);
   // initialization by array
   private:
   int* p;
   // base pointer
   int size;
   // number of elements
   };
   The first constructor takes no arguments, and will use a default value
   which  is contained in the function definition. This constructor  will
   act as a default constructor when arrays of vectors are being defined;
   see the section on Arrays of Class Objects.

   The definitions of the first two constructors as they would appear  in
   the C++ source file are:

   #include <stream.hpp>
   #include "vector.hpp"
   // contains class definition for Vector

   // a constructor with no arguments
   // (a default constructor for arrays)

   Vector::Vector(void)
   {

   size = 15;
   // default vector size
   p = new int[size];
   // uses new to allocate
   // space from the heap
   }

   // a constructor that takes a size argument

   Vector::Vector(int n)
   {
   // if the size is negative use stream i/o to
   // print an error message to stderr and exit

   if (n < 0)
   {
   cerr << "Illegal Vector size: " << n << "\n";
   exit(1);
   }

   // Ok, allocate room for the array from the heap

   size = n;
   p = new int[size];
   }

   Note  that  both these constructors allocate memory from the  heap  to
   store the data elements for the array member of the Vector class.

   The  class  constructor is called whenever an object of the  class  is
   created. This guarantees that the required initialization is done. The
   following   class  object  definitions  will  result  in   the   first
   constructor being called (that which takes no arguments):

   Vector a, b;
   // creates two Vectors of 15 elements
   Vector *c = new Vector;
   // creates a pointer to Vector and allocates a
   // Vector object from the heap for it to point to
   Vector d[5];
   // creates an array of 5 vectors
   // each with 15 elements
   The following definitions will result in the second constructor  being
   called and demonstrate the ways in which the required argument can  be
   passed:

   // create a Vector of 10 elements

   Vector a = Vector(10);
   Vector b(10);
   Vector b = 10;
   Vector *c = new Vector(10);
   // must use this with new


Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson