Files
Avancerad_CPP/Lab2/array.h
2026-03-05 13:16:26 +01:00

131 lines
4.3 KiB
C++

//##################################################################
// LAB 2
// ADVANCED PROGRAMMING IN C++
// MATRIX/ARRAY WITH SHALLOW COPY
//==================================================================
// array.h
// CLASS IMPLEMENTATION AND DEFINITION FOR
// THE ARRAY
// Christian Ohlsson
// Daniel Alfredsson
// Karlstad 990907
//==================================================================
#include <iostream.h>
#include "contract.h"
class Array {
private:
Contract *myContract;
public:
~Array();
Array(const int s=0);
Array(const Array &src);
const Array operator + (const Array &src);
void operator = (const Array &src);
const int &operator [] (int index) const;
int &operator [] (int index);
int getSize() const;
};
//################################################################
// Constructor
// Creates a new Array
//================================================================
Array::Array(const int s) {
myContract = new Contract();
myContract->size = s;
myContract->buffer = new int[s];
}
//################################################################
// Constructor
// Copys an array
//================================================================
Array::Array(const Array &src) {
src.myContract->owners++;
myContract = src.myContract;
}
//################################################################
// Operator overload
// Allows to assign a Array to another
//================================================================
void Array::operator = (const Array &src) {
if(myContract->owners>1) {
myContract->owners--;
myContract = new Contract;
}
else
delete [] myContract->buffer;
myContract->size = src.myContract->size;
myContract->buffer = new int[src.myContract->size];
for(int i=0 ; i<src.myContract->size ; i++)
myContract->buffer[i] = src.myContract->buffer[i];
}
//################################################################
// Operator overload
// Returns a element in the Array
//================================================================
const int &Array::operator [] (int index) const{
return (myContract->buffer[index]);
}
//################################################################
// Operator overload
// Returns a element in the Array
//================================================================
int &Array::operator [] (int index) {
if(myContract->owners>1) {
myContract->owners--;
Contract *tmp = new Contract;
tmp->size = myContract->size;
tmp->buffer = new int[tmp->size];
for(int i=0 ; i<tmp->size ; i++)
tmp->buffer[i] = myContract->buffer[i];
}
return (myContract->buffer[index]);
}
//################################################################
// Operator overload
// Allows to add a Array to another
//================================================================
const Array Array::operator + (const Array &src) {
Array tmp(myContract->size);
for(int i=0 ; i<myContract->size ; i++)
tmp.myContract->buffer[i] = myContract->buffer[i] + src.myContract->buffer[i];
return (tmp);
}
//################################################################
// Destructor
// Deallocates memory, used by the Array
//================================================================
Array::~Array() {
myContract->owners--;
if(myContract->owners == 0) {
delete [] myContract->buffer;
delete myContract;
}
}
//################################################################
// Operator overload
// Allows input in the Array, using STDIN
//================================================================
istream &operator >> (istream &is, Array &a) {
for(int l=0 ; l<a.getSize() ; l++)
is >> a[l];
return is;
}
//################################################################
// Operator overload
// Prints out an Array, using STDOUT
//================================================================
ostream &operator << (ostream &os, const Array &a) {
for(int k=0 ; k<a.getSize() ; k++)
os << a[k];
return os;
}
//################################################################
// Function getSize()
// Returns the size of the Array
//================================================================
int Array::getSize() const{
return myContract->size;
}