06.08.2012, 12:53. Показов 1038. Ответов 3
Допустим есть проект
Array_hpp
| C++ |
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
| //
// (---.Array_hpp---)
//
#ifndef Array_HPP // Preprocessor gates
#define Array_HPP
#include <sstream>
#include <iostream>
#include "ArrayException.hpp"
template <class T> //Remove the "=double" default parameter.
class Array
{
protected: // Declaration of private data members
unsigned int m_size;
T* m_data; //m_data should be a pointer, since you want to allocate data to it
public: // Public declaration of data members (in given example haven't ) and member functions
static int m_size_default;
//----------- Declaration of Constructors -----------//
Array(); // Default constructor
Array(const unsigned int new_size); // Constructor
Array(const Array<T>& ObjArray); // Copy constructor
~Array(); // Destructor
//----------- Declaration of Modificator(s) member functions -----------//
void SetElement(const T& ObjType, const unsigned int index);
static void DefaultSize( int newSize) ;
//----------- Declaration of Accessors member functions -----------//
T& GetElement(const unsigned int index) const;
static int DefaultSize() ;
int Size() const;
//----------- Declaration of Operators Overloading the class's members -----------//
Array<T>& operator = (const Array& ObjArray); //Const correctness here.
const T& operator [] (unsigned int index) const;
T& operator [] (unsigned int index);
};
#ifndef Array_cpp // Must be the same name as in source file #define
#include "Array.cpp"
#endif //#ifndef Array_cpp is CLOSED
#endif // #ifndef Array_HPP is CLOSED |
|
ArrayException_hpp
| C++ |
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
| //
// (---.ArrayException_hpp---)
//
#ifndef ArrayException_HPP
#define ArrayException_HPP
class ArrayException
{
public :
ArrayException(){};
virtual std::string GetMessage() const = 0;
};
class OutOfBoundsException : public ArrayException
{
private :
int array_index;
public :
OutOfBoundsException();
OutOfBoundsException(int index) : array_index(index) {}
std::string GetMessage() const
{
std::ostringstream os; // std::stringstream object
os << "\nEXCEPTION HANDLING ERROR: "
<< "the given element "
<< "with index : "
<< array_index
<< " is out of bounds\n " ;
return os.str();
}
};
#endif // ArrayException_H |
|
NumericArray_hpp
| C++ |
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
| //
// (---.NumericArray_hpp---)
//
#ifndef NUMERICARRAY_HPP
#define NUMERICARRAY_HPP
#include "Array.hpp"
template <class T>
class NumericArray: public Array<T>
{
public:
//----------- Declaration of Constructors -----------//
NumericArray();
NumericArray(int new_size);
~NumericArray();
//----------- Declaration of Calculator(s) (additional functionality) member(s) function -----------//
T DotProduct (const NumericArray<T> ObjFirstNumericArray ) ;
//----------- Declaration of Overloaded Operator(s) -----------//
NumericArray<T>& operator = (const NumericArray<T>& ObjNumericArray);
NumericArray<T> operator * ( double factor) const;
NumericArray<T> operator * ( const NumericArray<T>& ObjNumericArray) const;
NumericArray<T> operator + (const NumericArray<T>& ObjNumericArray) const;
// Throw an exception if the two arrays have not the same size.
};
#ifndef NumericArray_cpp
#include "NumericArray.cpp"
#endif //#ifndef NumericArray_cpp is CLOSED
#endif //NumericArray_hpp is closed |
|
Point_hpp
| C++ |
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
| //
// (---.Point_hpp---)
//
#ifndef Point_HPP // anti multiply including gates
#define Point_HPP
// Lib(s)
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
class Point
{
private: // Declaration of private data members
double x; // X coordinate
double y; // Y coordinate
public: // Public declaration of data members (in given example haven't ) and member functions
//----------- Declaration of Constructors -----------//
Point(); // Default constructor
Point(const double newX ,const double newY) ; // Constructor
explicit Point(const double value ); // Second Constructor
Point (const Point& ObjectOfClassPoint); // Copy constructor
~Point(); // Destructor
//----------- Declaration of Accessors member functions -----------//
std::string ToString() const;
double X() const;
double Y() const;
///*virtual*/ void Draw() const { std::cout << "This's Point from Draw()\n"; }
//----------- Declaration and of and Implementaion of setters member functions -----------//
void X(const double newX) {x = newX;}; // The x-coordinate
void Y(const double newY) {y = newY;}; // The y-coordinate
//----------- Declaration of Calculators (additional functionality) member functions -----------//
double Distance() const;
double Distance( const Point & ObjPoint ) const;
//----------- Declaration of Operators Overloading the class's members -----------//
Point operator - () const; // Negate the coordinates.
Point operator * (double factor) const; // Scale the coordinates.
Point operator * (const Point& ObjPoint) const; // Scale the objects .
Point operator + (const Point& ObjPoint) const; // Add coordinates.
bool operator == (const Point& ObjPoint) const; // Equally compare operator.
Point& operator = (const Point& ObjPoint); // Assignment operator.
Point& operator *= (double factor ); // Scale the coordinates & assign.
Point& operator += (const Point& ObjPoint);
Point& operator -= (const Point& ObjPoint);
//----------- Declaration of Global Ostream << Operator -----------//
friend std::ostream& operator << (std::ostream& out,const Point & ObjPoint);
};
//----------- Implementaion (inline) of getters member functions -----------//
// Given functions should be const because we not willing modify contained variables when we call those fucntions in any case
inline double Point::X() const {return x;};
inline double Point::Y() const {return y;};
#endif // Point_HPP |
|
Array_CPP
| C++ |
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
| //
// (---.Array_CPP---)
//
#ifndef Array_CPP // Preprocessor gates
#define Array_CPP
#include "Array.hpp"
template <class T>
int Array<T>::m_size_default = 10;
//----------- Implementation of Constructors -----------//
template <class T>
Array<T>::Array() : m_size(10), m_data(new T[m_size])
{
// std::cout << "Array Default Constructor was called " ;
}
template <class T>
Array<T>::Array(const unsigned int new_size) : m_size(new_size), m_data(new T[new_size])
{ //specified by the size input argument
// std::cout << "Array Constructor was called ";
}
template <class T>
Array<T>::Array(const Array& ObjArray) : m_data(0), m_size(0)
{
// std::cout << "Array copy Constructor ";
m_size = ObjArray.m_size; // shallow copy - this is not dynamic alloc
if (ObjArray.m_data) // if not zeros then there is a ref. - Deep copy needed
{
m_data = new T[ObjArray.m_size];
for (int i = 0; i < ObjArray.m_size; i++)
m_data[i] = ObjArray.m_data[i];
}
else
m_data = 0; //NULL
}
template <class T>
Array<T>::~Array()
{
// std::cout << "Array Destructor was called\n" ;
delete [] m_data;
}
//----------- Implementaion of Modificator(s) member(s) function -----------//
template <class T>
void Array<T>::SetElement(const T& ObjType,unsigned int index)
{
std::cout << "Set Element[" << index << "]<- ";
if (index >= m_size || index < 0 )
{
std::cout << " Data wasn't setted";
throw OutOfBoundsException(index);
}
m_data[index] = ObjType;
}
template <typename T>
void Array<T>::DefaultSize( int newSize)
{
m_size_default = newSize;
}
//----------- Implementation of Accessor(s) member functions -----------//
template <class T>
T& Array<T>::GetElement(const unsigned int index) const
{
std::cout << "GetElement[" << index <<"] ->" ;
if (index >= m_size || index < 0)
{
std::cout << " Data wasn't getted";
throw OutOfBoundsException(index);
}
return m_data[index];
}
template <typename T>
int Array<T>::DefaultSize()
{
return m_size_default;
}
template<class T>
int Array<T>::Size() const
{
return this->m_size;
}
//----------- Implementation of Operators Overloading the class's member -----------//
template <class T>
Array<T>& Array<T>::operator = (const Array& ObjArray)
{
// std::cout << " Array Assignment operator\n";
if (this == &ObjArray)
{
//std::cout << "Same Array \n";
return *this;
}
delete [] m_data;
// std::cout << "Deleted m_data array\n";
m_size = ObjArray.m_size; // shallow copy - this is not dynamic alloc
if (ObjArray.m_data) // if not zeros then there is a ref.
{
// std::cout <<"im here\n";
m_data = new T[ObjArray.m_size]; // create a new pointee.
for (unsigned int i = 0; i < ObjArray.m_size; i++)
m_data[i] = ObjArray.m_data[i]; //copy the points from/to array
}
else
m_data = 0; //NULL
return *this;
}
template <class T>
T& Array<T>::operator [] (unsigned int index)
{
//std::cout << "Array [] operator" << std::endl;
if (index >= this->m_size)
{
throw OutOfBoundsException(index);
}
return m_data[index];
}
template <class T>
const T& Array<T>::operator [] (unsigned int index) const
{
//std::cout << "const Array [] operator" << std::endl;
if (index > this-> m_size)
{
throw OutOfBoundsException(index);
}
return m_data[index];
}
#endif //Array_CPP |
|
NumericArray_cpp
| C++ |
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
| //
// (---.NumericArray_cpp---)
//
#ifndef NumericArray_CPP
#define NumericArray_CPP
#include "NumericArray.hpp"
template <class T>
NumericArray<T>::NumericArray():Array()
{
}
template <class T>
NumericArray<T>::NumericArray( int new_size): Array( new_size)
{
}
template <class T>
NumericArray<T>& NumericArray<T>::operator = (const NumericArray<T>& ar)
{
Array<T>::operator = (ar); // call base class assignment operator
return *this;
}
template <class T>
NumericArray<T>::~NumericArray()
{
//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 T>
NumericArray<T> NumericArray<T>::operator *( double factor) const
{
NumericArray<T> output(Array<T>::Size());
for(int i=0; i<Size(); i++)
{
output[i] = (*this)[i] * factor;
//return output;
}
return output;
}
template<class T>
NumericArray<T> NumericArray<T>::operator + (const NumericArray<T>& ObjNumericArray) const
{
NumericArray<T> output(Array<T>::Size());
for(int i=0; i<Size(); i++)
{
output[i] = (*this)[i] + ObjNumericArray[i];
}
return output;
}
template<class T>
NumericArray<T> NumericArray<T>::operator * ( const NumericArray<T>& ObjNumericArray) const
{
NumericArray<T> output(Array<T>::Size());
for(int i=0; i<Size(); i++)
{
output[i] = (*this)[i] * ObjNumericArray[i];
}
return output;
}
template<class T>
T NumericArray<T>::DotProduct (const NumericArray<T> ObjNumericArray )
{
NumericArray<T> output(Array<T>::Size());
T ObjType;
for(int i=0; i<Size(); i++)
{
output[i] = (*this)[i] * ObjNumericArray[i];
ObjType += output[i];
}
return ObjType;
}
#endif //NumericArray_cpp is closed |
|
Point_cpp
| C++ |
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
| //
// (---.Point_cpp---)
//
#include "Point.hpp"
int number_of_point_constructor_calls = 1 ;
int number_of_point_destructor_calls = 1 ;
int number_of_point_assignment_operator_calls = 1 ;
//----------- Implementation of Constructors -----------//
Point::Point() : x(0) , y(0) // Default constructor (implemented using colon syntax )
{
// std::cout << "hi my default constructor\n\n\t";
}
Point::Point(const double newX ,const double newY) : x(newX) , y(newY) // Constructor
{
// std::cout << " My Point constructor was called " << number_of_point_constructor_calls++ << " times " << "\n";
}
Point::Point(const double value ) : x(value) // Second Constructor
{
//std::cout << "hi my constructor\n\n\t";
}
Point::~Point() // Destructor
{
// std::cout << " My Point destructor was called " << number_of_point_destructor_calls++ << " times " << "\n";
}
Point::Point (const Point& ObjectOfClassPoint) // Copy constructor
{
//std::cout << "this is COPY constructor\n\n\t ";
x = ObjectOfClassPoint.x;
y = ObjectOfClassPoint.y;
}
//----------- Implementation of Accessor(s) member functions -----------//
std::string Point::ToString() const
{
// std::string s=Shape::ToString();
std::ostringstream os; // std::stringstream object
os << " Point (" << x << ", " << y << ") " ; // customization of output and calling base class functuanality
return os.str(); // str() function retrieve the string from the string buffer
}
//----------- Implementation of Calculators (additional functionality) member functions -----------//
double Point::Distance() const
{
return std::sqrt(pow(x,2) + pow(y,2));
}
double Point::Distance(const Point &ObjPt ) const
{
return std::sqrt(pow((x-ObjPt.x),2) + pow(y - ObjPt.y,2));
}
//----------- Implementation of Operators Overloading the class's member -----------//
Point Point::operator - () const // Negate the coordinates.
{
return Point(-x,-y);
}
Point Point::operator * (double factor) const // Scale the coordinates.
{
return Point(x * factor, y * factor);
}
Point Point::operator * (const Point& ObjPoint) const // Scale the objects .
{
return Point(x * ObjPoint.x, y * ObjPoint.y);
}
Point Point::operator + (const Point& ObjPoint) const // Add coordinates.
{
return Point(x + ObjPoint.x, y + ObjPoint.y);
}
bool Point::operator == (const Point& ObjPoint) const // Equally compare operator.
{
return (x==ObjPoint.x)&(y==ObjPoint.y);
}
Point& Point::operator = (const Point& ObjPoint) // Assignment operator.
{
// std::cout << " My Point assignment operator was called " << number_of_point_assignment_operator_calls++ << " times " << "\n";
x=ObjPoint.x, y=ObjPoint.y;
return *this; // The meaning of *this explained in this example (implementation) see comment out
}
Point& Point::operator *= (double factor) // Scale the coordinates & assign.
{
x *= factor;
y *= factor;
return *this ;
}
Point& Point::operator += (const Point& ObjPoint)
{
x += ObjPoint.x;
y += ObjPoint.y;
return *this;
}
Point& Point::operator -= (const Point& ObjPoint)
{
x -= ObjPoint.x;
y -= ObjPoint.y;
return *this;
}
//----------- Implementation of GLOBAL Ostream << Operator -----------//
std::ostream& operator << (std::ostream& out,const Point & ObjPoint)
{
return out << "[" << ObjPoint.x << "," << ObjPoint.y << "]" ;
} |
|
main_cpp
| C++ |
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
| //
// (---.main_cpp---)
//
#include "Point.hpp"
#include "Array.cpp"
#include "NumericArray.cpp"
#include <iostream>
int main()
{
//Create two Point arrays and test the operators
NumericArray<Point> pArray1(5);
NumericArray<Point> pArray2(5);
//Array<Point> PointArray (5);
//initialize
//for (int i=0; i!=PointArray.Size(); i++ ) PointArray[i] = Point (2*i,2*i);
for(int i=0; i<pArray1.Size(); i++) pArray1[i] = Point(i, i);
for(int i=0; i<pArray2.Size(); i++) pArray2[i] = Point(7*i, 4*i);
//Numeric Array's operations not working for Point objects
NumericArray<Point> PointArray(5);
PointArray = pArray1 * 34 ;
for(int i=0; i<PointArray.Size(); i++)
{
std::cout << PointArray[i] << std::endl;
}
std::cout << "\nTEST of + operator. PointArray + pArray2 \n" << std::endl;
std::cout << "\n data both arrays \n" << std::endl;
for(int i=1; i<PointArray.Size(); i++)
{
std::cout << pArray2[i] <<" + " << "" << PointArray[i] << std::endl;
}
PointArray = PointArray + pArray2;
std::cout << "\n NEW PointArray Array \n" << std::endl;
for(int i=1; i<PointArray.Size(); i++)
{
std::cout << PointArray[i] << std::endl;
}
std::cout << "\nTEST of * two NumericArray pArray1 pArray2 \n" << std::endl;
NumericArray<Point> PointArray1(5);
PointArray1 = pArray1*pArray2;
std::cout << "\n NEW2 PointArray Array \n" << std::endl;
for(int i=0; i<PointArray.Size(); i++)
{
std::cout << pArray1[i] <<"*"<<pArray2[i]<<"=" << PointArray1[i] << std::endl;
}
std::cout << "\nTEST DotProduct \n" << std::endl;
std::cout << "DOT PRODUCT : " << pArray1.DotProduct(pArray2);
// std::cout << "DOT PRODUCT : " ;
pArray1.DotProduct(pArray2);
std::cout << "\n\n\n";
std::cout << "\n\n\n";
return 0;
} |
|
Допустим у класса Array есть копи констрактор
| C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| template <class T>
Array<T>::Array(const Array& ObjArray) : m_data(0), m_size(0)
{
// std::cout << "Array copy Constructor ";
m_size = ObjArray.m_size; // shallow copy - this is not dynamic alloc
if (ObjArray.m_data) // if not zeros then there is a ref. - Deep copy needed
{
m_data = new T[ObjArray.m_size];
for (int i = 0; i < ObjArray.m_size; i++)
m_data[i] = ObjArray.m_data[i];
}
else
m_data = 0; //NULL
}
template <class T>
Array<T>::~Array()
{
// std::cout << "Array Destructor was called\n" ;
delete [] m_data;
} |
|
Начал переходить с одной имплементации Array на другую смотрю компилятор ругается на не знакомую ошибку какую то мол m_size bla bla inicialaze list (ща компелятор уже по другому ругается к сожалению ) и лайн на копи констрактор выдает
Открыл бек ап оказалось что ошибка заключалось в отсутствии вот этого
Array<T>::Array(const Array& ObjArray)
: m_data(0), m_size(0)
Ну допустим окей правила игры такие при создании копи констрактора но мне смущает что там нули стоят (как то криво смотрится ). Ребят подскажи кто на самом деле там должно быть а ля как в конструкторе типа :
| C++ |
1
| : m_size(new_size), m_data(new T[new_size]) |
|
или как в дефолтном конструкторе
| C++ |
1
| : m_size(10), m_data(new T[m_size]) |
|
или все таки нули должны быть ????
Добавлено через 11 минут
Не что то я попутал... похоже что эта хрень (
: m_data(0), m_size(0)) не нужна вообще... или я ошибаюсь ???