Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. # include <iostream>
  2. # include <stdexcept>
  3. using namespace std;
  4.  
  5. struct BadIndex : public std::exception {} ;
  6. const int ARRAY_LENGTH = 10 ;
  7.  
  8. class Homework
  9. {
  10. public :
  11. int a ;
  12. double b ;
  13. string c ;
  14. Homework(int a , double b , string c) ;
  15.  
  16. };
  17.  
  18. Homework :: Homework(int a , double b , string c)
  19. {
  20. this->a = a ;
  21. this->b = b ;
  22. this->c = c ;
  23. }
  24.  
  25. template<typename T>
  26. T getArrayElement(Homework c , int len , int index)
  27. {
  28. if(0 <= index && index < len)
  29. {
  30. Homework bbb(1 , 3.3 , "ok") ;
  31. return bbb ;
  32. }
  33. else if(index <= -1 && index >= -(len-1))
  34. {
  35. Homework bbb(2 , 3.3 , "ok") ;
  36. return bbb ;
  37. }
  38. else
  39. throw BadIndex() ;
  40. }
  41.  
  42.  
  43. int main()
  44. {
  45. Homework homework(3 , 5.646 , "hw") ;
  46. int index = 0 ;
  47. cin >> index ;
  48.  
  49. try
  50. {
  51. Homework result = getArrayElement<Homework>(homework , ARRAY_LENGTH , index) ;
  52. cout << result.a ;
  53. }
  54.  
  55. catch (BadIndex e)
  56. {
  57. cout << "Bad index !\n" ;
  58. }
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement