diff --git a/Lab2/lab2 b/Lab2/lab2 new file mode 100755 index 0000000..13132d0 Binary files /dev/null and b/Lab2/lab2 differ diff --git a/Lab2/main.cpp b/Lab2/main.cpp new file mode 100644 index 0000000..7f4c05e --- /dev/null +++ b/Lab2/main.cpp @@ -0,0 +1,29 @@ +/** + * @file main.cpp + * @author Trevor Barnes (barnestr@msoe.edu) + * @brief + * @version 1.0 + * @date 2022-03-22 + * + * @copyright Copyright (c) 2022 + * + */ +#include +#include +#include "row.h" +using namespace std; + +int main() { + int length; + cout << "Enter desired length for row matrix:" << endl; + cin >> length; + Row row1(length); + for(int i = 0; i < length; i++){ + cout << "before seg fault" << endl; + // Will use access operator instead + cout << row1.row_data[i] << " "; + } + cout << endl; + + return 0; +} diff --git a/Lab2/makefile b/Lab2/makefile new file mode 100644 index 0000000..d048656 --- /dev/null +++ b/Lab2/makefile @@ -0,0 +1,25 @@ +# lab2 Makefile + +CC = g++ +CFLAGS = -c -MMD +LFLAGS = +# Change w/ every new project +SOURCES = main.cpp row.cpp +OBJECTS = $(SOURCES:.cpp=.o) +# Change w/ every new project +EXECUTABLE = lab2 + +all: $(EXECUTABLE) $(SOURCES) + +$(EXECUTABLE): $(OBJECTS) + $(CC) $(LFLAGS) -o $@ $(OBJECTS) + +-include *.d + +%.o:%.cpp + $(CC) $(CFLAGS) $< + +clean: + rm -f $(EXECUTABLE) + rm -f $(OBJECTS) + rm -f *.d \ No newline at end of file diff --git a/Lab2/row.cpp b/Lab2/row.cpp new file mode 100644 index 0000000..e44dad5 --- /dev/null +++ b/Lab2/row.cpp @@ -0,0 +1,68 @@ +#include +#include "row.h" +using namespace std; + +// parameterized constructor +Row::Row(unsigned int length){ + cout << "-Row Constructor-" << endl; // TODO: temp + // Set matrix length private variable to passed in length + this->length = length; + // Create new array in heap for row_data + this->row_data = new double[length]; + // Clear all values in new array to 0 + for(int i = 0; i < length; i++) { + cout << "-Row Constructor-" << endl; // TODO: temp + this->row_data[i] = 0.0; + } + // finish +} + +// copy constructor +Row::Row(const Row& from){ + cout << "-Row Copy Constructor-" << endl; // TODO: temp + // New row matrix gets length from previous matrix + this->length = from.length; + // Create new array in heap with new length + this->row_data - new double[this->length]; + // Copy all row_data values over to new array + for(int i = 0; i < this->length; i ++) { + this->row_data[i] = from.row_data[i]; + } + // finish +} + +// destructor +Row::~Row(){ + cout << "-Row Destructor-" << endl; // TODO: temp + // Check for valid length then free the heap memory + if(this->length > 0 ) { + delete[] row_data; + } + //finish +} + +// access operator (const) +double Row::operator[](unsigned int column) const{ + //finish + double result; + return result; +} + +// access operator (non-const) +double& Row::operator[](unsigned int column){ + //finish + double result; + return result; +} + +// assignment operator +Row& Row::operator= (const Row& rhs){ + //finish + Row result(1); + return result; +} + +// clear row data +void Row::clear(){ + //finish +} \ No newline at end of file diff --git a/Lab2/row.h b/Lab2/row.h new file mode 100644 index 0000000..a765ede --- /dev/null +++ b/Lab2/row.h @@ -0,0 +1,51 @@ +#ifndef row_h +#define row_h +class Row{ + public: + /* Parameterized constructor + * Takes in length and creates a row matrix with values cleared + * to zero + */ + Row(unsigned int length); + + /* Copy constructor + * Create a new row matrix with the same size and values as the + * from matrix + */ + Row(const Row& from); + + /* Destructor + * Correctly delete any heap memory + */ + ~Row(); + + /* Access operator (const version) + * Allow access to row matrix data + * Should return an exception if column is too large + */ + double operator[](unsigned int column) const; + + /* Access operator (non const version) + * Allow access to row matrix data + * Should return an exception if column is too large + */ + double& operator[] (unsigned int column); + + /* Assignment operator + * 1. Check if two sides are the same object + * 2. Delete the current row matrix + * 3. Create a new row matrix with the same size and values as + * the rhs matrix + */ + Row& operator= (const Row& rhs); + + /* Clear all data values to zero + */ + void clear(); + private: + // Row matrix data + double * row_data; + // Size of row matrix + unsigned int length; +}; +#endif