Question

How do i Overload the & operator to concatenate two arrays?, so elements from both arrays...

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;
}

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

c++ code after removing error

---------------------------------------------------------------------------------------------------------------------------------------------------------

#include<iostream>

using namespace std;

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

{

size=0;

}

smartArray(int siz) // constructor, allocates the memory for *elements, if size==0, then no memory allocation is done

{

size=siz;

}

void input();

void dis();

//~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=a.size;

int q=m+n;

smartArray c(q);

// allocate meomry dynamic

c.elements=new int[q];

if(size< a.size){ // functions make array of elements from previous arrays

//cout<<"if part";

for ( i = 0 ; i < n ; i++)

c.elements[i]= elements[i];

for ( i = 0 ; i < m ; i++)

c.elements[n+i]=a.elements[i];

}

else

{

//cout<<"else part";

for ( i = 0 ; i < m ; i++)

c.elements[i]= a.elements[i];

for ( i = 0 ; i < n ; i++)

c.elements[m+i]=elements[i];

}

return c;

}

int main()

{

int n;

// input from user

cout<<"Enter numbers of elemets to store in array 1: ";

cin>>n;

smartArray ob1(n);

ob1.input();

cout<<"Enter numbers of elemets to store in array 2: ";

cin>>n;

smartArray ob2(n);

ob2.input();

smartArray ob3;

ob3=ob1&ob2;

ob3.dis();

}

void smartArray::input()

{

elements =new int[size];

for(int i=0;i<size;i++)

{

cin>>elements[i];

}

}

void smartArray::dis()

{

int i;

for(i=0;i<size;i++)

{

cout<<elements[i]<<"\t";

}

}

-------------------------------------------------------------------------------------------------------------------------------------------------------

Output

CAUsers satsahib Documents operator overloading 3.cpp [Executing) - Dev-C++5.11 File nter numbers of elemets to store in arra

Add a comment
Know the answer?
Add Answer to:
How do i Overload the & operator to concatenate two arrays?, so elements from both arrays...
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
  • Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include...

    Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include <iostream> using namespace std; template <typename T> struct Int_Node {    T value;    Int_Node<T> *pre, *next; }; template <typename T> class Link_List {    template <typename U>    friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list    template <typename U>    friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);...

  • Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

    Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...

  • Programming Question 4: Operator Overloading [20 marks] The class SpecialArray represents an arra...

    C++ please Programming Question 4: Operator Overloading [20 marks] The class SpecialArray represents an array of integers. The class contains two data members: array (of type int"), which represents the array of integers, and size (of type int) that represents the size of the array. The class SpecialArray also overloads the following operators: operator: Compares two arrays. An array "A" is smallerhan an array "B" if the size of array "A" is smaller than the size of "B", or if...

  • Do the following: 1) Add a constructor with two parameters, one for the numerator, one for...

    Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1 5) Modify the operator>> function so that after it reads the denominator...

  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string>...

    I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string> using namespace std; class Triple { private: int a, b, c; public: Triple(); // all elements have value 0 Triple(int k); // all elements have value k Triple(int x, int y, int z); // specifies all three elements Triple(string s); // string representation is "(a,b,c)" string toString(); // create a string representation of the vector void fromString(string s); // change the vector to equal...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

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