In: Computer Science
Consider the following header file for the intArray object and the following main.cpp and answer the following questions:
intArray.h |
main.cpp |
#include <iostream> using namespace std; class intArray { friend ostream& operator<<(ostream& outStream, intArray& rhs); public: intArray(); intArray(int _size); intArray(int _size, int array[]); intArray(const intArray&); ~intArray(); void Print(); void PrintSize(); int Size() const; int operator[](int) const; intArray operator=(const intArray ); private: int size; int *data; }; |
7 #include <iostream> |
Give the only prototype for the overloaded + operators as shown in lines 22, 23 and 24. Make sure all are cascade capable to allow the assignment to Array3.
Prototypes are written below alongwith the explanation. If you need any further clarification please feel free to ask in comments.
Line 22 Array3=Array1 +Array2;
Prototype:--->>> const intArray operator +( const intArray&);
Explain: As the assignment wants to add two intArray objects and return another intArray object, we need a overloaded function that returns intArray object and takes intArray object as parameter. The above operator overloaded function fullfills all the conditions for this assignemnt.
Line 23 Array3=Aray1 +5;
Prototype:---->>> const intArray operator +(const int);
Explain: As the assignment wants to add an intArray object and integer & return another intArray object, we need a overloaded function that returns intArray object and takes integer as parameter. The above operator overloaded function fullfills all the conditions for this assignemnt.
Line 24 Array3= 6 + Array1;
Prototype:---->>>> friend intArray operator +(const int, const intArray&);
Explain: As the assignment is done using integer as first argument and intArray object as second, We need a friend function which is operator overloaded in nature. A friend function which takes integer as first argument and intArray object as second parameter. The above friend operator overloaded function fullfills all the conditions for this assignemnt.