|
-
June 19th, 2012, 10:36 AM
#1
generic inheritance
Very simple idea:
There was <template> Array that take class Point, multiply to a factor and print this bunch of objects of class Point
Now I try to split Array to two template Array and NumericArray.
NumericArray derived only multiplication operator...
Pretty simple... but (((( 
1>------ Build started: Project: HP3_4.2b_ex2_with_inheritance, Configuration: Release Win32 ------
1> NumericArray.cpp
1>c:\all my\с++\ha level 6\solution\level 6\hp3_4.2b_ex2_with_inheritance\NumericArray.h(7): error C2143: syntax error : missing ',' before '<'
1> c:\all my\с++\ha level 6\solution\level 6\hp3_4.2b_ex2_with_inheritance\NumericArray.h(15) : see reference to class template instantiation 'NumericArray<Type>' being compiled
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
h(7) and h(15) correspond open and close bracket of class
my code :
Code:
//array.h
#ifndef Array_H
#define Array_H
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; //
Type& operator [] (int index);
const Type& operator [] (int index) const;
int Size() const;
};
#ifndef Array_cpp
#include "array.cpp"
#endif
#endif //Array_CPP
//*******************
//*******************
//*******************
//*******************
//NumericArray.h
#ifndef NumericArray_H
#define NumericArray_H
template <class Type> //Remove the "=double" default parameter.
class NumericArray: public Array<Type>
{
public:
NumericArray();
NumericArray(int new_size);
~NumericArray(); //Don't make your destructor virtual. There is no reason to do it.
//NumericArray<Type>& operator=(const Array& ar); //Const correctness here.
NumericArray operator *(double factor) const; //
};
#endif
//*******************
//*******************
//*******************
//*******************
//point.h
#include "array.h"
#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::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
//*******************
//*******************
//*******************
//*******************
//NumericArray.cpp
#include "NumericArray.h"
#include <sstream>
#include <iostream>
#include <exception>
using namespace std;
#ifndef NumericArray_CPP
#define NumericArray_CPP
template <class Type>
NumericArray<Type>::NumericArray() : m_size(10), m_data(0) // странно получается, размер 10, а данных нет
{
}
template <class Type>
NumericArray<Type>::NumericArray(int new_size) : m_size(new_size), m_data(new Type[new_size])
{
}
template <class Type>
NumericArray<Type>::~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 Type>
NumericArray<Type> NumericArray<Type>::operator *(double factor) const
{
NumericArray<Type> output(NumericArray<Type>::Size());
for(int i=0; i<Array::Size(); i++)
{
output[i] = (*this)[i] * factor;
//return output;
}
return output;
}
#endif //NumericArray_CPP
//*******************
//*******************
//*******************
//*******************
#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"
#include <exception>
#include "NumericArray.h"
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;
}
}
//*******************
//*******************
//*******************
//*******************
Many many thanks in advance !
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|