Advertisement
herhor67

Vector3D

Jun 5th, 2019
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <iostream>
  5.  
  6. #include "windows.h"
  7.  
  8. #include <string>
  9.  
  10.  
  11. using namespace std;
  12.  
  13.  
  14. class v3d {
  15. public:
  16.     double* arr;
  17.  
  18.     v3d() {
  19.         cout << "Tworzymy..." << endl;
  20.         arr = (double*)calloc(3, sizeof(double));
  21.     }
  22.  
  23.     ~v3d() {
  24.         cout << "Przed free: ";
  25.         free(arr);
  26.         cout << " i po free." << endl;
  27.     }
  28.  
  29.     v3d operator + (const v3d& b)
  30.     {
  31.         v3d temp1;
  32.         temp1.arr[0] = arr[0] + b.arr[0];
  33.         temp1.arr[1] = arr[1] + b.arr[1];
  34.         temp1.arr[2] = arr[2] + b.arr[2];
  35.         return temp1;
  36.     }
  37.  
  38.     v3d& operator += (const v3d& b)
  39.     {
  40.         arr[0] += b.arr[0];
  41.         arr[1] += b.arr[1];
  42.         arr[2] += b.arr[2];
  43.         return *this;
  44.     }
  45.     v3d& operator -= (const v3d& b)
  46.     {
  47.         arr[0] -= b.arr[0];
  48.         arr[1] -= b.arr[1];
  49.         arr[2] -= b.arr[2];
  50.         return *this;
  51.     }
  52.  
  53.     v3d& operator = (const v3d& b)
  54.     {
  55.         this->arr[0] = b.arr[0];
  56.         this->arr[1] = b.arr[1];
  57.         this->arr[2] = b.arr[2];
  58.         return *this;
  59.     }
  60.  
  61.     double len() {
  62.         return pow(pow(arr[0], 2) + pow(arr[1], 2) + pow(arr[2], 2), 0.5);
  63.     }
  64.  
  65.     string to_str() {
  66.  
  67.         return "[" + to_string(arr[0]) + ", " + to_string(arr[1]) + ", " + to_string(arr[2]) + "]";
  68.     }
  69. };
  70.  
  71.  
  72. int main()
  73. {
  74.     v3d a, b, sum;
  75.  
  76.     a.arr[0] = 12;
  77.     a.arr[1] = -56;
  78.     a.arr[2] = 5e-3;
  79.  
  80.     b.arr[0] = 7 - 3e2;
  81.     b.arr[1] = 24.789;
  82.     b.arr[2] = 0;
  83.  
  84.     a -= b;
  85.  
  86.     sum = a + b;
  87.  
  88.     cout << sum.to_str() << endl << sum.len() << endl;
  89.     system("pause");
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement