5 Replies - 414 Views - Last Post: 10 June 2012 - 03:00 AM Rate Topic: -----

#1 oteel  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 06-June 12

Exception handling object

Posted 09 June 2012 - 03:46 AM

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 .
//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");
		
		}



Is This A Good Question/Topic? 1
  • +

Replies To: Exception handling object

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Exception handling object

Posted 09 June 2012 - 05:13 AM

Your OutOfBoundsException code looks fine for the requirement, so I'm not sure what you're seeking here.
Was This Post Helpful? 2
  • +
  • -

#3 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Exception handling object

Posted 09 June 2012 - 05:16 AM

Your question isn't clear, what appears to be the problem? What errors, if any, are you getting? At a glance it seems to compile and run fine.
Was This Post Helpful? 2
  • +
  • -

#4 oteel  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 06-June 12

Re: Exception handling object

Posted 09 June 2012 - 07:20 AM

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
private: 
    int m_size;                            // Size of Dynamic Array.   


to private data from Exception handling class


class OutOfBoundsException : public ArrayException
{
private : 
	int m_index;


Is it clear now ?
I'm also should say sorry for my English :):):):)
Was This Post Helpful? 1
  • +
  • -

#5 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Exception handling object

Posted 09 June 2012 - 07:57 AM

Ah okay, so there are a few ways you could incorporate this. One would be to have a simple function that just returns the value held by m_index, something like GetIndex. Since you'll have the object when you catch it, you could output the string and then the index using GetIndex().

Another way would be to incorporate this number into the string you return from GetMessage(), which I guess would fit closer to what you've done so far. You could maybe look at stringstreams to do this (you seem to already be using stringstreams so I'm assuming you know how they work). Something I came up looked like
virtual string GetMessage()
{
   ostringstream output;
		
   output << "\nthe given index is out of bounds, index [" << m_index << ']';
   return output.str();
}

You could also define another inserter (overload the << operator) for the OutOfBoundsException class as an alternative to having a GetMessage() function. Hope this is useful.

This post has been edited by nuclearfroggy: 09 June 2012 - 03:32 PM

Was This Post Helpful? 2
  • +
  • -

#6 oteel  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 06-June 12

Re: Exception handling object

Posted 10 June 2012 - 03:00 AM

View Postnuclearfroggy, on 09 June 2012 - 07:57 AM, said:

Ah okay, so there are a few ways you could incorporate this. One would be to have a simple function that just returns the value held by m_index, something like GetIndex. Since you'll have the object when you catch it, you could output the string and then the index using GetIndex().

Another way would be to incorporate this number into the string you return from GetMessage(), which I guess would fit closer to what you've done so far. You could maybe look at stringstreams to do this (you seem to already be using stringstreams so I'm assuming you know how they work). Something I came up looked like
virtual string GetMessage()
{
   ostringstream output;
		
   output << "\nthe given index is out of bounds, index [" << m_index << ']';
   return output.str();
}

You could also define another inserter (overload the << operator) for the OutOfBoundsException class as an alternative to having a GetMessage() function. Hope this is useful.



many thanks your your post !!!! You made my life better !

View Postoteel, on 10 June 2012 - 02:59 AM, said:

View Postnuclearfroggy, on 09 June 2012 - 07:57 AM, said:

Ah okay, so there are a few ways you could incorporate this. One would be to have a simple function that just returns the value held by m_index, something like GetIndex. Since you'll have the object when you catch it, you could output the string and then the index using GetIndex().

Another way would be to incorporate this number into the string you return from GetMessage(), which I guess would fit closer to what you've done so far. You could maybe look at stringstreams to do this (you seem to already be using stringstreams so I'm assuming you know how they work). Something I came up looked like
virtual string GetMessage()
{
   ostringstream output;
		
   output << "\nthe given index is out of bounds, index [" << m_index << ']';
   return output.str();
}

You could also define another inserter (overload the << operator) for the OutOfBoundsException class as an alternative to having a GetMessage() function. Hope this is useful.



many thanks your your post !!!! You made my life better !


*Many thanks you for your post !
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1