Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class FloatingVector{
- int vecSize;
- float * vals;
- public:
- FloatingVector (int const = 0,
- float const * const = NULL);
- void print() const;
- void set(int const, float const);
- // Some other members...
- ~FloatingVector(); // give back vals!
- FloatingVector(const FloatingVector &); // Copy Constructor, 11 uses
- FloatingVector operator+(const FloatingVector &);
- };
- FloatingVector::FloatingVector(int const size,float const * const tmp){
- vecSize = tmp==NULL ? 0 : size;
- vals = vecSize==0 ? NULL : new float[vecSize];
- for(int i=0;i<vecSize;i++) vals[i] = tmp[i];
- cout << "default costructor" << endl;
- }
- void FloatingVector::print() const {
- cout << "Vector size: " << vecSize << " | " << "Content: ";
- for(int i=0;i<vecSize;i++)
- cout << vals[i] << ' ';
- cout << '\n';
- }
- void FloatingVector::set(int const index, float const val){
- if(index<vecSize) vals[index] = val;
- cout << "set function called" << endl;
- }
- FloatingVector::~FloatingVector() { delete[] vals;
- cout << "delete operation" << endl;
- };
- FloatingVector::FloatingVector(const FloatingVector &rhs){
- vecSize = rhs.vecSize;
- vals = new float[vecSize];
- for(int i=0;i<vecSize;i++) vals[i] = rhs.vals[i];
- cout << "copy constructor called" << endl;
- }
- FloatingVector& FloatingVector::operator+(const FloatingVector &rhs){
- FloatingVector* tmp1 = new FloatingVector();
- if ( vecSize != rhs.vecSize ) return *tmp1;
- float *tmpFloat = new float[vecSize];
- for(int i=0;i<vecSize;i++) tmpFloat[i] = vals[i] + rhs.vals[i];
- cout << "its time to + operator duty..." << endl;
- FloatingVector *tmp = new FloatingVector(vecSize, tmpFloat);
- return *tmp;
- };
- int main(void){
- float a_arr[] = {1.1, 2.2, 3.3};
- float b_arr[] = {1.2, 2.3, 3.4};
- FloatingVector a(3, a_arr);
- FloatingVector b(3, b_arr);
- FloatingVector c = a + a + b;
- a.print();
- b.print();
- c.print();
- float d_arr[] = {0.1, 0.2};
- FloatingVector d(2, d_arr);
- FloatingVector e = d;
- d.set(1, 9.9);
- d.print();
- e.print();
- // if the size of the vectors
- // doesn’t match return an empty
- // vector of size 0.
- FloatingVector f = c + d;
- f.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement