Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. // CS 2134 HW 02
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. class IntArray{
  7. public:
  8. //IntArray(int n=0) : size(n), array(new int[n]) {}
  9. IntArray(int n=0):size(n){}
  10. //deep copy
  11. IntArray(const IntArray& og) : size(og.size), array(new int[og.size]) {
  12. for (int i=0; i<this->size; ++i) {
  13. this->array[i] = og.array[i];
  14. }
  15. }
  16. //move constructor
  17. IntArray(IntArray&& og) : size(og.size), array(og.array) {
  18. og.size = 0;
  19. og.array = nullptr;
  20. }
  21. //destructor
  22. ~IntArray() {
  23. delete [] array;
  24. }
  25. //move assignment operator
  26. IntArray& operator=(IntArray&& og){
  27. if (this != og) {
  28. delete [] array;
  29. this->size = og.size;
  30. this->array = og.array;
  31. og.size = 0;
  32. og.array = nullptr;
  33. }
  34. return *this;
  35. }
  36.  
  37. void put(int i, int value) {
  38. if (i < this->size) {
  39. this->array[i] = value;
  40. }
  41. }
  42.  
  43. int get(int i) {
  44. if (i < this->size){}
  45. return this->array[i];
  46. }
  47. }
  48.  
  49. void print() const {
  50. for (int i=0; i<this->size; ++i) {
  51. cout << this->array[i];
  52. }
  53. cout << endl;
  54. }
  55. private:
  56. int* array;
  57. int size;
  58. };
  59.  
  60. int main() {
  61. IntArray* a = new IntArray(5);
  62. a->put(0,12);
  63. a->put(1,24);
  64. a->put(2,36);
  65. a->put(3,48);
  66. a->put(4,60);
  67. a->print();
  68.  
  69. IntArray b(*a);
  70. b.print();
  71.  
  72. IntArray c(move(*a));
  73. c.print();
  74.  
  75. IntArray d = b;
  76. d.print();
  77.  
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement