65,204
社区成员




#ifndef Array_cpp
#define Array_cpp
//Implementation of Array class
#include "Array.hpp"
#include "Point.hpp"
#include "OutOfBoundsException.hpp"
namespace Corneliagao{
namespace CAD{
// Default Constructor
template <class T>
Array<T>::Array()
{
//Set the default size of array to 10
m_size = 10;
m_data = new Point[m_size];
}
//Constructor with a size argument
template <class T>
Array<T>::Array(int size)
{
//Store the user input to size
m_size = size;
m_data = new Point[m_size];
}
//copy constructor :obtain the size of C array and each elements data
template <class T>
Array<T>::Array(const Array & arr)
{
// Determine the size of my_arr
m_size = arr.m_size;
// Create an array by constructor
m_data = new Point[m_size];
for (int i = 0; i < m_size; i++)
{
// Assign each data by loop
m_data[i] = arr.m_data[i];
}
cout<<"The copy constructor is called"<<endl;
}
//Destructor
template <class T>
Array<T>::~Array()
{
// Delete Array
delete [] m_data;
}
//Initialize m_default_size as 5
template <class T>
int Array<T>::m_default_size =5;
//Reset the value of m_default_size
template <class T>
int Array<T>::DefaultSize(int size) const
{
m_default_size = size;
return m_default_size;
}
//Get the value of m_default_size
template <class T>
int Array<T>::DefaultSize() const
{
return m_default_size;
}
//output the size of the array
template <class T>
int Array<T>::Size()const
{
return m_size;
}
//will set an element of our array with a given point
template <class T>
void Array<T>::SetElement(const T& pt,int index)
{
if (index > m_size-1 || index < 0)
{
//OutOfBoundsException my_error(index);
throw OutOfBoundsException(index); }
else
{
m_data[index] = pt;//Error:No viable overloaded '='
}
}
//will get an element of our array with a given point
template <class T>
const Array<T>& Array<T>::GetElement(int index)
{
if (index > m_size-1 || index < 0)
{
//For handler with int type
throw OutOfBoundsException(index);//the index was too small or too big
}
else
{
// Otherwise, return the proper one
return m_data[index];//Error:No viable conversion from 'Corneliagao::CAD::Point' to 'const Array<int>'
}
}
//operator is called for non-const objects.
template <class T>
const Array<T>& Array<T>::operator [] (int index)
{ if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
cout << "Index is out of bound,return the first element." << endl;
throw OutOfBoundsException(index);
}
else
{
return m_data[index];//Error:No viable conversion from 'Corneliagao::CAD::Point' to 'const Array<int>'
}
}
// Assignment operator
template <class T>
Array<T>& Array<T>::operator = (const Array<T>& source)
{
cout << "Copy-Assignment Operator Called!\n" << endl;
if (this == &source)
{
cout << "The same Array " << endl;
return *this;
}
delete [] m_data;
cout <<"Deleted m_data array" << endl;
m_size = source.m_size; // shallow copy - this is not dynamic alloc
if (source.m_data) // if no 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 to array
}
else
m_data = 0; //NULL
return *this;
}
// Operator to print an Array of objects, loop every object in the array and print it
template <class P>
ostream& operator << (ostream& os, const Array<P>& ar)
{
os << "Size of the Array = " << ar.m_size << endl;
for (int i = 0; i < ar.m_size; i++)
os << "Array [" << i << "]= "<< ar.m_data[i] << endl;
return os;
}
}
}
#endif
//
// Array.hpp
// Section4.2b.2
//
// Created by Cornelia on 10/16/16.
// Copyright © 2016 corneliagao. All rights reserved.
//Defination of Array class
#ifndef Array_hpp
#define Array_hpp
#include <stdio.h>
#include "Point.hpp"
using namespace std;
namespace Corneliagao{
namespace CAD{
template <class T>
class Array
{
private:
int m_size;
//Declare m_default_size as ststic variable
static int m_default_size;
public:
//dynamic C array of Point objects
Point* m_data;
//Default constructor
Array();
//Constructor with a size argument
Array(int size);
//copy constructor
Array(const Array & arr);
//Destructor
~Array();
//Reset the value of m_default_size
int DefaultSize(int size) const;
//Get the value of m_default_size
int DefaultSize() const;
//output the size of the array
int Size()const;
//will set an element of our array with a given point
void SetElement( const T& pt,int index) ;
//will output an element of our array with a given point
const Array<T>& GetElement(int index) ;
// Assignment operator.
Array<T>& operator = (const Array<T>& source);
//operator is called for non-const objects.
const Array<T>& operator [] (int index);
template<typename P>
// Operator to print an Array of objects
friend ostream& operator << (ostream& os, const Array<P>& ar);
};
}
}
#ifndef Array_cpp
#include "Array.cpp"
#endif/* Array_cpp */
//When just include header, main.cpp can't see the template definition of Array::Array() 0r Array::~Array. When include Array.cpp, it could know current transformation of Array<T>::Array() and Array<T>::~Array(). However, placing these code here is to put all of the template class code in the header file. Therefore, when include the header, all of the template code will be in one place, and main.cpp could identify them.
#endif /* Array_h */
//
// Point.hpp
// Section4.2b.2
//
// Created by Cornelia on 10/16/16.
// Copyright © 2016 corneliagao. All rights reserved.
//Defination of Point class
#ifndef Point_HPP
#define Point_HPP
#include <stdio.h>
#include <iostream>
using namespace std;
namespace Corneliagao{
namespace CAD{
class Point
{
private:
// Data members
double x;
double y;
public:
// Constructors
Point(); // Default constructor
Point(const Point &p); //Copy constructor
Point(double newx,double newy);// Initialize with x and y value
// Destructor
~Point();
// Accessing functions
double X() const;// Get the x value
double Y() const;// Get the y value
void X(double newx);// Set the x value
void Y(double newx);// Set the y value
// Member operator overloading
Point operator - () const; // Negate the coordinates.
Point operator * (double factor) const; // Scale the coordinates.
Point operator + (const Point& p) const;// Add coordinates.
bool operator == (const Point& p) const;// Equally compare operator.
Point& operator = (const Point& source);// Assignment operator.
Point& operator *= (double factor); // Scale the coordinates & assign.
double Distance()const; // Calculate the distance to the origin (0, 0).
double Distance(const Point& p)const;// Calculate the distance between two points.
// define toString()
std::string ToString()const;
// Send to ostream.Move inside the class definition and declare it as friend. The function remains a global function but it can now access the data members directly without the need for calling the getters or ToString() function.
friend ostream& operator << (ostream& os, const Point& p);
};
}
}
#endif /* Point_hpp */
//
// OutOfBoundsException.hpp
// Section4.2b.2
//
// Created by Cornelia on 10/16/16.
// Copyright © 2016 corneliagao. All rights reserved.
#ifndef OutOfBoundsException_hpp
#define OutOfBoundsException_hpp
#include <sstream>
#include <iostream>
#include <stdio.h>
#include "ArrayException.hpp"
using namespace std;
namespace Corneliagao{
namespace CAD{
class OutOfBoundsException: public ArrayException
{
private:
int m_index;
public:
//constructors
//Defalt constructor
OutOfBoundsException();
//Initialize with m_index value
OutOfBoundsException(int m_index);
//Desturctor
~OutOfBoundsException();
//overrriding function
std::string GetMessage() const;
};
inline string OutOfBoundsException::GetMessage() const //Overrides ABC member funtion.
{
stringstream errMsg;
errMsg << "Index " << m_index << " is out of bounds." << endl;
return errMsg.str();
}
}
}
#endif /* OutOfBoundsException_hpp */