Create and Implement a C++ program to make a class named Array_Operations
● Globally declare size of array.
● Data member of class should be a dynamic array.
● Set all values as zero in default constructor.
● Overload Subscript operator i.e. []
● Overload streams operators (should be friend functions)
o cin i.e. >> to input object values
o cout i.e. << to output/display values
● Now take 4 objects of class in main. Initialize values of 1st and 2nd array (using stream
operators >> and subscript [] overloading). Now overload +,-, operators to add and
subtract 1st and 2nd arrays and store the result in 3
rd and 4th array respectively. Now
display all arrays.
#include <iostream>
#include <vector>
using namespace std;
static const ARR_SIZE = 16;
class Array_Operations {
public:
  Array_Operations() {
    for (int i = 0; i < ARR_SIZE; i++)
      data[i] = 0;
  }
 
  friend ostream &operator<<( ostream &output, const Array_Operations &arr ) { 
    output << "[" << arr.data[0];
    for (int i = 1; i < ARR_SIZE; i++)
      output << ", " << arr.data[i];
    output << "]";
    return output;            
  }
  friend istream &operator>>( istream  &input, const Array_Operations &arr ) { 
    for (int i = 0; i < ARR_SIZE; i++)
      input >> arr.data[i];
    return input;            
  }
  friend Array_Operations& operator+(const Array_Operations& lhs,
                   const Array_Operations& rhs) {
    Array_Operations tmpArray;
    for (int i = 0; i < ARR_SIZE; i++)
      tmpArray.data[i] = lhs.data[i] + rhs.data[i];
  }
  friend Array_Operations& operator-(const Array_Operations& lhs,
                   const Array_Operations& rhs) {
    Array_Operations tmpArray;
    for (int i = 0; i < ARR_SIZE; i++)
      tmpArray.data[i] = lhs.data[i] - rhs.data[i];
  }
private:
  vector<int> data(ARR_SIZE);
};
int main(void) 
{
  Array_Operations arr1, arr2, arr3, arr3;
  cin >> arr1 >> arr2;
  arr3 = arr1 + arr2;
  arr4 = arr1 - arr2; 
  cout << arr1 << "\n";
  cout << arr2 << "\n";
  cout << arr3 << "\n";
  cout << arr4 << "\n";
  return 0;
}
Comments