C++ Tutorial/Class/object array

Материал из C\C++ эксперт
Версия от 10:29, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Allocate an array of objects using new operator

#include <iostream> 
#include <new> 
using namespace std; 
 
class Rectangle { 
  int width; 
  int height; 
public: 
  Rectangle() {  
    width = height = 0; 
    cout << "Constructing " << width << " by " << height << " rectangle.\n"; 
  } 
 
  Rectangle(int w, int h) { 
    width = w; 
    height = h; 
    cout << "Constructing " << width << " by " << height << " rectangle.\n"; 
  } 
 
  ~Rectangle() {  
     cout << "Destructing " << width << " by " << height << " rectangle.\n"; 
  }  
 
  void set(int w, int h) { 
    width = w; 
    height = h; 
  } 
 
  int area() { 
    return width * height; 
  } 
}; 
 
int main() 
{ 
  Rectangle *p; 
 
  try { 
    p = new Rectangle [3]; 
  } catch (bad_alloc xa) { 
    cout << "Allocation Failure\n"; 
    return 1; 
  } 
 
  cout << "\n"; 
 
  p[0].set(3, 4); 
  p[1].set(10, 8); 
  p[2].set(5, 6); 
 
  for(int i=0; i < 3; i++) 
    cout << "Area is " << p[i].area() << endl; 
 
  delete [] p; 
 
  return 0; 
}
Constructing 0 by 0 rectangle.
Constructing 0 by 0 rectangle.
Constructing 0 by 0 rectangle.
Area is 12
Area is 80
Area is 30
Destructing 5 by 6 rectangle.
Destructing 10 by 8 rectangle.
Destructing 3 by 4 rectangle.

allocates and frees an object and an array of objects of type loc.

#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
   
class loc {
  int longitude, latitude;
public:
  loc() {longitude = latitude = 0;}
  loc(int lg, int lt) {
    longitude = lg;
    latitude = lt;
  }
   
  void show() {
    cout << longitude << " ";
    cout << latitude << "\n";
  }
   
  void *operator new(size_t size);
  void operator delete(void *p);
   
  void *operator new[](size_t size);
  void operator delete[](void *p);
};
   
// new overloaded relative to loc.
void *loc::operator new(size_t size)
{
void *p;
   
  cout << "In overloaded new.\n";
  p =  malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}
   
// delete overloaded relative to loc.
void loc::operator delete(void *p)
{
  cout << "In overloaded delete.\n";
  free(p);
}
   
// new overloaded for loc arrays.
void *loc::operator new[](size_t size)
{
  void *p;
   
  cout << "Using overload new[].\n";
  p =  malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}
   
// delete overloaded for loc arrays.
void loc::operator delete[](void *p)
{
  cout << "Freeing array using overloaded delete[]\n";
  free(p);
}
   
int main()
{
  loc *p1, *p2;
  int i;
   
  try {
    p1 = new loc (10, 20); // allocate an object
  } catch (bad_alloc xa) {
    cout << "Allocation error for p1.\n";
    return 1;;
  }
   
  try {
    p2 = new loc [10]; // allocate an array
  } catch (bad_alloc xa) {
    cout << "Allocation error for p2.\n";
    return 1;;
  }
   
  p1->show();
 
  for(i=0; i<10; i++)
    p2[i].show();
   
  delete p1;    // free an object
  delete [] p2; // free an array
   
  return 0;
}

An array of objects: call its method

#include <iostream>
 
 class MyClass
 {
 public:
     MyClass() { 
        itsAge = 1; 
        itsWeight=5; 
     } 
     ~MyClass() {}                          
     int GetAge() const { 
         return itsAge; 
     }
     int GetWeight() const { 
        return itsWeight; 
     }
     void SetAge(int age) { 
        itsAge = age;
     }
 
 private:
     int itsAge;
     int itsWeight;
 };
 
 int main()
 {
     MyClass myObject[5];
     int i;
     for (i = 0; i < 5; i++)
         myObject[i].SetAge(2*i +1);
 
     for (i = 0; i < 5; i++)
         std::cout << " #" << i+1<< ": " << myObject[i].GetAge() << std::endl;
     return 0;
 }
#1: 1
 #2: 3
 #3: 5
 #4: 7
 #5: 9

An array of pointers to objects

#include <iostream>
 
 class MyClass
 {
 public:
     MyClass() { 
        itsAge = 1; 
        itsWeight=5; 
     } 
     ~MyClass() {}                          
     int GetAge() const { return itsAge; }
     int GetWeight() const { return itsWeight; }
     void SetAge(int age) { itsAge = age; }
 
 private:
     int itsAge;
     int itsWeight;
 };
 
 int main()
 {
     MyClass * myObject[50];
     int i;
     MyClass * objectPointer;
     for (i = 0; i < 50; i++)
     {
         objectPointer = new MyClass;
         objectPointer->SetAge(2*i +1);
         myObject[i] = objectPointer;
     }
 
     for (i = 0; i < 50; i++)
         std::cout << "#" << i+1 << ": " << myObject[i]->GetAge() << std::endl;
 
     for (i = 0; i < 50; i++)
     {
         delete myObject[i];
         myObject[i] = NULL;
     }
 
     return 0;
 }
#1: 1
#2: 3
#3: 5
#4: 7
#5: 9
#6: 11
#7: 13
#8: 15
#9: 17
#10: 19
#11: 21
#12: 23
#13: 25
#14: 27
#15: 29
#16: 31
#17: 33
#18: 35
#19: 37
#20: 39
#21: 41
#22: 43
#23: 45
#24: 47
#25: 49
#26: 51
#27: 53
#28: 55
#29: 57
#30: 59
#31: 61
#32: 63
#33: 65
#34: 67
#35: 69
#36: 71
#37: 73
#38: 75
#39: 77
#40: 79
#41: 81
#42: 83
#43: 85
#44: 87
#45: 89
#46: 91
#47: 93
#48: 95
#49: 97
#50: 99

An array on the heap

#include <iostream>
 
 class MyClass
 {
 public:
     MyClass() { 
        itsAge = 1; 
        itsWeight=5; 
     }
     ~MyClass();                                
     int GetAge() const { return itsAge; }
     int GetWeight() const { return itsWeight; }
     void SetAge(int age) { itsAge = age; }
 
 private:
     int itsAge;
     int itsWeight;
 };
 
 MyClass :: ~MyClass()
 {
   std::cout << "Destructor called!\n";
 }
 
 int main()
 {
     MyClass * objectArrayPointer = new MyClass[50];
     int i;
     MyClass * currentPointer;
     for (i = 0; i < 50; i++)
     {
         currentPointer = new MyClass;
         currentPointer->SetAge(2*i +1);
         objectArrayPointer[i] = *currentPointer;
         delete currentPointer;
     }
 
     for (i = 0; i < 50; i++)
         std::cout << "#" << i+1 << ": " << objectArrayPointer[i].GetAge() << s
td::endl;
 
     delete [] objectArrayPointer;
 
     return 0;
 }
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
#1: 1
#2: 3
#3: 5
#4: 7
#5: 9
#6: 11
#7: 13
#8: 15
#9: 17
#10: 19
#11: 21
#12: 23
#13: 25
#14: 27
#15: 29
#16: 31
#17: 33
#18: 35
#19: 37
#20: 39
#21: 41
#22: 43
#23: 45
#24: 47
#25: 49
#26: 51
#27: 53
#28: 55
#29: 57
#30: 59
#31: 61
#32: 63
#33: 65
#34: 67
#35: 69
#36: 71
#37: 73
#38: 75
#39: 77
#40: 79
#41: 81
#42: 83
#43: 85
#44: 87
#45: 89
#46: 91
#47: 93
#48: 95
#49: 97
#50: 99
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!

Create an array of objects

#include <iostream> 
using namespace std; 
  
class MyClass { 
  int x; 
public: 
  void setX(int i) { x = i; } 
  int getX() { return x; } 
}; 
 
int main() 
{ 
  MyClass obs[4]; 
  int i; 
 
  for(i=0; i < 4; i++) 
    obs[i].setX(i); 
 
  for(i=0; i < 4; i++) 
    cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n"; 
 
  return 0; 
}
obs[0].getX(): 0
obs[1].getX(): 1
obs[2].getX(): 2
obs[3].getX(): 3

Delete an array of objects

#include <iostream>
#include <cstdlib>
#include <new>
using namespace std;
class Point {
  int x, y;
public:
  Point() {x = y = 0;}
  Point(int px, int py) {
    x = px;
    y = py;
  }
  void show() {
    cout << x << " ";
    cout << y << "\n";
  }
  void *operator new(size_t size);
  void operator delete(void *p);
  void *operator new[](size_t size);
  void operator delete[](void *p);
};
// new overloaded relative to Point.
void *Point::operator new(size_t size)
{
void *p;
  cout << "In overloaded new.\n";
  p =  malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}
// delete overloaded relative to Point.
void Point::operator delete(void *p)
{
  cout << "In overloaded delete.\n";
  free(p);
}
// new overloaded for Point arrays.
void *Point::operator new[](size_t size)
{
  void *p;
  cout << "Using overload new[].\n";
  p =  malloc(size);
  if(!p) {
    bad_alloc ba;
    throw ba;
  }
  return p;
}
// delete overloaded for Point arrays.
void Point::operator delete[](void *p)
{
  cout << "Freeing array using overloaded delete[]\n";
  free(p);
}
int main()
{
  Point *p1, *p2;
  int i;
  try {
    p1 = new Point (10, 20); // allocate an object
  } catch (bad_alloc xa) {
    cout << "Allocation error for p1.\n";
    return 1;;
  }
  try {
    p2 = new Point [10]; // allocate an array
  } catch (bad_alloc xa) {
    cout << "Allocation error for p2.\n";
    return 1;;
  }
  p1->show();
  for(i=0; i<10; i++)
    p2[i].show();
  delete p1; // free an object
  delete [] p2; // free an array
  return 0;
}
In overloaded new.
Using overload new[].
10 20
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
In overloaded delete.
Freeing array using overloaded delete[]

Initialize an array of objects by referencing the constructor directly

#include <iostream> 
using namespace std; 
 
class MyClass { 
  int x; 
public: 
  MyClass(int i) { x = i; } 
  int getX() { return x; } 
}; 
 
int main() 
{ 
  MyClass obs[4] = { MyClass(-1), MyClass (-2), 
                   MyClass (-3), MyClass (-4) };
  int i; 
 
  for(i=0; i < 4; i++) 
    cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n"; 
 
  return 0; 
}
obs[0].getX(): -1
obs[1].getX(): -2
obs[2].getX(): -3
obs[3].getX(): -4

Initialize an array of objects without referencing the constructor directly

#include <iostream> 
using namespace std; 
 
class MyClass { 
  int x; 
public: 
  MyClass(int i) { x = i; } 
  int getX() { return x; } 
}; 
 
int main() 
{ 
  MyClass obs[4] = { -1, -2, -3, -4 }; 
  int i; 
 
  for(i=0; i < 4; i++) 
    cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n"; 
 
  return 0; 
}
obs[0].getX(): -1
obs[1].getX(): -2
obs[2].getX(): -3
obs[3].getX(): -4

Object array of derived classes

#include <iostream> 
#include <cstring> 
using namespace std; 
 
class Shape { 
  double width; 
  double height; 
 
  char name[20]; 
public: 
  Shape() { 
    width = height = 0.0; 
    strcpy(name, "unknown"); 
  } 
 
  Shape(double w, double h, char *n) { 
    width = w; 
    height = h; 
    strcpy(name, n); 
  } 
 
  Shape(double x, char *n) { 
    width = height = x; 
    strcpy(name, n); 
  } 
 
  void display() { 
    cout << "Width and height are " << width << " and " << height << "\n"; 
  } 
 
  double getWidth() { return width; } 
  double getHeight() { return height; } 
  void setWidth(double w) { width = w; } 
  void setHeight(double h) { height = h; } 
  char *getName() { return name; } 
 
  virtual double area() {   
    cout << "Error: area() must be overridden.\n";  
    return 0.0;  
  }   
 
}; 
 
class Triangle : public Shape { 
  char style[20];
public: 
 
  Triangle() { 
    strcpy(style, "unknown"); 
  } 
 
  Triangle(char *str, double w, double h) : Shape(w, h, "triangle") { 
    strcpy(style, str); 
  } 
   
  Triangle(double x) : Shape(x, "triangle") { 
    strcpy(style, "isosceles");  
  } 
 
  double area() { 
    return getWidth() * getHeight() / 2; 
  } 
 
  void showStyle() { 
    cout << "Triangle is " << style << "\n"; 
  } 
}; 
 
class Rectangle : public Shape { 
public: 
 
  Rectangle(double w, double h) : Shape(w, h, "rectangle") { }  
  
  Rectangle(double x) : Shape(x, "rectangle") { }  
 
  bool isSquare() { 
    if(getWidth() == getHeight()) 
       return true; 
    return false; 
  } 
 
  double area() { 
    return getWidth() * getHeight(); 
  } 
}; 
 
int main() { 
  Shape *shapes[5]; 
  
  shapes[0] = &Triangle("right", 8.0, 12.0);  
  shapes[1] = &Rectangle(10);  
  shapes[2] = &Rectangle(10, 4);  
  shapes[3] = &Triangle(7.0);  
  shapes[4] = &Shape(10, 20, "generic"); 
  
  for(int i=0; i < 5; i++) {  
    cout << "object is " << shapes[i]->getName() << "\n";  
    cout << "Area is " << shapes[i]->area() << "\n\n";  
  }  
 
  return 0; 
}
object is generic
Error: area() must be overridden.
Area is 0
object is generic
Error: area() must be overridden.
Area is 0
object is generic
Error: area() must be overridden.
Area is 0
object is generic
Error: area() must be overridden.
Area is 0
object is generic
Error: area() must be overridden.
Area is 0