65,209
社区成员
发帖
与我相关
我的任务
分享//
// NumericArray.hpp
// Section4.2b.2
//
// Created by Cornelia on 10/16/16.
//
//
#ifndef NumericArray_hpp
#define NumericArray_hpp
#include "Array.hpp"
#include <stdio.h>
#include <iostream>
using namespace std;
namespace Corneliagao{
namespace CAD{
template <typename T>
class NumericArray: public Array<T>
{
private:
public:
//Default constructor
NumericArray();
//Constructor with a size argument
NumericArray(int size);
//copy constructor
NumericArray(const NumericArray &arrin);
//Destructor
~NumericArray();
//output the size of the array
int Size();
// Assignment operator.
NumericArray<T>& operator = (const NumericArray<T>& source);
//operator +
NumericArray<T>& operator + (NumericArray<T> arrayt);
//operator*
NumericArray<T> operator *(T factor) ;
//Function DotProduct
T DotProduct( NumericArray<T> & arr1);
};
}
}
#ifndef NumericArray_cpp
#include "NumericArray.cpp"
#endif/* NumericArray_cpp */
#endif /* NumericArray_hpp */
]//
// NumericArray.cpp
// Section4.2b.2
//
// Created by Cornelia on 10/16/16.
//
//
#ifndef NumericArray_cpp
#define NumericArray_cpp
#include "NumericArray.hpp"
#include "Point.hpp"
#include "NumericArrayException.hpp"
#include <exception>
#include <sstream>
#include <iostream>
namespace Corneliagao{
namespace CAD{
// Default Constructor
template <typename T>
NumericArray<T>::NumericArray(): Array<T>(){}
//Constructor with a size argument
template <typename T>
NumericArray<T>::NumericArray(int size)
{
}
//copy constructor
template <typename T>
NumericArray<T>::NumericArray(const NumericArray &arrin): Array<T>::Array(arrin){}
//Destructor
template <typename T>
NumericArray<T>::~NumericArray(){}
//output the size of the array
template<typename T>
int NumericArray<T>::Size()
{Array<T>::size();}
//Operator =
template<typename T>
NumericArray<T> &NumericArray<T>::operator = (const NumericArray<T> &source)
{
Array<T>::operator=(source);
return *this;
}
//Operator *
template <typename T>
NumericArray<T> NumericArray<T>::operator* (T factor)
{
NumericArray<T> Temp(Array<T>::Size());
for (int i = 0; i < Array<T>::Size(); i++)
{
Temp = factor*((*this).GetElement(i));
}
return Temp;
}
//Operator +
template <typename T>
NumericArray<T>& NumericArray<T>::operator + ( NumericArray<T> arrayt)
{
NumericArray<T> Temp(Array<T>::Size());
for(int i = 0; i < Array<T>::Size(); i++)
{
Temp = (*this).GetElement(i)+ arrayt.GetElement(i);
}
return Temp;
}
//Function Dotproduct
template<typename T>
T NumericArray<T>::DotProduct( NumericArray<T> & arr1)
{
T result;
for (int i = 0; i <Array<T>::Size(); i++)
result += NumericArray<T>::GetElement(i) * (arr1.GetElement(i));
return result;
}
}
}
#endif//
// main.cpp
// Section4.2b.2
//
// Created by Cornelia on 10/16/16.
//
#include <iostream>
#include <cmath>
#include "Array.hpp"
#include "NumericArray.hpp"
//#include "Array.hpp"
//when put "#ifndef Array_cpp #include "Array.cpp" #endif" before #endif in header file, the main.cpp could still include "Array.hpp" instead of 'Array.cpp"
#include "iostream"
using namespace Corneliagao::CAD;
using namespace std;//Declaration to save "std" when use count/cin
//Assumption:type used as template argument must be numeric, it could do numeric operation, like plus, scale.
int main()
{
NumericArray<int> intArray1; //default constructor
NumericArray<int> intArray2(4); //initialize array
for (int i = 0; i < 3; i++)
{
intArray2[i] = i + 1;
cout << intArray2[i] << endl;
}
intArray1 = intArray2; //test assignment operator
for (int i = 0; i < 3; i++)
cout << intArray1[i] << endl;
NumericArray<int> intArray3(4); //initialize array
for (int i = 0; i < 3; i++)
{
intArray3[i] = i + 2;
cout << intArray3[i] << endl;
} cout << endl;
NumericArray<int> intArray4(intArray3); //test copy constructor
cout << intArray4[1] << endl << endl;
intArray4 = intArray2 * 3; //test * operator
cout << intArray4 << endl;
// test dot product.
NumericArray<int> intArray5(4);
intArray1.DotProduct(intArray3);
cout << "Dot product of intArray1 and intArray3 is : [";
for (int i = 0;i < 5;i++)
{
cout << intArray5[i] << " ";
}
cout << "]" << endl;
//test operator +
NumericArray<double> intArray6(5);
intArray6 = intArray1 + intArray2;
cout << "intArray1 plus intArray2 is : [";
for (int i = 0;i < 5;i++)
{
cout << intArray6[i] << " ";
}
cout << "]" << endl;
return 0;
}
template <typename T>
const T& Array<T>::operator [] (int index) const
{
// Out of bound error code
if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
throw OutOfBoundsException(index);
}
else
{
return m_data[index];
}
}[/quote]
这里返回的是一个 const 引用,咋能给它赋值呢 ...[/quote]
啊,那怎么调试啊,自己弄不懂了template <typename T>
const T& Array<T>::operator [] (int index) const
{
// Out of bound error code
if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
throw OutOfBoundsException(index);
}
else
{
return m_data[index];
}
}[/quote]
这里返回的是一个 const 引用,咋能给它赋值呢 ...template <typename T>
const T& Array<T>::operator [] (int index) const
{
// Out of bound error code
if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
throw OutOfBoundsException(index);
}
else
{
return m_data[index];
}
}