C++/Data Structure/Array Object

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Arrays of Objects

 
#include <iostream>
using namespace std;
class MyClass {
  int i;
public:
  void setInt(int j) { 
     i=j; 
  }
  int getInt() { 
     return i; 
  }
};
int main()
{
  MyClass myObject[3];
  int i;
  for(i=0; i<3; i++) 
     myObject[i].setInt(i+1);
  for(i=0; i<3; i++)
     cout << myObject[i].getInt() << "\n";
  return 0;
}


A simple bounded 2-d array example.

 

#include <iostream>
#include <cstdlib>
using namespace std;
class MyArray {
  int isize, jsize;
  int *p;
public:
  MyArray(int i, int j);
  int &put(int i, int j);
  int get(int i, int j);
};
MyArray::MyArray(int i, int j)
{
  p = new int [i*j];
  if(!p) {
    cout << "Allocation error\n";
    exit(1);
  }
  isize = i;
  jsize = j;
}
int &MyArray::put(int i, int j)
{
  if(i <0 || i>=isize || j<0 || j>=jsize) {
    cout << "Bounds error!!!\n";
    exit(1);
  }
  return p[i*jsize + j];
}
int MyArray::get(int i, int j)
{
  if(i <0 || i>=isize || j<0 || j>=jsize) {
    cout << "Bounds error!!!\n";
    exit(1);
  }
  return p[i*jsize +j]; 
}
  
int main()
{
  MyArray a(2, 3);
  int i, j;
  for(i = 0; i <2; i++)
    for(j=0; j<3; j++)
      a.put(i, j) = i+j;
  for(i = 0; i <2; i++)
    for(j=0; j<3; j++)
      cout << a.get(i, j) << " ";
  a.put(10, 10);
  return 0;
}


Define and init class array: two int type parameters in constructor

 
#include <iostream>
using namespace std;
class squares {
  int num, sqr;
public:
  squares(int a, int b) { 
     num = a; 
     sqr = b; 
  }
  void show() {
     cout << num << " " << sqr << endl; 
  }
};
int main()
{
  squares ob[10] = {
    squares(1, 1),
    squares(2, 4),
    squares(3, 9),
    squares(4, 16),
    squares(5, 25),
    squares(6, 36),
    squares(7, 49),
    squares(8, 64),
    squares(9, 81),
    squares(10, 100)
  };
  int i;
  for(i = 0; i <10; i++) 
     ob[ i ].show();
  return 0;
}


Incrementing an object pointer

  
#include <iostream>  
using namespace std;
class MyClass {  
  int num;  
public:  
  void set_num(int val) {num = val;}  
  void show_num();  
};  
     
void MyClass::show_num()  
{  
  cout << num << "\n";  
}  
     
main(void)  
{  
  MyClass ob[2], *p;  
     
  ob[0].set_num(10);
  ob[1].set_num(20);  
     
  p = &ob[0];  
  p->show_num(); 
     
  p++;  
  p->show_num(); 
     
  p++; 
  p->show_num(); 
     
  return 0;  
}


Init class array: char type

 
#include <iostream>
using namespace std;
class CharClass {
  char ch;
public:
  CharClass(char c) { 
     ch = c; 
  }
  char get_ch() { 
     return ch; 
  }
};
int main()
{
  CharClass ob[10] = { "a", "b", "c","d", "e", "f", 
                     "g", "h", "i", "j" };
  int i;
  for(i = 0; i <10; i++) 
    cout << ob[ i ].get_ch() << " ";
  cout << endl;
  return 0;
}


Init class array: int constructor parameter

 
#include <iostream>
using namespace std;
class samp {
  int x;
public:
  samp(int n) { 
     x = n; 
  }
  int getx() { 
     return x; 
  }
};
int main()
{
  samp A[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  int i;
  for(i = 0; i <10; i++) 
     cout << A[ i ].getx() << " ";
  cout << endl;
  return 0;
}


Initialize each object in an array by specifying an initialization list

 
#include <iostream>
using namespace std;
class MyClass {
  int i;
public:
  MyClass(int j) { 
     i = j; 
  } // constructor
  int getInt() { 
     return i; 
  }
};
int main()
{
  MyClass myObject[3] = {1, 2, 3}; // initializers
  int i;
  for(i=0; i<3; i++)
    cout << myObject[i].getInt() << "\n";
  return 0;
}


Two dimension object array: reference by index

 
#include <iostream>
using namespace std;
class MyClass {
  double a, b;
public:
  MyClass(double x, double y) {
    a = x;
    b = y;
  }
  void show() { 
    cout << a << " " << b << "\n"; 
  }
};
int main()
{
  MyClass ob[2][5] = {
    MyClass(1, 1), MyClass(2, 2),
    MyClass(3, 3), MyClass(4, 4),
    MyClass(5, 5), MyClass(6, 6),
    MyClass(7, 7), MyClass(8, 8),
    MyClass(9, 9), MyClass(10, 10)
  };
  int i, j;
  for(i = 0; i <2; i++)
    for(j=0; j<5; j++)
      ob[ i ][j].show();
  cout << "\n";
  return 0;
}


Two dimension object array: reference by pointer

 
#include <iostream>
using namespace std;
class MyClass {
  double a, b;
public:
  MyClass(double x, double y) {
    a = x;
    b = y;
  }
  void show() { cout << a << " " << b << "\n"; }
};
int main()
{
  MyClass ob[2][5] = {
    MyClass(1, 1), MyClass(2, 2),
    MyClass(3, 3), MyClass(4, 4),
    MyClass(5, 5), MyClass(6, 6),
    MyClass(7, 7), MyClass(8, 8),
    MyClass(9, 9), MyClass(10, 10)
  };
  MyClass *p;
  p = (MyClass *) ob;
  int i, j;
  for(i = 0; i <2; i++)
    for(j=0; j<5; j++) {
      p->show();
      p++;
    }
  cout << "\n";
  return 0;
}