smgr

Untitled

Mar 13th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include "vektor.h"
  3.  
  4. unsigned int Vektor::defSize = 12;
  5. double Vektor::defValue = 418;
  6.  
  7. /// Másoló konstruktor.
  8. /// @param v - Vektor referencia
  9. Vektor::Vektor(const Vektor& v) {
  10.     this->nElements = v.nElements;
  11.     this->pVec = new double[this->nElements];
  12.     for (unsigned int i = 0; i < this->nElements; i++) {
  13.         this->pVec[i] = v.pVec[i];
  14.     }
  15. }
  16.  
  17. /// Destruktor.
  18. Vektor::~Vektor() {
  19.     delete[] this->pVec;
  20. }
  21.  
  22. /// Értékadó operátor.
  23. /// @param v - Vektor referencia
  24. /// @return  - referencia az adott elemre
  25. Vektor& Vektor::operator=(const Vektor& v) {
  26.     if (this != &v) {
  27.         delete[] this->pVec;
  28.         this->nElements = v.nElements;
  29.         this->pVec = new double[this->nElements];
  30.         for (unsigned int i = 0; i < this->nElements; i++) {
  31.             this->pVec[i] = v.pVec[i];
  32.         }
  33.     }
  34.     return *this;
  35. }
  36.  
  37. /// Indexoperátorok.
  38. /// Túlcímzés esetén Ön nagybetűs Neptun-kódját tartalmazó const char* típusú kivételt dob!
  39. /// @param idx - index értéke
  40. /// @return    - referencia az adott elemre
  41. double& Vektor::operator[](unsigned int idx) {
  42.     if (idx >= this->nElements) {
  43.         throw "ILE2S9";
  44.     }
  45.     return this->pVec[idx];
  46. }
  47. const double& Vektor::operator[](unsigned int idx) const {
  48.     if (idx >= this->nElements) {
  49.         throw "ILE2S9";
  50.     }
  51.     return this->pVec[idx];
  52. }
  53.  
  54. /// Szorzás: Valós * Vektor
  55. /// @param val - valós érték (bal oldali operandus)
  56. /// @param vec - vektor, aminek minden elemét megszorosszuk (jobb oldali operandus)
  57. Vektor operator*(double val, const Vektor& vec) {
  58.     Vektor vec_new = vec;
  59.     for (unsigned int i = 0; i < vec.size(); i++) {
  60.         vec_new[i] *= val;
  61.     }
  62.     return vec_new;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment