Exception handling object

I'm out of track how to realize one point in exercise condition
wording :
Give the OutOfBoundsException class a constructor with an int as argument that indicates the erroneous array index and store it in a data member.

How should I change the code below , please help .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//array.h
#ifndef Array_H
#define Array_H
#include "point.h"
#include <string>
using namespace std;
 
class Array
{
private:
    Point* m_data;                         // Dynamic Array of Point pointers.    
    int m_size;                            // Size of Dynamic Array.                                             
public: 		
    Array();                         // Default constructor
    Array(const int new_size);       // constructor with size argument.
    Array(const Array& ar);          // Copy constructor
    ~Array() ;                     // Destructor		
    int Size();                      // Get the size of the m_data pointer Array	
    void SetElement(const Point& pt, int index);
    Point& Array::GetElement(int index);
    Array& Array::operator = (const Array& source);    // Assignment operator.
    Point& Array::operator [] (int index); //[] operator can be used for both reading and writing elements. 		
    friend ostream& operator << (ostream& os, const Array& ar); // Global Methods
};
#endif // Array_H

//ArrayException.h
#include <iostream>
#include <string>
using namespace std;

class ArrayException 
{
public : 
	ArrayException(){};
	virtual string GetMessage() = 0;
};

class OutOfBoundsException : public ArrayException
{
private : 
	int m_index;
public : 
	OutOfBoundsException();
	OutOfBoundsException(int index) : m_index(index) {}
	virtual string GetMessage()
	{
	return "\nthe given index is out of bounds "  ;
	}

};

// point.h
#ifndef Point_H
#define Point_H
#include <string>
#include <iostream>
using namespace std;
class Point
{
private:
    double m_x;                                // X coordinate for Point 
    double m_y;                                // Y coordinate
public:
    // Constructors
    Point();                            // Default constructor
	Point(double new_x, double new_y);  // Initialize with x and y value
	~Point();							// Destructor
	Point(const Point& point);      // Copy Constructor
	string Point::ToString(void) const;            // String representation of a point
    friend ostream& operator << (ostream& os, const Point& point);  
};
#endif // Point_H

//array.cpp
#include "array.h"
#include "point.h"
#include "ArrayException.h"
#include <cmath>
#include <sstream>
#include <iostream>
using namespace std;

void Array::SetElement(const Point& pt, int index)
{
	try
	{
		if (index >= m_size){throw OutOfBoundsException(11);} // When the index is out of bounds, ignore the “set”.
		m_data[index] = pt;
		cout << "Set Element " << pt  << endl;
	}
	catch ( OutOfBoundsException& outOfBoundsException )
	 {
		 cout << outOfBoundsException.GetMessage()  << endl;
	 }
}

Point& Array::GetElement(int index)
{
   // cout << "GetElement...done " << endl;
	try 
	{
		if (index > m_size || index < 0){ throw OutOfBoundsException(12);} // по заданию надо ставить вроде как -1 но что то ошибка какая то когда минус один ставишь разобраться потом 
	} 
	catch (OutOfBoundsException& outOfBoundsException)
	{
		cout << outOfBoundsException.GetMessage() << endl; 
	}  
	return m_data[index];
}


Array::Array()                                     
{
 //   cout << "Array Default Constructor... done" << endl;
    m_size = 10;
    m_data = new Point[m_size];
}
Array::Array(const int new_size) //specified by the size input argument							
{
 //   cout << "Array size Constructor" << endl;
    m_size = new_size;
    m_data = new Point[m_size];
}
Array::Array(const Array& ar) 
{
 //   cout << "Array copy Constructor...done" << endl; 
    m_size    =    ar.m_size;  // shallow copy - this is not dynamic alloc 
    if (ar.m_data)  // if not zeros then there is a ref. - Deep copy needed
    {
        cout <<"im here on copy"<<endl;
        m_data = new Point[ar.m_size];
        for (int i = 0; i < ar.m_size; i++)
            m_data[i]    =    ar.m_data[i];
    }
    else
        m_data = 0;  //NULL
}
Array::~Array()                                 
{
  //  cout << "Array Destructor... done" << endl;
    delete [] m_data;
}
Array& Array::operator = (const Array& source)
{
  //  cout << "Array Assignment operator... done" << endl; 
    if (this == &source)
        {
      //  cout << "Same Array " << endl;
        return *this;
        }
    delete [] m_data; // my data has been destroyed
  //  cout <<"Deleted m_data array" << endl; 
    m_size = source.m_size;  // shallow copy - this is not dynamic alloc 
    if (source.m_data)  // if not zeros then there is a ref. - Deep copy needed
    {
      //  cout <<"im here"<<endl;
 
        m_data = new Point[source.m_size];  // create a new pointee.
 
        for (int i = 0; i < source.m_size; i++)
            m_data[i]    =    source.m_data[i]; //copy the points from/to array
    }
    else
        m_data = 0;  //NULL
    return *this;
}
int Array::Size() 
{
    return m_size;
} 


ostream& operator << (ostream& os, const Array& ar)
{
    os << "\nArray Size = " << ar.m_size << endl;
    for (int i = 0; i < ar.m_size; i++)
        os << "Array [" << i << "]= "<< ar.m_data[i] << endl;
    return os;
}
Point& Array::operator [] (int index)
{
  //  cout << "Array [] operator" << endl;
    if (index > this->m_size)
	{//When the index is out of bounds, return the first element. 
     //   cout << "i am hreeeee" << endl;
        return this->m_data[0];
	}
    return m_data[index];
}

//point.cpp
#include "point.h"
#include <sstream>
#include <string>
using namespace std;

ostream& operator << (ostream& os, const Point& point)
{
    return os << point.ToString();
}

Point::Point() : m_x(0), m_y(0) {}
Point::Point(double new_x, double new_y) : m_x(new_x), m_y(new_y) {}
Point::Point(const Point& point)
{
    m_x = point.m_x;
    m_y = point.m_y;
}
Point::~Point() {
	//cout << "Point destructor... done " << endl;
}

string Point::ToString() const
{
    ostringstream os;
    os << m_x << ", " << m_y;
    string double_string = "Point("; // create a string like: “Point(1.5, 3.9)”
    double_string += os.str();
    double_string += ")";
    return double_string;
}

		//main.cpp
		#include <iostream>
		#include <sstream>
		#include "array.h"
		#include "point.h"
		#include "ArrayException.h"
		using namespace std;
		int main()
		{
			{
			Point* p1 = new Point(1,12);
			Point* p2 = new Point(-3,4);
			Point* p3 = new Point(-32,21);
			//	cout << " TEST OF ToString fuctionf working : " << p2->ToString() << endl;
				cout << endl; 
			Array arr1(3);
			arr1.SetElement(*p1,0);
			arr1.SetElement(*p2,1);
			arr1.SetElement(*p3,5);
				cout << arr1.GetElement(0) << endl << arr1.GetElement(1) << endl << arr1.GetElement(2) << endl << endl;
			Array arr2(3);
	/*		arr1 = arr1;// assignment operator test
				cout << arr1.GetElement(0) << endl << arr1.GetElement(1) << endl << arr1.GetElement(2) << endl << endl;
			arr2 = arr1;// assignment operator test
				cout << arr2.GetElement(0) << endl << arr2.GetElement(1) << endl << arr2.GetElement(2) << endl << endl;
			Array arr3(arr2);// Copy constructor test
				cout << arr3.GetElement(0) << endl << arr3.GetElement(1) << endl << arr3.GetElement(2) << endl << endl;
				cout << endl; 
				cout << "now let's test [] overload operator" << endl;*/
			Array* my_Array3 = new Array(3);
			my_Array3->SetElement(*p2,0);
			my_Array3->SetElement(*p3,1);
				cout << "my_Array3 [] operator " << (*my_Array3)[1] << endl;
			delete p1;
			delete p2;
			delete p3;
			delete my_Array3;
		}

			system ("pause");
		
		}
closed account (o3hC5Di1)
Hi there,

First, let's see what file we'll need. The exercise sais to adapt the outofboundException - located in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//ArrayException.h
#include <iostream>
#include <string>
using namespace std;

class ArrayException 
{
public : 
	ArrayException(){};
	virtual string GetMessage() = 0;
};

class OutOfBoundsException : public ArrayException
{
private : 
	int m_index;
public : 
	OutOfBoundsException();
	OutOfBoundsException(int index) : m_index(index) {}
	virtual string GetMessage()
	{
	return "\nthe given index is out of bounds "  ;
	}

};


Now, let's break down your assignment:

Give the OutOfBoundsException class a constructor with an int as argument...


This line:
OutOfBoundsException(int index) : m_index(index) {}

Does exactly that.

... that indicates the erroneous array index and store it in a data member.


Now comes the tricky part. The int passed as an argument will be the out-of-bound index of the array. So you will need to pass the index to the class constructor in a manner similar to:

if arr[i] is out of bounds
   throw outofboundsException( i )


I'm not sure what is meant by "data member", perhaps someone else would be so kind to shed some light on that.

Hope that gets you on your way.

All the best,
NwN
I just realize that my ask not clear enough so I decide clarify a little bit. Everything ok with compilation. I just don't understand how realize one feature. I what that my exception handling object output me not just message that there is exception but also give me number of index that don't exceptable for this program.

"
Give the OutOfBoundsException class a constructor with an int as argument that indicates the erroneous array index and store it in a data member.
"

so I should connect this data -> array index
1
2
private: 
    int m_size;                            // Size of Dynamic Array.    


to private data from Exception handling class


1
2
3
4
class OutOfBoundsException : public ArrayException
{
private : 
	int m_index;


Is it clear now ?
I'm also should say sorry for my English :):):):)
closed account (o3hC5Di1)
Hi there,

Sorry, I still seems to me like the same issue?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//this sets the m_index variable
OutOfBoundsException(int index) : m_index(index) 
{
    m_index = index;
}

//while looping our array
for (int i=0; i<10;i++)
{
    if (out of bounds)
    {
        //this throws the exception with current iteration number as index
        throw OutOfBoundsException(i);
    }
}

catch (OutOfBoundsException e)
{
    e.getMessage(); //prints the error
}


Hope that helps.

All the best,
NwN
Topic archived. No new replies allowed.