Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include "limitarr.h"
  4. #include "ArrayDb.h";
  5. const int arrlen = 4;
  6.  
  7. int main() {
  8. LimitArr lim1(arrlen, 20, 10);
  9. cout << lim1;
  10. for (int i = 20; i < 24; i++)
  11. lim1[i] = i + 100;
  12. cout << lim1;
  13. cout << lim1[22] << "\n";
  14. ArrayDb & ra = lim1;
  15. ArrayDb * pa = &lim1;
  16. ra[22] = 88.87;
  17. (*pa)[21] = 66;
  18. cout << lim1;
  19. system("pause");
  20. return 0;
  21. }
  22.  
  23.  
  24. //////////////
  25.  
  26.  
  27. // arraydb.h ‐‐ define array class
  28. #ifndef _ARRAYDB_H_
  29. #define _ARRAYDB_H_
  30. #include <ostream>
  31. #include <iostream>
  32. using namespace std;
  33. class ArrayDb {
  34. private:
  35. int size; // number of array elements
  36. protected:
  37. double * arr; // address of first element
  38. public:
  39. ArrayDb(); // default constructor
  40. // create an ArrayDb of n elements, set each to val
  41. ArrayDb(int n, double val = 0.0);
  42. // create an ArrayDb of n elements, initialize to array pn
  43. ArrayDb(const double * pn, unsigned int n);
  44. ArrayDb(const ArrayDb & a); // copy constructor
  45. virtual ~ArrayDb(); // destructor
  46. unsigned int arsize() const { return size; } // returns array size
  47. // overloaded operators
  48. virtual double & operator[](int i); // array indexing
  49. virtual const double & operator[](int i) const; // array indexing (no =)
  50. ArrayDb & operator=(const ArrayDb & a);
  51. friend std::ostream & operator<<(std::ostream & os, const ArrayDb & a);
  52. };
  53. #endif
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. //#include "stdafx.h"
  61. #include <pch.h>
  62. #include <iostream>
  63. #include <stdlib.h> // exit() prototype
  64. #include "arraydb.h"
  65. // default constructor ‐‐ no arguments
  66. ArrayDb::ArrayDb() {
  67. arr = NULL;
  68. size = 0;
  69. }
  70. // constructs array of n elements, each set to val
  71. ArrayDb::ArrayDb(int n, double val) {
  72. arr = new double[n];
  73. size = n;
  74. for (int i = 0; i < size; i++)
  75. arr[i] = val;
  76. }
  77. // initialize ArrayDb object to a non‐class array
  78. ArrayDb::ArrayDb(const double *pn, unsigned int n) {
  79. arr = new double[n];
  80. size = n;
  81. for (int i = 0; i < size; i++)
  82. arr[i] = pn[i];
  83. }
  84. // initialize ArrayDb object to another ArrayDb object
  85. ArrayDb::ArrayDb(const ArrayDb & a) {
  86. size = a.size;
  87. arr = new double[size];
  88. for (int i = 0; i < size; i++)
  89. arr[i] = a.arr[i];
  90. }
  91. ArrayDb::~ArrayDb() {
  92. delete[] arr;
  93. }
  94. // let user access elements by index (assignment allowed)
  95. double & ArrayDb::operator[](int i) {
  96. // check index before continuing
  97. if (i < 0 || i >= size) {
  98. std::cout << "Error in array limits: "
  99. << i << " is a bad index\n";
  100. exit(1);
  101. }
  102. return arr[i];
  103. }
  104. // let user access elements by index (assignment disallowed)
  105. const double & ArrayDb::operator[](int i) const {
  106. // check index before continuing
  107. if (i < 0 || i >= size) {
  108. std::cout << "Error in array limits: "
  109. << i << " is a bad index\n";
  110. exit(1);
  111. }
  112. return arr[i];
  113. }
  114. // define class assignment
  115. ArrayDb & ArrayDb::operator=(const ArrayDb & a) {
  116. if (this == &a) // if object assigned to self,
  117. return *this; // don't change anything
  118. delete arr;
  119. size = a.size;
  120. arr = new double[size];
  121. for (int i = 0; i < size; i++)
  122. arr[i] = a.arr[i];
  123. return *this;
  124. }
  125. // quick output, 5 values to a line
  126. std::ostream & operator<<(std::ostream & os, const ArrayDb & a) {
  127. int i;
  128. for (i = 0; i < a.size; i++) {
  129. os << a.arr[i] << " ";
  130. if (i % 5 == 4)
  131. os << "\n";
  132. }
  133. if (i % 5 != 0)
  134. os << "\n";
  135. return os;
  136. }
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. #include "pch.h"
  148. #include "LimitArr.h"
  149. #include <iostream>
  150. #include <stdlib.h>
  151.  
  152. LimitArr::LimitArr(void) :ArrayDb() {
  153. l_bound = 0;
  154. }
  155. LimitArr::LimitArr(int n, double val) : ArrayDb(n, val) {
  156. l_bound = 0;
  157. }
  158.  
  159. LimitArr::LimitArr(int n, int lb, double val) : ArrayDb(n, val) {
  160. l_bound = lb;
  161. }
  162.  
  163. LimitArr::LimitArr(const double * pn, int n, int lb) : ArrayDb(pn, n) {
  164. l_bound = lb;
  165. }
  166. LimitArr::LimitArr(const LimitArr & a) : ArrayDb(a) {
  167. l_bound = a.l_bound;
  168. }
  169. LimitArr::LimitArr(const ArrayDb & a) : ArrayDb(a) {
  170. l_bound = 0;
  171. }
  172. LimitArr::~LimitArr(void) {
  173. }
  174. void LimitArr::new_lb(int lb) {
  175. l_bound = lb;
  176. }
  177. int LimitArr::lbound() {
  178. return l_bound;
  179. }
  180. int LimitArr::ubound() {
  181. return l_bound + arsize() - 1;
  182. }
  183. double & LimitArr::operator[](int i) {
  184. return arr[i - l_bound];
  185. }
  186. const double & LimitArr::operator[](int i) const {
  187. return arr[i - l_bound];
  188. }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. #pragma once
  198. #ifndef _LIMARR_H_
  199. #define _LIMARR_H_
  200. #include "ArrayDb.h"
  201. class LimitArr :
  202. public ArrayDb {
  203. protected:
  204. int low_bnd;
  205. void ok(int i) const;
  206. public:
  207. LimitArr(void);
  208. LimitArr(int n, double val = 0.0);
  209. LimitArr(int n, int lb, double val = 0.0);
  210. //where lb is lower bond of array
  211. LimitArr(const double * pn, int n, int lb = 0);
  212. LimitArr(const LimitArr & a);
  213. LimitArr(const ArrayDb & a);
  214. ~LimitArr(void);
  215.  
  216. int l_bound;
  217.  
  218. void new_lb(int lb);// reset lower bound
  219. int lbound(); // return lower bound
  220. int ubound(); // return upper bound
  221.  
  222. double & operator[](int i); // array indexing
  223. const double & operator[](int i) const; // array indexing (no =)
  224.  
  225. };
  226. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement