Advertisement
Guest User

Untitled

a guest
Jul 9th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class VEC;
  4.  
  5. class VEC {
  6. public:
  7. // default constructor
  8. VEC() {}
  9.  
  10. // dimensions constructor
  11. VEC(int d) {
  12. dim = d;
  13. data = new double[d];
  14. }
  15.  
  16. // copy constructor for &v
  17. VEC(const VEC &v) {
  18. std::cout << "\n Copy constructor (VEC) called!!";
  19. if (data)this->~VEC();
  20. dim = v.dim;
  21. data = v.data;
  22. if (data) memcpy((data = new double[dim]), v.data, sizeof(double) * dim);
  23. }
  24.  
  25. // assignment constructor for &v
  26. void operator = (const VEC &v)
  27. {
  28. std::cout << "\n Assignment constructor (VEC &v) called!!";
  29. if (data)this->~VEC();
  30. dim = v.dim;
  31. data = v.data;
  32. if (data) memcpy((data = new double[dim]), v.data, sizeof(double) * dim);
  33. }
  34.  
  35. // assignment constructor for v
  36. void operator = (const VEC v)
  37. {
  38. std::cout << "\n Assignment constructor (VEC v) called!!";
  39. if (data)this->~VEC();
  40. dim = v.dim;
  41. data = v.data;
  42. if (data) memcpy((data = new double[dim]), v.data, sizeof(double) * dim);
  43. }
  44.  
  45. // destructor
  46. ~VEC() {
  47. std::cout << "\n ~VEC got called. checking if data is true.";
  48. if (data) {
  49. std::cout << "\n data is true. attempting to delete data at address " << data;
  50. delete[] data;
  51. std::cout << "\n deletion succesful";
  52. }
  53. else std::cout << "\n data is false.";
  54. }
  55.  
  56. // copy
  57. void copy(const VEC &v)
  58. {
  59. std::cout << "\n attempting to copy!";
  60. dim = v.dim;
  61. data = v.data;
  62. if (data) memcpy((data = new double[dim]), v.data, sizeof(double) * dim);
  63. std::cout << "\n Original (v.data)'s address == " << v.data;
  64. std::cout << "\n New (data)'s address == " << data;
  65. std::cout << "\n Copy operation successful! ";
  66. }
  67. int dim;
  68. double* data;
  69. };
  70.  
  71. VEC Product(double x, VEC v) {
  72. int const dim = v.dim;
  73. VEC r(dim);
  74. for (int i = 0; i < v.dim; ++i) {
  75. r.data[i] = v.data[i] * x;
  76. }
  77. return VEC(r);
  78. }
  79.  
  80. VEC EmptyVector(int n) {
  81. VEC r(n);
  82. std::cout << "\n empty vec start. \n vec address == " << &r << " and its data address == " << r.data;
  83. for (int i = 0; i < n; ++i) {
  84. r.data[i] = 0;
  85. }
  86. return VEC(r);
  87. }
  88.  
  89. VEC UnitVector(int i, int n) {
  90. VEC r;
  91. std::cout << "\n unit vec start. \n vec address == " << &r << " and its data address == " << r.data;
  92. r.copy(EmptyVector(n));
  93. std::cout << "\n unit vec after copy. \n vec address == " << &r << " and its data address == " << r.data;
  94. r.data[i] = 1;
  95. return VEC(r);
  96. }
  97.  
  98. int main() {
  99. std::cout << "\n attempting to calculate the gravity";
  100. VEC gravity = Product(5, UnitVector(1, 2));
  101. std::cout << "\n gravity vector calculation complete.";
  102. int pause = 0;
  103. std::cin >> pause;
  104. return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement