fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdexcept>
  4. #include <string.h>
  5.  
  6. using namespace std;
  7.  
  8. class Array {
  9. public:
  10. Array() {
  11. size = max_size;
  12. ptr = new unsigned char[size];
  13. }
  14. Array(int size) {
  15. if(size <= max_size)
  16. this->size = size;
  17. else
  18. this->size = max_size;
  19. ptr = new unsigned char[size];
  20. for(int i = 0; i < size; i++)
  21. ptr[i] = '0';
  22. }
  23. Array(const Array &obj) {
  24. size = obj.size;
  25. ptr = new unsigned char[size];
  26. for(int i = 0; i < size; i++)
  27. ptr[i] = obj.ptr[i];
  28. }
  29. ~Array() {
  30. delete []ptr;
  31. }
  32. unsigned char &operator[](int index) {
  33. if(index >= 0 && index < size)
  34. return ptr[index];
  35. else
  36. throw out_of_range("out_of_range");
  37.  
  38. }
  39. friend ostream &operator <<(ostream &os, const Array &obj) {
  40. for(int i = 0; i < obj.size; i++)
  41. os << obj.ptr[i];
  42. return os;
  43. }
  44.  
  45. virtual Array operator+(const Array &obj2) {
  46. for(int i = 0; i < obj2.size; i++)
  47. this->ptr[i] = (int)this->ptr[i] + (int)obj2.ptr[i];
  48. }
  49.  
  50. protected:
  51. static const int max_size = 10;
  52. unsigned char *ptr;
  53. int size;
  54. };
  55.  
  56. class Money:public Array {
  57. protected:
  58. static const int max_size = 100;
  59.  
  60. };
  61.  
  62. int main() {
  63. int n = 5;
  64. Array table(n);
  65. Array t(n);
  66. cout << table;
  67.  
  68. for(int i = 0; i < n; i++)
  69. {table[i] = i;
  70. t[i] = i + 3;}
  71.  
  72. cout << table;
  73. cout << t;
  74. cout << table+t;
  75.  
  76.  
  77. return 0;
  78. }
Success #stdin #stdout 0s 15240KB
stdin
1392.789
stdout
00000