Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do i implement the class smartArray, as a class template elemtype to accept

ID: 3701602 • Letter: H

Question

How do i implement the class smartArray, as a class template elemtype to accept any numerical value.

Below is code i have issues , in being unable to make the ostream and istream work for the template

template <class elemType>
class smartArray{
friend   ostream& operator<<(ostream &,const elemType &);    // i have issues with this
friend istream& operator>>(istream &, elemType & );               // issues with making this work as template
public:
elemType *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) ;
private:
int size;
};

template <class elemType>
ostream& operator<< (ostream &out,const elemType& b) {
    out << "[" ;
    for (int i = 0 ; i < b.size ;i++){
        out << b.elements[i] << ",";
}
   out << "]";
    return out;
}

template <class elemType>
istream& operator>>(istream &in, elemType& b) {
    for (int i = 0 ; i < b.size ;i++)
{ cout << "Enter element number "<< i << ": "; in >> b.elements[i];}
    return in;
}


Explanation / Answer

Below is the modified code to treat friend functions in a template class in a special way. Bolded lines are modified one:

template <class elemType>

class smartArray;
template <class elemType>
ostream& operator<<(ostream &,const smartArray<elemType> &);
template <class elemType>
istream& operator>>(istream &, smartArray<elemType> & );

template <class elemType>
class smartArray{
friend ostream& operator<< <>(ostream &,const smartArray &); // i have issues with this
friend istream& operator>> <>(istream &, smartArray & ); // issues with making this work as template

public:
elemType *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) ;
private:
int size;
};

template <class elemType>
ostream& operator<< (ostream &out,const smartArray<elemType>& b) {
out << "[" ;
for (int i = 0 ; i < b.size ;i++){
out << b.elements[i] << ",";
}
out << "]";
return out;
}

template <class elemType>
istream& operator>>(istream &in, smartArray<elemType>& b) {
for (int i = 0 ; i < b.size ;i++)
{ cout << "Enter element number "<< i << ": "; in >> b.elements[i];}
return in;
}

......

It requires a forward declaration and the overload on operator << is to print the class smartArray and not each element of type elemType. Therefore the friend function for overload operator << and >> should contain an argument of type smartArray not of type elemType.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote