Question

Using C++ to write .cpp and .h file. Main function and sample output are given. The task is to write a vector class for dynamic allocation.declare the the class named vector with the required attributes. The task are defined in the main function.

#include <iostream> #include vector.h. #define LOG(x,y) std::cout << x << y « std::endl; #define INFO(x) std::cout << [INF// 6- Testing the overloaded array operator []. v1[0 50; // 7-Testing the overloaded shift operator < LOG (first index ishint vector() : vector(initialSize) {} vector (size_t size) _size(size) { size and initialize all members with zeros //cre[INFO]: [INFO]: object of class vector was created using the vector(size_t) constructor [INFO] object of class vector was cre[INFO [INFO]: object of class vector was created using the vector (size t) constructor [INFO] object of class vector was crea

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Implemented Code

#include <iostream>

using namespace std;

class vector
{
public:
   vector() :vector(initialsize)
   {
       cout << "\nObject of class vector was created using the vector() constructor";
      
   }
   vector(size_t size) :_size(size)
   {
       cout << "\nObject of class vector was created using the vector(size_t) constructor";
       data = new int[_size] {0};
   }

   vector(int* arr, size_t size)
   {
       data = new int[size];
       for (size_t i = 0; i < size; i++)
       {
           data[i] = arr[i];
       }
       _size = size;
   }

   void assign(int* arr, size_t size)
   {
       if (data)
       {
           delete[] data;
           _size = 0;
       }

       data = new int[size];
       for (size_t i = 0; i < size; i++)
       {
           data[i] = arr[i];
       }
       _size = size;
   }

   friend ostream& operator << (const ostream& out, const vector& v)
   {
       cout << "\nVector : size(3)";
       cout << "\ndata[0] :" << data[0];
       cout << "\ndata[1] :" << data[1];
       cout << "\ndata[2] :" << data[2];
   }

   vector add(const vector& v)
   {
       int sizeofnewv = (this->_size > v._size) ? v._size : this->_size;
       vector res(sizeofnewv);

       int k = 0;
       for (size_t i = 0,j = 0; i < this->_size,j < v._size; i++,j++)
       {
           res.data[k] = this->data[i] + v.data[j];
           k++;
       }

       return res;
   }

   vector operator + (const vector& v)
   {
       int sizeofnewv = (this->_size > v._size) ? this->_size : v._size;
       vector res(sizeofnewv);

       int k = 0;
       for (size_t i = 0, j = 0; i < this->_size, j < v._size; i++, j++)
       {
           res.data[k] = this->data[i] + v.data[j];
           k++;
       }

       return res;
   }

   int& operator[](int index)
   {
       if(index < _size)
           return data[index];

       return data[0];
   }

   void resize(size_t size)
   {
       data = (int*)realloc(data, size);
   }

   ~vector()
   {
       if(data)
           delete[] data;
       _size = 0;
       cout << "\nObject of class vector is deleted";
   }
private:
   const int initialsize = 3;
   int _size;
public:
   int* data;
};

Add a comment
Know the answer?
Add Answer to:
Using C++ to write .cpp and .h file. Main function and sample output are given. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT