Question
How do i Overload the & operator to concatenate two arrays?, so elements from both arrays will be seen Below is code i have so
How do i Overload the & operator to concatenate two arrays?, so elements from both arrays will be seen
Below is code i have so far. everything works except the operator overload of &. The & operator only works when both array are equal, first array smaller then second, fails when first array is the largest.
class smartArray{ public: int *elements; // dynamic array, memory is allocated in the constructor. Use *this to access size member. int length(); // returns the length of the array smartArray(); // default constructor, sets size to 0 smartArray(int size); // constructor, allocates the memory for *elements, if size==0, then no memory allocation is done ~smartArray() ;// destructor void resize(int newsize) ; // resizes array bool operator==(const smartArray &another_point) ; // overloads == operator bool operator!=(const smartArray &another_point) { return !((*this)==another_point); } int& operator[] (const int index); smartArray(smartArray &); // copy constructor
smartArray operator&(const smartArray & ); smartArray operator+(const smartArray & ); smartArray operator*(const smartArray & ); smartArray operator/(const smartArray & ); smartArray operator-(const smartArray & ); private : friend ostream& operator<<(ostream &, smartArray &); friend istream& operator>>(istream &, smartArray & ); int size ; };
smartArray smartArray :: operator&(const smartArray& a ) { // overlaods concatenation of arrays, checks for size of arrays int i,m,n; n=size m=size>a.size?size:a.size; int q=m+n; smartArray c(q); if(size< a.size){ // functions make array of elements from previous arrays for ( i = 0 ; i < n ; i++) c.elements[i]= elements[i]; for ( i = 0 ; i < q ; i++) c.elements[n+i]=a.elements[i]; } else for ( i = 0 ; i < n ; i++){ c.elements[i]= a.elements[i]; for ( i = 0 ; i < q ; i++) c.elements[n+i]=elements[i]; } return c; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started