vector.cc
changeset 54 c064ce9f40f5
parent 45 7197576fedcf
child 74 829976007e62
equal deleted inserted replaced
53:6b3d7e3418c1 54:c064ce9f40f5
     8 #include <ctime>
     8 #include <ctime>
     9 
     9 
    10 #define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0))
    10 #define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0))
    11 #define MAX 6
    11 #define MAX 6
    12 
    12 
       
    13 static int refCounter = 0;
       
    14 
    13 namespace algebra {
    15 namespace algebra {
    14     class Vector {
    16     class Vector {
    15       private:
    17       private:
    16         int x;
    18         int x;
    17         int y;
    19         int y;
    18 
    20 
    19       public:
    21       public:
    20         Vector (int xx = 0, int yy = 0);
    22         Vector (int xx = 0, int yy = 0);
       
    23         Vector (const Vector &);
    21        ~Vector ();
    24        ~Vector ();
    22 
    25 
    23         int X () { return x; };
    26         int X () { return x; };
    24         int Y () { return y; };
    27         int Y () { return y; };
    25 
    28 
    31     };
    34     };
    32 
    35 
    33     Vector::Vector (int xx, int yy) {
    36     Vector::Vector (int xx, int yy) {
    34         x = xx;
    37         x = xx;
    35         y = yy;
    38         y = yy;
       
    39 
       
    40         refCounter++;
       
    41     }
       
    42 
       
    43     Vector::Vector (const Vector & copy)
       
    44     : x (copy.x), y (copy.y) {
       
    45         fprintf (stderr, "Warning Copy Constructor\n");
       
    46         refCounter++;
    36     }
    47     }
    37 
    48 
    38     Vector::~Vector () {
    49     Vector::~Vector () {
    39         fprintf (stderr, "Removing Vector (%.3d, %.3d)\n", x, y);
    50         fprintf (stderr, "Removing Vector (%.3d, %.3d)\n", x, y);
       
    51         refCounter--;
    40     }
    52     }
    41 
    53 
    42     Vector Vector::operator+ (Vector & v) {
    54     Vector Vector::operator+ (Vector & v) {
    43         return Vector (x + v.X (), y + v.Y ());
    55         return Vector (x + v.X (), y + v.Y ());
    44     }
    56     }
    58 
    70 
    59 using namespace algebra;
    71 using namespace algebra;
    60 
    72 
    61 int main (int argc, char **argv)
    73 int main (int argc, char **argv)
    62 {
    74 {
    63     Vector *v[MAX];
    75     Vector **v;
    64     Vector result;
    76     Vector result;
    65 
    77 
    66     srand (time (NULL));
    78     srand (time (NULL));
    67 
    79 
    68     if ((MAX % 2) != 0) {
    80     if ((MAX % 2) != 0) {
    69         printf ("MAX %% 2 != 0\n");
    81         printf ("MAX %% 2 != 0\n");
    70         return EXIT_FAILURE;
    82         return EXIT_FAILURE;
    71     }
    83     }
       
    84 
       
    85     v = new Vector *[MAX];
    72 
    86 
    73     for (int i = 0; i < MAX; i++) {
    87     for (int i = 0; i < MAX; i++) {
    74         v[i] = new Vector (GETRANDOM (100), GETRANDOM (100));
    88         v[i] = new Vector (GETRANDOM (100), GETRANDOM (100));
    75     }
    89     }
    76 
    90 
    94         result = *v[i + 1] - *v[i];
   108         result = *v[i + 1] - *v[i];
    95         result.vector ();
   109         result.vector ();
    96         printf (" = %3.2f\n", result.abs ());
   110         printf (" = %3.2f\n", result.abs ());
    97     }
   111     }
    98 
   112 
    99     printf ("\nCLEANUP\n");
       
   100 
       
   101     for (int i = 0; i < MAX; i++)
   113     for (int i = 0; i < MAX; i++)
   102         delete v[i];
   114         delete v[i];
   103 
   115 
       
   116     delete [] v;
       
   117 
       
   118     fprintf (stderr, "Objects remaining on exit: %d\n", refCounter);
       
   119 
   104     return EXIT_SUCCESS;
   120     return EXIT_SUCCESS;
   105 }
   121 }