Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. IntArray arr(10);
  2. for(int i = 0; 0 < 10; i++)
  3. a[i] = i * 10;
  4.  
  5. a[0] = 0, a[1] =10, a[2]=20.... so on
  6.  
  7. IntArray arr(-3, 10)
  8.  
  9. IntArray arr(6, 8)`
  10.  
  11. //---------------------Header file
  12.  
  13. #ifndef INTARRAY_H_
  14. #define INTARRAY_H_
  15.  
  16. #include <iostream>
  17.  
  18.  
  19. using namespace std;
  20.  
  21. class IntArray
  22. {
  23.  
  24. private:
  25. int first;
  26. int last;
  27. int size;
  28.  
  29. string arrName;
  30. int* arrPtr;
  31.  
  32.  
  33. public:
  34. IntArray();
  35. int& operator[](int i);
  36.  
  37. IntArray(int num);
  38. IntArray(int num1, int num2);
  39.  
  40. int low();
  41. int high();
  42. void setName(string str);
  43. //streams
  44. friend istream& operator>>(istream& is, IntArray& d);
  45. friend ostream& operator<<(ostream& os, IntArray& d);
  46.  
  47. };
  48.  
  49. #endif /* INTARRAY_H_ */
  50. //---------------------
  51.  
  52. #include "IntArray.h"
  53.  
  54.  
  55.  
  56. IntArray::IntArray(){
  57. size = 10;
  58. first = 0; last = 9;
  59. arrPtr = new int[size];
  60. }
  61.  
  62. IntArray::IntArray(int num){
  63. size = num;
  64. first = 0; last = size-1;
  65. arrPtr = new int[size];
  66. //cout<<"n the size "<<size<<endl;
  67. //arrPtr[3] = 123;
  68.  
  69. //ex = 3;
  70. }
  71.  
  72. IntArray::IntArray(int num1, int num2){
  73. first = num1; last = num2;
  74. size = last - first + 1;
  75. //cout<<"n the size "<<size<<endl;
  76. arrPtr = new int[size];
  77. }
  78.  
  79. int& IntArray::operator[](int i) {
  80. if (i >= (last+1)){
  81. cout<<"Error: Index out of range"<<endl;
  82. exit(1);
  83. }
  84. return arrPtr[i];
  85. }
  86.  
  87. int IntArray::getInt(int i){
  88. return arrPtr[i];
  89. }
  90.  
  91. //======================
  92. int IntArray::low(){
  93. return first;
  94. }
  95. int IntArray::high(){
  96. return last;
  97. }
  98. //======================
  99. //==========OUT stream==========
  100.  
  101. // for cout << justName<< endl;
  102. ostream& operator<<(ostream& os, IntArray& aPtr) {
  103. for(int i = aPtr.low(); i <= aPtr.high(); i++){
  104. os << aPtr.arrName <<"[" << i << "] = " << aPtr[i] << " ";
  105. }
  106.  
  107. return os;
  108. }
  109.  
  110. void IntArray::setName(string str){
  111. arrName = str;
  112. }
  113.  
  114. IntArray c(6, 8);
  115. for(int i = c.low(); i <= c.high(); i++)
  116. c[i] = i * 10;
  117. c.setName("c");
  118. cout << c << endl;
  119. csis << c << endl;
  120. IntArray e(c);
  121. e.setName("e");
  122. cout << e << endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement