|
|
|
| Complex |
Description: Complex Numbers
|
| Triangle |
Description: Triangle
|
| Vector |
Description: Templatized class - Safe arrays for any data type
|
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <assert.h>
#include <iomanip>
const maxsize = 1000;
Template <class datatype> class Vector {
private:
int size; //% represents current size of array
datatype DATA[maxsize]; //% represents max # of elements
public:
Vector(int size2 = 0); //% default set to 0
Vector(Vector<datatype> &x); //% copy constructor
int length(void); //% returns size
void resize(int newsize); //% set size to newsize
datatype &operator [] (int n); //% w/ range checking
Vector<datatype> &operator = (Vector<datatype> x); // asgn operator
Vector<datatype> &operator += (Vector<datatype> x); //
}; //class Vector
//%%%%%%%%%%%%%%%%&&&&&&&%%%%%%%%%%%%%%%%%//
//%%%CLASS Vector FUNCTION DEFINITIONS%%%//
//%%%%%%%%%%%%%%%%%%%%%%%&&&&&&&%%%%%%%%%%//
//
Template <class datatype>
Vector<datatype>::Vector<datatype>(int size2) {
size = size2;
}
//
Template <class datatype>
Vector<datatype>::Vector<datatype>(Vector<datatype> &x) {
size = x.length();
for(int k=0; k < size; k++)
DATA[k] = x[k];
}
//
Template <class datatype>
int Vector<datatype>::length(void) {
return size;
}
//
Template <class datatype>
void Vector<datatype>::resize(int newsize) {
assert(newsize <= maxsize);
size = newsize;
}
//
Template <class datatype>
datatype &Vector<datatype>::operator [] (int n) {
if (n >= 0 && n < size) return DATA[n];
//else
cout << "Out of range error! "<< endl;
abort();
}
//
Template <class datatype>
Vector<datatype> &Vector<datatype>::operator = (Vector<datatype> x) {
resize(x.length());
for( int i = 0; i < size; i ++)
DATA[i] = x[i];
return *this;
}
//
Template <class datatype>
Vector<datatype> &Vector<datatype>::operator += (Vector<datatype> x) {
int origsize = size;
size+=x.size;
for(int i = origsize; i < size ; i++)
DATA[i] = x[i-origsize];
return *this;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%END Vector FCN DEFINITIONS%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%