General Template Friend Class or Function in C++

It would be helpful to add a display operator to the Array class. One approach would be to declare a display operator for each possible t...


It would be helpful to add a display operator to the Array class. One approach would be to declare a display operator for each possible type of Array, but this would undermine the whole point of having made Array a template.
What is needed is an insertion operator that works for any possible type of Array.
ostream& operator<< (ostream& Array<T>&);
To make this work, we need to declare operator<< to be a template function.
template <class T> ostream& operator<< (ostream&, Array<T>&)
Now that operator<< is a template function, you need only to provide an implementation. Listing 19.4 shows the Array template extended to include this declaration and provides the implementation for the operator<<.

NOTE: To compile this listing, copy lines 8-26 of Listing 19.2 and insert them between lines 3 and 4. Also copy lines 51-86 of Listing 19.2 and insert them between lines 37 and 38.

Listing 19.4. Using operator ostream.

1: #include <iostream.h>
2:
3: const int DefaultSize = 10;
4:
5: template <class T> // declare the template and the parameter
6: class Array // the class being parameterized
7: {
8: public:
9: // constructors
10: Array(int itsSize = DefaultSize);
11: Array(const Array &rhs);
12: ~Array() { delete [] pType; }
13:
14: // operators
15: Array& operator=(const Array&);
16: T& operator[](int offSet) { return pType[offSet]; }
17: const T& operator[](int offSet) const
18: { return pType[offSet]; }
19: // accessors
20: int GetSize() const { return itsSize; }
21:
22: friend ostream& operator<< (ostream&, Array<T>&);
23:
24: private:
25: T *pType;
26: int itsSize;
27: };
28:
29: template <class T>
30: ostream& operator<< (ostream& output, Array<T>& theArray)
31: {
32: for (int i = 0; i<theArray.GetSize(); i++)
33: output << "[" << i << "] " << theArray[i] << endl; return output;
34: }
35:
36: enum BOOL { FALSE, TRUE};
37:
38: int main()
39: {
40: BOOL Stop = FALSE; // flag for looping
41: int offset, value;
42: Array<int> theArray;
43:
44: while (!Stop)
45: {
46: cout << "Enter an offset (0-9) ";
47: cout << "and a value. (-1 to stop): " ;
47: cin >> offset >> value;
48:
49: if (offset < 0)
50: break;
51:
52: if (offset > 9)
53: {
54: cout << "***Please use values between 0 and 9.***\n";
55: continue;
56: }
57:
58: theArray[offset] = value;
59: }
60:
61: cout << "\nHere's the entire array:\n";
62: cout << theArray << endl;
63: return 0;
64: }


Output: Enter an offset (0-9) and a value. (-1 to stop): 1 10
Enter an offset (0-9) and a value. (-1 to stop): 2 20
Enter an offset (0-9) and a value. (-1 to stop): 3 30
Enter an offset (0-9) and a value. (-1 to stop): 4 40
Enter an offset (0-9) and a value. (-1 to stop): 5 50
Enter an offset (0-9) and a value. (-1 to stop): 6 60
Enter an offset (0-9) and a value. (-1 to stop): 7 70
Enter an offset (0-9) and a value. (-1 to stop): 8 80
Enter an offset (0-9) and a value. (-1 to stop): 9 90
Enter an offset (0-9) and a value. (-1 to stop): 10 10
***Please use values between 0 and 9.***
Enter an offset (0-9) and a value. (-1 to stop): -1 -1

Here's the entire array:
[0] 0
[1] 10
[2] 20
[3] 30
[4] 40
[5] 50
[6] 60
[7] 70
[8] 80
[9] 90

Analysis: On line 22, the function template operator<<() is declared to be a friend of the Array class template. Because operator<<() is implemented as a template function, every instance of this parameterized array type will automatically have an operator<<(). The implementation for this operator starts on line 29. Every member of an array is called in turn. This only works if there is an operator<< defined for every type of object stored in the array.
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: General Template Friend Class or Function in C++
General Template Friend Class or Function in C++
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/general-template-friend-class-or.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/general-template-friend-class-or.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content