candyapplecorn

class walledMatrix : public matrix

Jun 30th, 2014
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. /*
  2. I'm just playing around with inheritance here.
  3. walledMatrix takes a matrix and lines its "edges" with walls.
  4.  
  5. edit: changed to support templates
  6. */
  7. template <class T>
  8. class walledMatrix : public matrix<T>
  9. {
  10. public:
  11.     walledMatrix(int x, int y) : matrix<T>(x, y){ wall(); }
  12.     void wall(){
  13.         for (int i = 0; i < length; i++){
  14.             operator()(i, 0) = 6, operator()(i, width - 1) = 7;
  15.         }
  16.         for (int i = 0; i < width; i++){
  17.             operator()(0, i) = 8, operator()(length - 1, i) = 9;
  18.         }
  19.     }
  20. };
  21. // Sample program:
  22. /*
  23. int _tmain(int argc, _TCHAR* argv[])
  24. {
  25.     int length = 20, width = 20;
  26.     walledMatrix<int> mat(length, width);
  27.  
  28.     cout << "Sample area:\n";
  29.     for (int c = 0; c < length; c++){
  30.         cout << endl;
  31.         for (int r = 0; r < width; r++)
  32.             cout << (mat(c, r) ? 'X' : '0');
  33.     }
  34.     cout << endl << endl;
  35.     return 0;
  36. }
  37. */
  38. // sample output:
  39. /*
  40. Sample area:
  41.  
  42. XXXXXXXXXXXXXXXXXXXX
  43. X000000000000000000X
  44. X000000000000000000X
  45. X000000000000000000X
  46. X000000000000000000X
  47. X000000000000000000X
  48. X000000000000000000X
  49. X000000000000000000X
  50. X000000000000000000X
  51. X000000000000000000X
  52. X000000000000000000X
  53. X000000000000000000X
  54. X000000000000000000X
  55. X000000000000000000X
  56. X000000000000000000X
  57. X000000000000000000X
  58. X000000000000000000X
  59. X000000000000000000X
  60. X000000000000000000X
  61. XXXXXXXXXXXXXXXXXXXX
  62.  
  63. Press any key to continue . . .
  64. */
Advertisement
Add Comment
Please, Sign In to add comment