< previous page page_237 next page >

Page 237
LISTING 15.5 CREATING AN ARRAY BY USINGnew

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
 1:    // Listing 15.5 - An array on the free store
 2:
 3:    #include <iostream.h>
 4:
 5:    class CAT
 6:    {
 7:    public:
 8:       CAT() { itsAge = 1; itsWeight=5; }  // default constructor
 9:       ~CAT();                                      // destructor
10:       int GetAge() const { return itsAge; }
11:       int GetWeight() const { return itsWeight; }
12:       void SetAge (int age) { itsAge = age; }
13:
14:   private:
15:      int itsAge;
16:      int itsWeight;
17:   };
18:
19:   CAT :: ~CAT()
20:   {
21:      // cout << Destructor called!\n;
22:   }
23:
24:   int main()
25:   {
26:      CAT * Family = new CAT[500];
27:      int i;
28:      CAT * pCat;
29:      for (i = 0; i < 500; i++)
30:      {
31:         pCat = new CAT;
32:         pCat->SetAge(2*i +1);
33:         Family[i] = *pCat;
34:         delete pCat;
35:      }
36:
37:      for (i = 0; i < 500; i++)
38:         cout << Cat # << i+1 << :  << Family[i].GetAge() << endl;
39:
40:      delete [] Family;
41:
42:      return 0;
43:   }

Output:
Cat #1: 1
Cat #2: 3
Cat #3: 5

 
< previous page page_237 next page >

If you like this book, buy it!