Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class FloatingVector{
  5. int vecSize;
  6. float * vals;
  7. public:
  8. FloatingVector (int const = 0,
  9. float const * const = NULL);
  10. void print() const;
  11. void set(int const, float const);
  12. // Some other members...
  13. ~FloatingVector(); // give back vals!
  14. FloatingVector(const FloatingVector &); // Copy Constructor, 11 uses
  15. FloatingVector operator+(const FloatingVector &);
  16. };
  17.  
  18. FloatingVector::FloatingVector(int const size,float const * const tmp){
  19. vecSize = tmp==NULL ? 0 : size;
  20. vals = vecSize==0 ? NULL : new float[vecSize];
  21. for(int i=0;i<vecSize;i++) vals[i] = tmp[i];
  22. cout << "default costructor" << endl;
  23. }
  24.  
  25. void FloatingVector::print() const {
  26. cout << "Vector size: " << vecSize << " | " << "Content: ";
  27. for(int i=0;i<vecSize;i++)
  28. cout << vals[i] << ' ';
  29. cout << '\n';
  30. }
  31.  
  32. void FloatingVector::set(int const index, float const val){
  33. if(index<vecSize) vals[index] = val;
  34. cout << "set function called" << endl;
  35. }
  36.  
  37.  
  38. FloatingVector::~FloatingVector() { delete[] vals;
  39. cout << "delete operation" << endl;
  40. };
  41.  
  42. FloatingVector::FloatingVector(const FloatingVector &rhs){
  43. vecSize = rhs.vecSize;
  44. vals = new float[vecSize];
  45. for(int i=0;i<vecSize;i++) vals[i] = rhs.vals[i];
  46. cout << "copy constructor called" << endl;
  47. }
  48.  
  49. FloatingVector& FloatingVector::operator+(const FloatingVector &rhs){
  50.  
  51. FloatingVector* tmp1 = new FloatingVector();
  52. if ( vecSize != rhs.vecSize ) return *tmp1;
  53. float *tmpFloat = new float[vecSize];
  54. for(int i=0;i<vecSize;i++) tmpFloat[i] = vals[i] + rhs.vals[i];
  55. cout << "its time to + operator duty..." << endl;
  56.  
  57. FloatingVector *tmp = new FloatingVector(vecSize, tmpFloat);
  58.  
  59. return *tmp;
  60. };
  61.  
  62. int main(void){
  63. float a_arr[] = {1.1, 2.2, 3.3};
  64. float b_arr[] = {1.2, 2.3, 3.4};
  65. FloatingVector a(3, a_arr);
  66. FloatingVector b(3, b_arr);
  67. FloatingVector c = a + a + b;
  68. a.print();
  69. b.print();
  70. c.print();
  71. float d_arr[] = {0.1, 0.2};
  72. FloatingVector d(2, d_arr);
  73. FloatingVector e = d;
  74. d.set(1, 9.9);
  75. d.print();
  76. e.print();
  77. // if the size of the vectors
  78. // doesn’t match return an empty
  79. // vector of size 0.
  80. FloatingVector f = c + d;
  81. f.print();
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement