Your goal is to overload the operators for “Array” class. You will need to write three files (array.h, array.cpp).
class Array{
// think about the private data members...
public:
Array();
Array(int size);
Array(int* arr, int size);
Array(const Array &);
int& operator[](int i); // returns int at [i] int& operator[](int i) const;
const Array & operator=(const Array&);
Array operator+(const Array&);
Array operator-(const Array&);
Array operator++();
Array operator++(int);
Array& operator--(int);
bool operator==(const Array&)const; // returns true if two arrays are same
bool operator!(); // returns true if the Array is empty
void operator+=(const Array&);
void operator-=(const Array&);
int operator()(int idx, int val); // erases the value val at idx. Returns 1 for deletion and -1 if idx does not exist or is invalid.
~Array();
};
ostream& operator<<(ostream& input, const Array&);
istream& operator>>(istream& output, Array&);
#include<iostream>
#include<vector>
using namespace std;
class Array
{
vector<int>v;
public:
Array():v(0) {};
Array(int size) :v(size, 0) {};
Array(int* arr, int size)
{
for (int i = 0; i < size; i++)
{
v.push_back(arr[i]);
}
}
Array(const Array& a) :v(a.v) {}
int& operator[](int i) // returns int at [i] int& operator[](int i) const
{
return v[i];
}
const Array& operator=(const Array& a)
{
v = a.v;
return *this;
}
Array operator+(const Array& a)
{
Array tmp(v.size());
for (int i = 0; i < v.size(); i++)
{
tmp.v[i] = v[i] + a.v[i];
}
return tmp;
}
Array operator-(const Array& a)
{
Array tmp(v.size());
for (int i = 0; i < v.size(); i++)
{
tmp.v[i] = v[i] - a.v[i];
}
return tmp;
}
Array operator++()
{
Array tmp(v.size());
for (int i = 0; i < v.size(); i++)
{
tmp.v[i] = v[i] +1;
}
return tmp;
}
Array operator+(int val)
{
Array tmp(v.size());
for (int i = 0; i < v.size(); i++)
{
tmp.v[i] = v[i] + val;
}
return tmp;
}
Array& operator--(int)
{
Array tmp(v.size());
for (int i = 0; i < v.size(); i++)
{
tmp.v[i] = v[i] - 1;
}
return tmp;
}
bool operator==(const Array& a)const // returns true if two arrays are same
{
if (v.size() != a.v.size())return false;
for (int i = 0; i < v.size(); i++)
{
if (v[i] != a.v[i]) return false;
}
return true;
}
bool operator!() // returns true if the Array is empty
{
if (v.size() == 0)return true;
else return false;
}
void operator+=(const Array& a)
{
for (int i = 0; i < v.size(); i++)
{
v[i] += a.v[i];
}
}
void operator-=(const Array& a)
{
for (int i = 0; i < v.size(); i++)
{
v[i] -= a.v[i];
}
}
int operator()(int idx, int val) // erases the value val at idx. Returns 1 for deletion and -1 if idx does not exist or is invalid.
{
if (v[idx] == val)
{
vector<int>::iterator it;
for (it = v.begin(); it != v.end(); it++)
{
if (*it == val)
{
v.erase(it);
break;
}
}
}
}
~Array() {};
friend ostream& operator<<(ostream& os, const Array&);
friend istream& operator >> (istream& output, Array&);
};
ostream& operator<<(ostream& os, const Array& a)
{
for (int i = 0; i < a.v.size(); i++)
{
os << a.v[i] << " ";
}
return os;
}
istream& operator >> (istream& is, Array& a)
{
for (int i = 0; i < a.v.size(); i++)
{
is>>a.v[i];
}
return is;
}
int main()
{
Array a(5);
cout << "Enter values of an array: ";
cin >> a;
cout << "Third of an array: ";
cout << a[2] << endl;
int arr[5] = { 1,2,3,4,5 };
Array b(arr, 5);
cout << b<<endl;
cout << b+5 << endl;
cout << a +b << endl;
cout << a -b << endl;
a += b;
cout << a << endl;
a -= b;
cout << a << endl;
}
Comments
Leave a comment