first commit
This commit is contained in:
106
Lab1/array.h
Normal file
106
Lab1/array.h
Normal file
@@ -0,0 +1,106 @@
|
||||
//##################################################################
|
||||
// LAB 1
|
||||
// ADVANCED PROGRAMMING IN C++
|
||||
// MATRIX/ARRAY WITH DEEP COPY
|
||||
//==================================================================
|
||||
// array.h
|
||||
// CLASS IMPLEMENTATION AND DEFINITION FOR
|
||||
// THE ARRAY
|
||||
// Christian Ohlsson
|
||||
// Daniel Alfredsson
|
||||
// Karlstad 990907
|
||||
//==================================================================
|
||||
|
||||
#include <iostream.h>
|
||||
class Array {
|
||||
private:
|
||||
int size; //The size of the Array
|
||||
int *buffer; //Pointer to an int-buffer
|
||||
public:
|
||||
~Array();
|
||||
Array(const int s=0);
|
||||
Array(const Array &src);
|
||||
void operator = (const Array &src);
|
||||
Array Array::operator +(const Array &src);
|
||||
int &operator [] (int index) const;
|
||||
int getSize() const;
|
||||
};
|
||||
//################################################################
|
||||
// Constructor
|
||||
// Creates a new Array
|
||||
//================================================================
|
||||
Array::Array(const int s=0) {
|
||||
buffer = new int[s];
|
||||
size= s;
|
||||
}
|
||||
//################################################################
|
||||
// Constructor
|
||||
// Copys an array
|
||||
//================================================================
|
||||
Array::Array(const Array &src) {
|
||||
size = src.size;
|
||||
buffer = new int[size];
|
||||
for(int i=0 ; i<size ; i++)
|
||||
buffer[i] = src.buffer[i];
|
||||
}
|
||||
//################################################################
|
||||
// Operator overload
|
||||
// Allows to assign a Array to another
|
||||
//================================================================
|
||||
void Array::operator = (const Array &src) {
|
||||
delete [] buffer;
|
||||
size = src.size;
|
||||
buffer = new int[size];
|
||||
for(int i=0 ; i<size ; i++)
|
||||
buffer[i] = src.buffer[i];
|
||||
}
|
||||
//################################################################
|
||||
// Operator overload
|
||||
// Returns a element in the Array
|
||||
//================================================================
|
||||
int &Array::operator [] (int index) const{
|
||||
return buffer[index];
|
||||
}
|
||||
//################################################################
|
||||
// Operator overload
|
||||
// Allows to add a Array to another
|
||||
//================================================================
|
||||
Array Array::operator + (const Array &src) {
|
||||
Array tmp(size);
|
||||
for(int i=0 ; i<size ; i++)
|
||||
tmp.buffer[i] = buffer[i] + src.buffer[i];
|
||||
return (tmp);
|
||||
}
|
||||
//################################################################
|
||||
// Destructor
|
||||
// Deallocates memory, used by the Array
|
||||
//================================================================
|
||||
Array::~Array() {
|
||||
delete [] buffer;
|
||||
}
|
||||
//################################################################
|
||||
// Operator overload
|
||||
// Allows input in the Array, using STDIN
|
||||
//================================================================
|
||||
istream &operator >> (istream &is, Array &v) {
|
||||
for(int l=0 ; l<v.getSize() ; l++)
|
||||
is >> v[l];
|
||||
return is;
|
||||
}
|
||||
//################################################################
|
||||
// Operator overload
|
||||
// Prints out an Array, using STDOUT
|
||||
//================================================================
|
||||
ostream &operator << (ostream &os, const Array &v) {
|
||||
for(int k=0 ; k<v.getSize() ; k++)
|
||||
os << v[k];
|
||||
return os;
|
||||
}
|
||||
//################################################################
|
||||
// Function getSize()
|
||||
// Returns the size of the Array
|
||||
//================================================================
|
||||
int Array::getSize() const{
|
||||
return size;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user