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] +'0';}
  48. return *this;
  49. }
  50.  
  51. protected:
  52. static const int max_size = 10;
  53. unsigned char *ptr;
  54. int size;
  55. };
  56.  
  57. class Money:public Array {
  58. protected:
  59. static const int max_size = 100;
  60.  
  61. };
  62.  
  63. int main() {
  64. int n = 5;
  65. Array table(n);
  66. Array t(n);
  67. cout << table <<'\n';
  68.  
  69. for(int i = 0; i < n; i++)
  70. {table[i] = i +'0';
  71. t[i] = i + 3 +'0';}
  72.  
  73. cout << table <<'\n';
  74. cout << t <<'\n';
  75. table = table+t;
  76. cout << table <<'\n';
  77.  
  78. return 0;
  79. }
Success #stdin #stdout 0s 15240KB
stdin
1392.789
stdout
00000
01234
34567