Home > Back-end >  Novice C primer the fifth edition of teaching materials in case of a two dimensional array class tem
Novice C primer the fifth edition of teaching materials in case of a two dimensional array class tem

Time:12-10

As title, the book is a class template is used to realize two dimensional array, allows the result is that , the first line should be 1, 2, 3, 4, 5, the output of this thing is what ah, ask god to help see, novice small white online
 # # ifndef _ARRAYTP_H_ 
# define _ARRAYTP_H_
#include
#include
template
The class ArrayTP
{
Public:
ArrayTP () {}
Explicit ArrayTP (const T & amp; V);
Virtual T & amp; Operator [] (int I);
Virtual operator T [] (int I) const;
Friend STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, ArrayTP & amp; Tp);

Private:
T ar [n].

};
template
ArrayTP : : ArrayTP (const T & amp; V) {
for (int i=0; I & lt; n; I++)
Ar [I]=v;
}

template
T & amp; ArrayTP : : operator (int I) [] {
If (i<0 | | i>=n)
{
STD: : cerr & lt; <"Error in array limits:" & lt; STD: : exit (EXIT_FAILURE);
}
Return the ar [I];
}
template
T ArrayTP : : operator (int I) const [] {
If (I & lt; 0 | | I & gt;=n)
{
STD: : cerr & lt; <"Error in array limits:" & lt; STD: : exit (EXIT_FAILURE);
}
Return the ar [I];
}
template
STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, ArrayTP & Tp) {
OS & lt; Return the OS;
}
# endif//! _ARRAYTP_H_



 # include & lt; Iostream> 
# include "arraytp. H"

Int main ()
{
Using STD: : cout;
Using STD: : endl;
ArrayTPArrayTPArrayTPint i, j;
For (I=0; I & lt; 10; I++) {
Sums [I]=0;
For (j=0; j<5; J++)
{
Twodee [I] [j]=(I + 1) * (j + 1);
Sums [I] +=twodee [I] [j];


}
Aves [I]=(double) sums [I]/10;
}
For (I=0; I & lt; 10; I++)
{
for (j=0; J & lt; 5; J++)
{
Cout. Width (4);
Cout & lt; }
Cout & lt; <": the sum=";
Cout. Width (3);
Cout & lt; return 0;
}
  • Related