mituri

[C++] przeciazanie operatora ()

Jun 19th, 2015
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. /*
  2.  
  3. Stworz klase int2d oraz przeciaz operator() aby mozna bylo wywolac program wg kodu ponizej:
  4.  
  5. int main()
  6. {
  7. int2d t1(4,3);
  8. for(int i=0; i<4; i++)
  9. for(int j=0; j<3; j++)
  10. t1(i,j)=i+j;
  11. for(int i=0; i<4; i++)
  12. {
  13. for(int j=0; j<3; j++) cout << t1(i,j)<<" ";
  14. cout<<endl;
  15. }
  16. }
  17. Wyświetlał następujące wyniki:
  18. 0 1 2
  19. 1 2 3
  20. 2 3 4
  21. 3 4 5
  22.  
  23. */
  24.  
  25. #include "stdafx.h"
  26. #include <iostream>
  27. #include <string>
  28. #include <queue>
  29. #include <fstream>
  30. #include <vector>
  31.  
  32. using namespace::std;
  33.  
  34.  
  35. class int2d : public vector<vector<int>> {
  36. public:
  37. vector<vector<int>> vec;
  38. int a;
  39. int b;
  40.  
  41. int2d(int a = 0, int b = 0) {
  42. vector<int> tmp(b);
  43. fill(tmp.begin(), tmp.end(), 0);
  44. for(int i = 0; i < a; i++) vec.push_back(tmp);
  45. }
  46.  
  47. int2d& operator = (const int2d& X){
  48. if (this == &X)
  49. return *this;
  50. int2d tmp(X.a, X.b);
  51. }
  52.  
  53. int& operator()(int aa, int bb){
  54. a = aa;
  55. b = bb;
  56. return vec[a][b];
  57. }
  58. };
  59.  
  60. int _tmain(int argc, _TCHAR* argv[]){
  61.  
  62. int2d t1(4, 3);
  63.  
  64. for(int i = 0; i < 4; i++)
  65. {
  66. for(int j = 0; j < 3; j++)
  67. {
  68. t1(i, j) = i + j;
  69. }
  70. }
  71.  
  72. for(int i = 0; i < 4; i++)
  73. {
  74. for(int j = 0; j < 3; j++)
  75. cout << t1(i, j) << " ";
  76. {
  77. cout<<endl;
  78. }
  79. }
  80.  
  81. system("PAUSE");
  82. }
Advertisement
Add Comment
Please, Sign In to add comment