Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IntArray arr(10);
- for(int i = 0; 0 < 10; i++)
- a[i] = i * 10;
- a[0] = 0, a[1] =10, a[2]=20.... so on
- IntArray arr(-3, 10)
- IntArray arr(6, 8)`
- //---------------------Header file
- #ifndef INTARRAY_H_
- #define INTARRAY_H_
- #include <iostream>
- using namespace std;
- class IntArray
- {
- private:
- int first;
- int last;
- int size;
- string arrName;
- int* arrPtr;
- public:
- IntArray();
- int& operator[](int i);
- IntArray(int num);
- IntArray(int num1, int num2);
- int low();
- int high();
- void setName(string str);
- //streams
- friend istream& operator>>(istream& is, IntArray& d);
- friend ostream& operator<<(ostream& os, IntArray& d);
- };
- #endif /* INTARRAY_H_ */
- //---------------------
- #include "IntArray.h"
- IntArray::IntArray(){
- size = 10;
- first = 0; last = 9;
- arrPtr = new int[size];
- }
- IntArray::IntArray(int num){
- size = num;
- first = 0; last = size-1;
- arrPtr = new int[size];
- //cout<<"n the size "<<size<<endl;
- //arrPtr[3] = 123;
- //ex = 3;
- }
- IntArray::IntArray(int num1, int num2){
- first = num1; last = num2;
- size = last - first + 1;
- //cout<<"n the size "<<size<<endl;
- arrPtr = new int[size];
- }
- int& IntArray::operator[](int i) {
- if (i >= (last+1)){
- cout<<"Error: Index out of range"<<endl;
- exit(1);
- }
- return arrPtr[i];
- }
- int IntArray::getInt(int i){
- return arrPtr[i];
- }
- //======================
- int IntArray::low(){
- return first;
- }
- int IntArray::high(){
- return last;
- }
- //======================
- //==========OUT stream==========
- // for cout << justName<< endl;
- ostream& operator<<(ostream& os, IntArray& aPtr) {
- for(int i = aPtr.low(); i <= aPtr.high(); i++){
- os << aPtr.arrName <<"[" << i << "] = " << aPtr[i] << " ";
- }
- return os;
- }
- void IntArray::setName(string str){
- arrName = str;
- }
- IntArray c(6, 8);
- for(int i = c.low(); i <= c.high(); i++)
- c[i] = i * 10;
- c.setName("c");
- cout << c << endl;
- csis << c << endl;
- IntArray e(c);
- e.setName("e");
- cout << e << endl;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement