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
|
template <class Type> //Remove the "=double" default parameter.
class Array
{
protected:
int m_size;
Type* m_data; //m_data should be a pointer, since you want to allocate data to it
public:
Array();
Array(int new_size);
~Array(); //Don't make your destructor virtual. There is no reason to do it.
Array<Type>& operator=(const Array& ar); //Const correctness here.
//Type& operator * (double factor) const;
Array operator *(double factor) const; // я это имел ввиду (про header)
Type& operator [] (int index);
const Type& operator [] (int index) const;
int Size() const;
};
//****************
//****************
//****************
#include <sstream>
#include <iostream>
using namespace std;
class Point
{
private:
double m_x;
double m_y;
public:
// Constructors
Point(): m_x(0), m_y(0) {};
Point(double new_x, double new_y) : m_x(new_x), m_y(new_y) {};
friend ostream& operator << (ostream& os, const Point& point)
{
return os << point.ToString();
}
//std::string Point::ToString(void) const // create a string representation of a point
string ToString() const
{
// create a string like: “Point(1.5, 3.9)”
std::ostringstream os;
os << m_x << " , " << m_y;
std::string double_string = os.str();
return "Point(" + double_string + ")";
}
//Point Point::operator * (double factor) const;
Point operator *(double factor) const; // итак в классе, Point:: не нужно
//Point& Point::operator *= (double factor);
Point & operator *=(double factor);
};
//****************
//****************
//****************
//array.cpp
//#include "Array.h"
//#include <sstream>
//#include <iostream>
#include <exception>
//using namespace std;
//#ifndef Array_CPP
//#define Array_CPP
template <class Type>
Array<Type>::Array() : m_size(10), m_data(0) // странно получается, размер 10, а данных нет
{
}
template <class Type>
Array<Type>::Array(int new_size) : m_size(new_size), m_data(new Type[new_size])
{
}
template <class Type>
Array<Type>::~Array()
{
//Technically, the if is not necessary
if(m_data)
{
delete[] m_data;
m_data = 0;
}
//Not necessary either, but just to be clean
m_size = 0;
}
template <class Type>
Array<Type>& Array<Type>::operator=(const Array& ar)
{
//Check self assign:
if(this == &ar) {return *this;}
Array<Type> copy(ar); //Create a copy of ar; If this fails, then *this will not be changed, and nothing will leak
//Succeeded creating copy. Now we can put it inside this
this->Swap(copy); //And then swap the copy into this!
return *this;
}
template <class Type>
Type& Array<Type>::operator [] (int index)
{
//cout << "Array [] operator" << endl;
if (index > this->m_size)
{
cout << "i am hreeeee" << endl;
return this->m_data[0];
}
return m_data[index];
}
template <class Type>
const Type& Array<Type>::operator [] (int index) const
{
// cout << "Array [] operator" << endl;
if (index > this->m_size)
{
cout << "i am hreeeee" << endl;
return this->m_data[0];
}
return m_data[index];
}
template<class Type>
int Array<Type>::Size() const
{
return this->m_size;
}
template<class Type>
//Type& Array<Type>::operator * (double factor) const
Array<Type> Array<Type>::operator *(double factor) const
{
Array<Type> output(Array<Type>::Size());
for(int i=0; i<Array::Size(); i++)
{
output[i] = (*this)[i] * factor;
//return output;
}
return output;
}
//****************
//****************
//****************
//#endif //Array_CPP
//#include "array.h"
//#include "Point.h"
//#include <sstream>
//#include <iostream>
//using namespace std;
Point Point::operator * (double factor) const
{
return Point(m_x * factor, m_y * factor);
}
Point& Point::operator *= (double factor)
{
Point tmp = (*this) * factor;
*this = tmp;
return *this;
}
//****************
//****************
//****************
//main.cpp
// #include "point.h"
// #include <iostream>
// #include "array.cpp"
// using namespace std;
int main()
{
//Create two Point arrays and test the operators
Array<Point> pArray1(5);
Array<Point> pArray2(5);
//initialize
for(int i=0; i<pArray1.Size(); i++) pArray1[i] = Point(i, i);
for(int i=0; i<pArray2.Size(); i++) pArray2[i] = Point(2*i, 2*i);
//Numeric Array's operations not working for Point objects
cout << "times PointArray1 by 3 and print out the new array: "<< endl;
Array<Point> answ1 = pArray1 * 3;
for(int i=0; i<answ1.Size(); i++){
cout << answ1[i] << endl;
}
}
|