repo update

This commit is contained in:
2022-04-04 15:41:31 -05:00
parent 1156c003f9
commit 16003d1715
20 changed files with 13101 additions and 13046 deletions

74
.gitignore vendored
View File

@@ -1,38 +1,38 @@
# Prerequisites # Prerequisites
*.d *.d
# Compiled Object files # Compiled Object files
*.slo *.slo
*.lo *.lo
*.o *.o
*.obj *.obj
# Precompiled Headers # Precompiled Headers
*.gch *.gch
*.pch *.pch
# Compiled Dynamic libraries # Compiled Dynamic libraries
*.so *.so
*.dylib *.dylib
*.dll *.dll
# Fortran module files # Fortran module files
*.mod *.mod
*.smod *.smod
# Compiled Static libraries # Compiled Static libraries
*.lai *.lai
*.la *.la
*.a *.a
*.lib *.lib
# Executables # Executables
*.exe *.exe
*.out *.out
*.app *.app
# Other # Other
/.vscode /.vscode
*.pdf *.pdf
*.zip *.zip
*.code-workspace *.code-workspace

View File

@@ -1,188 +1,188 @@
/** /**
* @file ReadSTL.cpp * @file ReadSTL.cpp
* @author Trevor Barnes (barnestr@msoe.edu) * @author Trevor Barnes (barnestr@msoe.edu)
* @brief A C++ file containing the functionality need to read in a .stl file. * @brief A C++ file containing the functionality need to read in a .stl file.
* The ReadSTL class allows the min/max coordinates and total count of the STL * The ReadSTL class allows the min/max coordinates and total count of the STL
* facets be stored. The appropriate getters are also included. * facets be stored. The appropriate getters are also included.
* @version 1.1 * @version 1.1
* @date 2022-03-15 * @date 2022-03-15
* *
* @copyright Copyright (c) 2022 * @copyright Copyright (c) 2022
* *
*/ */
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
using namespace std; using namespace std;
class ReadSTL { class ReadSTL {
private: private:
int numFacet; int numFacet;
double Xmin; double Xmin;
double Xmax; double Xmax;
double Ymin; double Ymin;
double Ymax; double Ymax;
double Zmin; double Zmin;
double Zmax; double Zmax;
public: public:
ReadSTL(string filename) { ReadSTL(string filename) {
// Instantiate variables // Instantiate variables
numFacet = 0; numFacet = 0;
// Input Stream setup // Input Stream setup
ifstream inputfile(filename, ifstream::in); ifstream inputfile(filename, ifstream::in);
string line; string line;
string token; string token;
string solidName; string solidName;
// Solid header line // Solid header line
getline(inputfile, line); getline(inputfile, line);
// Input string stream for each line // Input string stream for each line
istringstream lineStream(line); istringstream lineStream(line);
// Iterate stream past "solid" // Iterate stream past "solid"
lineStream >> token; lineStream >> token;
// Get solid name // Get solid name
lineStream >> solidName; lineStream >> solidName;
do { do {
// Process entire solid, looping through each facet // Process entire solid, looping through each facet
// Get next line // Get next line
getline(inputfile, line); getline(inputfile, line);
// Set string stream to new line // Set string stream to new line
lineStream.str(line); lineStream.str(line);
// Clear sstream error state // Clear sstream error state
lineStream.clear(); lineStream.clear();
// Get first word in line // Get first word in line
lineStream >> token; lineStream >> token;
if(!token.compare("facet")) { if(!token.compare("facet")) {
// Facet header // Facet header
// Iterate past "normal" // Iterate past "normal"
lineStream >> token; lineStream >> token;
// No additional functionality required for facet normal vectors // No additional functionality required for facet normal vectors
// Normal X // Normal X
lineStream >> token; lineStream >> token;
// Normal Y // Normal Y
lineStream >> token; lineStream >> token;
// Z Normal // Z Normal
lineStream >> token; lineStream >> token;
// End facet header // End facet header
do { do {
// Get next line // Get next line
getline(inputfile, line); getline(inputfile, line);
// Set string stream to new line // Set string stream to new line
lineStream.str(line); lineStream.str(line);
// Clear sstream error state // Clear sstream error state
lineStream.clear(); lineStream.clear();
lineStream >> token; lineStream >> token;
if (!token.compare("outer")) { if (!token.compare("outer")) {
do { do {
// Get next line // Get next line
getline(inputfile, line); getline(inputfile, line);
// Set string stream to new line // Set string stream to new line
lineStream.str(line); lineStream.str(line);
// Clear sstream error state // Clear sstream error state
lineStream.clear(); lineStream.clear();
lineStream >> token; lineStream >> token;
if(!token.compare("vertex")) { if(!token.compare("vertex")) {
// Process vertex // Process vertex
// Coord double value // Coord double value
double coord; double coord;
// X Coord // X Coord
lineStream >> token; lineStream >> token;
coord = stod(token); coord = stod(token);
if((coord < Xmin) || numFacet == 0) { if((coord < Xmin) || numFacet == 0) {
Xmin = coord; Xmin = coord;
} }
if((coord > Xmax) || numFacet == 0) { if((coord > Xmax) || numFacet == 0) {
Xmax = coord; Xmax = coord;
} }
// Y Coord // Y Coord
lineStream >> token; lineStream >> token;
coord = stod(token); coord = stod(token);
if((coord < Ymin) || numFacet == 0) { if((coord < Ymin) || numFacet == 0) {
Ymin = coord; Ymin = coord;
} }
if((coord > Ymax) || numFacet == 0) { if((coord > Ymax) || numFacet == 0) {
Ymax = coord; Ymax = coord;
} }
// Z Coord // Z Coord
lineStream >> token; lineStream >> token;
coord = stod(token); coord = stod(token);
if((coord < Zmin) || numFacet == 0) { if((coord < Zmin) || numFacet == 0) {
Zmin = coord; Zmin = coord;
} }
if((coord > Zmax) || numFacet == 0) { if((coord > Zmax) || numFacet == 0) {
Zmax = coord; Zmax = coord;
} }
} // End vertex line } // End vertex line
} while(token.compare("endloop")); // Endloop } while(token.compare("endloop")); // Endloop
} }
}while(token.compare("endfacet")); // Endfacet }while(token.compare("endfacet")); // Endfacet
// Iterate facet counter // Iterate facet counter
numFacet++; numFacet++;
} }
}while(token.compare("endsolid")); }while(token.compare("endsolid"));
// Close file // Close file
inputfile.close(); inputfile.close();
} }
/** /**
* @brief Getter for the number of facets * @brief Getter for the number of facets
* *
* @return int * @return int
*/ */
int getNumFacets(){ int getNumFacets(){
return numFacet; return numFacet;
} }
/** /**
* @brief Getter for Xmin * @brief Getter for Xmin
* *
* @return double * @return double
*/ */
double getXmin() { double getXmin() {
return Xmin; return Xmin;
} }
/** /**
* @brief Getter for Xmax * @brief Getter for Xmax
* *
* @return double * @return double
*/ */
double getXmax() { double getXmax() {
return Xmax; return Xmax;
} }
/** /**
* @brief Getter for Ymin * @brief Getter for Ymin
* *
* @return double * @return double
*/ */
double getYmin() { double getYmin() {
return Ymin; return Ymin;
} }
/** /**
* @brief Getter for Ymax * @brief Getter for Ymax
* *
* @return double * @return double
*/ */
double getYmax() { double getYmax() {
return Ymax; return Ymax;
} }
/** /**
* @brief Getter for Zmin * @brief Getter for Zmin
* *
* @return double * @return double
*/ */
double getZmin() { double getZmin() {
return Zmin; return Zmin;
} }
/** /**
* @brief Getter for Zmax * @brief Getter for Zmax
* *
* @return double * @return double
*/ */
double getZmax() { double getZmax() {
return Zmax; return Zmax;
} }
}; };

File diff suppressed because it is too large Load Diff

View File

@@ -1,26 +1,26 @@
solid Shape solid Shape
facet normal 0.00441668 -0.0110034 -0.99993 facet normal 0.00441668 -0.0110034 -0.99993
outer loop outer loop
vertex 3.44187 5.90737 22.4502 vertex 3.44187 5.90737 22.4502
vertex 3.45978 5.88253 22.4505 vertex 3.45978 5.88253 22.4505
vertex 3.20279 5.6495 22.4519 vertex 3.20279 5.6495 22.4519
endloop endloop
endfacet endfacet
facet normal -0.840637 -0.386028 -0.379884 facet normal -0.840637 -0.386028 -0.379884
outer loop outer loop
vertex 0.546978 18.4458 9.70707 vertex 0.546978 18.4458 9.70707
vertex 0.231294 19.1332 9.70707 vertex 0.231294 19.1332 9.70707
vertex 0.517646 19.2217 8.98353 vertex 0.517646 19.2217 8.98353
endloop endloop
endfacet endfacet
facet normal -0.840637 -0.386028 -0.379884 facet normal -0.840637 -0.386028 -0.379884
outer loop outer loop
vertex 0.546978 18.4458 9.70707 vertex 0.546978 18.4458 9.70707
vertex 0.231294 19.1332 9.70707 vertex 0.231294 19.1332 9.70707
vertex 0.517646 19.2217 8.98353 vertex 0.517646 19.2217 8.98353
endloop endloop
endfacet endfacet
endsolid Shape endsolid Shape

View File

@@ -1,36 +1,36 @@
/** /**
* @file main.cpp * @file main.cpp
* @author Trevor Barnes (barnestr@msoe.edu) * @author Trevor Barnes (barnestr@msoe.edu)
* @brief The main driver file used to demonstrate the functionality of the * @brief The main driver file used to demonstrate the functionality of the
* ReadSTL class. The user enters the file location of the STL file as a * ReadSTL class. The user enters the file location of the STL file as a
* command line arg and the console returns the min/max coords of the solid's * command line arg and the console returns the min/max coords of the solid's
* facets as well as the total number of facets. * facets as well as the total number of facets.
* @version 1.1 * @version 1.1
* @date 2022-03-15 * @date 2022-03-15
* *
* @copyright Copyright (c) 2022 * @copyright Copyright (c) 2022
* *
*/ */
#include <string> #include <string>
#include <iostream> #include <iostream>
#include "ReadSTL.cpp" #include "ReadSTL.cpp"
using namespace std; using namespace std;
int main(int argc, char** argv) { int main(int argc, char** argv) {
ReadSTL stl1(argv[1]); ReadSTL stl1(argv[1]);
cout << endl << "File read successfully!" << endl; cout << endl << "File read successfully!" << endl;
// Formatted file info output // Formatted file info output
cout << "Facets in file: " << stl1.getNumFacets() << endl << endl; cout << "Facets in file: " << stl1.getNumFacets() << endl << endl;
cout << "X Min: " << stl1.getXmin() << endl; cout << "X Min: " << stl1.getXmin() << endl;
cout << "X Max: " << stl1.getXmax() << endl << endl; cout << "X Max: " << stl1.getXmax() << endl << endl;
cout << "Y Min: " << stl1.getYmin() << endl; cout << "Y Min: " << stl1.getYmin() << endl;
cout << "Y Max: " << stl1.getYmax() << endl << endl; cout << "Y Max: " << stl1.getYmax() << endl << endl;
cout << "Z Min: " << stl1.getZmin() << endl; cout << "Z Min: " << stl1.getZmin() << endl;
cout << "Z Max: " << stl1.getZmax() << endl << endl; cout << "Z Max: " << stl1.getZmax() << endl << endl;
return 0; return 0;
} }

View File

@@ -1,25 +1,25 @@
# lab1 Makefile # lab1 Makefile
CC = g++ CC = g++
CFLAGS = -c -MMD -g CFLAGS = -c -MMD -g
LFLAGS = LFLAGS =
# Change w/ every new project # Change w/ every new project
SOURCES = ReadSTL.cpp main.cpp SOURCES = ReadSTL.cpp main.cpp
OBJECTS = $(SOURCES:.cpp=.o) OBJECTS = $(SOURCES:.cpp=.o)
# Change w/ every new project # Change w/ every new project
EXECUTABLE = Lab1 EXECUTABLE = Lab1
all: $(EXECUTABLE) $(SOURCES) all: $(EXECUTABLE) $(SOURCES)
$(EXECUTABLE): $(OBJECTS) $(EXECUTABLE): $(OBJECTS)
$(CC) $(LFLAGS) -o $@ $(OBJECTS) $(CC) $(LFLAGS) -o $@ $(OBJECTS)
-include *.d -include *.d
%.o:%.cpp %.o:%.cpp
$(CC) $(CFLAGS) $< $(CC) $(CFLAGS) $<
clean: clean:
rm -f $(EXECUTABLE) rm -f $(EXECUTABLE)
rm -f $(OBJECTS) rm -f $(OBJECTS)
rm -f *.d rm -f *.d

View File

@@ -1,94 +1,94 @@
/** /**
* @file main.cpp * @file main.cpp
* @author Trevor Barnes (barnestr@msoe.edu) * @author Trevor Barnes (barnestr@msoe.edu)
* @brief A main driver that provides some rudimentary tests for row.cpp * @brief A main driver that provides some rudimentary tests for row.cpp
* Tests involving memory leaks were tested using valgrind via the command: * Tests involving memory leaks were tested using valgrind via the command:
* `valgrind ./lab2` Test output was left in submission to signify modules * `valgrind ./lab2` Test output was left in submission to signify modules
* that have successfully passed and provide further clarity of testing * that have successfully passed and provide further clarity of testing
* methods. * methods.
* @version 1.0 * @version 1.0
* @date 2022-03-22 * @date 2022-03-22
* *
* @copyright Copyright (c) 2022 * @copyright Copyright (c) 2022
* *
*/ */
#include <string> #include <string>
#include <iostream> #include <iostream>
#include "row.h" #include "row.h"
using namespace std; using namespace std;
int main() { int main() {
// Test Setup // Test Setup
int length; int length;
double testValue; double testValue;
cout << "Enter the desired length for the row matrix:" << endl; cout << "Enter the desired length for the row matrix:" << endl;
cin >> length; cin >> length;
cout << "Enter a double value to test as row data element:" << endl; cout << "Enter a double value to test as row data element:" << endl;
cin >> testValue; cin >> testValue;
cout << "Test - Constructor / Access Operator" << endl; cout << "Test - Constructor / Access Operator" << endl;
// Test: Constructor // Test: Constructor
Row row1(length); Row row1(length);
// Test: Access Operator (non const) // Test: Access Operator (non const)
try{ try{
// All elements are accessed // All elements are accessed
for(int i = 0; i < length; i++){ for(int i = 0; i < length; i++){
cout << row1[i] << " "; cout << row1[i] << " ";
} }
cout << endl; cout << endl;
// First element shown as 0 // First element shown as 0
cout << row1[0] << endl; cout << row1[0] << endl;
// First element is assigned the test value // First element is assigned the test value
row1[0] = testValue; row1[0] = testValue;
// First element is now the test value // First element is now the test value
cout << row1[0] << endl; cout << row1[0] << endl;
// Throws OOB Exception // Throws OOB Exception
cout << row1[length+1] << endl; cout << row1[length+1] << endl;
}catch(const out_of_range e){ }catch(const out_of_range e){
cout << "Out of Range Exception Successfully Caught" << endl; cout << "Out of Range Exception Successfully Caught" << endl;
} }
cout << "Test - Constructor / Access Operator: PASS" << endl; cout << "Test - Constructor / Access Operator: PASS" << endl;
cout << "Test - Copy Constructor / Access Operator (const)" << endl; cout << "Test - Copy Constructor / Access Operator (const)" << endl;
// Test: Copy Constructor // Test: Copy Constructor
const Row row1Copy(row1); const Row row1Copy(row1);
// Test: Access Operator (const) // Test: Access Operator (const)
try{ try{
for(int i = 0; i < length; i++){ for(int i = 0; i < length; i++){
cout << row1Copy[i] << " "; cout << row1Copy[i] << " ";
} }
cout << endl; cout << endl;
// First element is the same as the original Row object's // First element is the same as the original Row object's
cout << row1Copy[0] << endl; cout << row1Copy[0] << endl;
// Throws OOB Exception // Throws OOB Exception
cout << row1Copy[length+1]; cout << row1Copy[length+1];
}catch(const out_of_range e){ }catch(const out_of_range e){
cout << "Out of Range Exception Successfully Caught" << endl; cout << "Out of Range Exception Successfully Caught" << endl;
} }
cout << "Test - Copy Constructor / Access Operator (const): PASS" << endl; cout << "Test - Copy Constructor / Access Operator (const): PASS" << endl;
// Test: Assignment Operator // Test: Assignment Operator
Row row2(length); Row row2(length);
// Set new row object equal to another row object // Set new row object equal to another row object
row2 = row1; row2 = row1;
// Show that the elements are identical to the original row object // Show that the elements are identical to the original row object
for(int i = 0; i < length; i++){ for(int i = 0; i < length; i++){
cout << row2[i] << " "; cout << row2[i] << " ";
} }
cout << endl; cout << endl;
// Test: Clear // Test: Clear
cout << "Test - Clear" << endl; cout << "Test - Clear" << endl;
row2.clear(); row2.clear();
for(int i = 0; i < length; i++){ for(int i = 0; i < length; i++){
cout << row2[i] << " "; cout << row2[i] << " ";
} }
cout << endl; cout << endl;
cout << "Test - Clear: PASS" << endl; cout << "Test - Clear: PASS" << endl;
// Tested for memory leaks using valgrind: // Tested for memory leaks using valgrind:
// Result: "All heap blocks were freed -- no leaks are possible" // Result: "All heap blocks were freed -- no leaks are possible"
return 0; return 0;
} }

View File

@@ -1,25 +1,25 @@
# lab2 Makefile # lab2 Makefile
CC = g++ CC = g++
CFLAGS = -c -MMD -g CFLAGS = -c -MMD -g
LFLAGS = LFLAGS =
# Change w/ every new project # Change w/ every new project
SOURCES = main.cpp row.cpp SOURCES = main.cpp row.cpp
OBJECTS = $(SOURCES:.cpp=.o) OBJECTS = $(SOURCES:.cpp=.o)
# Change w/ every new project # Change w/ every new project
EXECUTABLE = Lab2 EXECUTABLE = Lab2
all: $(EXECUTABLE) $(SOURCES) all: $(EXECUTABLE) $(SOURCES)
$(EXECUTABLE): $(OBJECTS) $(EXECUTABLE): $(OBJECTS)
$(CC) $(LFLAGS) -o $@ $(OBJECTS) $(CC) $(LFLAGS) -o $@ $(OBJECTS)
-include *.d -include *.d
%.o:%.cpp %.o:%.cpp
$(CC) $(CFLAGS) $< $(CC) $(CFLAGS) $<
clean: clean:
rm -f $(EXECUTABLE) rm -f $(EXECUTABLE)
rm -f $(OBJECTS) rm -f $(OBJECTS)
rm -f *.d rm -f *.d

View File

@@ -1,51 +1,51 @@
#ifndef row_h #ifndef row_h
#define row_h #define row_h
class Row{ class Row{
public: public:
/* Parameterized constructor /* Parameterized constructor
* Takes in length and creates a row matrix with values cleared * Takes in length and creates a row matrix with values cleared
* to zero * to zero
*/ */
Row(unsigned int length); Row(unsigned int length);
/* Copy constructor /* Copy constructor
* Create a new row matrix with the same size and values as the * Create a new row matrix with the same size and values as the
* from matrix * from matrix
*/ */
Row(const Row& from); Row(const Row& from);
/* Destructor /* Destructor
* Correctly delete any heap memory * Correctly delete any heap memory
*/ */
~Row(); ~Row();
/* Access operator (const version) /* Access operator (const version)
* Allow access to row matrix data * Allow access to row matrix data
* Should return an exception if column is too large * Should return an exception if column is too large
*/ */
double operator[](unsigned int column) const; double operator[](unsigned int column) const;
/* Access operator (non const version) /* Access operator (non const version)
* Allow access to row matrix data * Allow access to row matrix data
* Should return an exception if column is too large * Should return an exception if column is too large
*/ */
double& operator[] (unsigned int column); double& operator[] (unsigned int column);
/* Assignment operator /* Assignment operator
* 1. Check if two sides are the same object * 1. Check if two sides are the same object
* 2. Delete the current row matrix * 2. Delete the current row matrix
* 3. Create a new row matrix with the same size and values as * 3. Create a new row matrix with the same size and values as
* the rhs matrix * the rhs matrix
*/ */
Row& operator= (const Row& rhs); Row& operator= (const Row& rhs);
/* Clear all data values to zero /* Clear all data values to zero
*/ */
void clear(); void clear();
private: private:
// Row matrix data // Row matrix data
double * row_data; double * row_data;
// Size of row matrix // Size of row matrix
unsigned int length; unsigned int length;
}; };
#endif #endif

View File

@@ -1,25 +1,25 @@
# lab3 Makefile # lab3 Makefile
CC = g++ CC = g++
CFLAGS = -c -MMD -g CFLAGS = -c -MMD -g
LFLAGS = LFLAGS =
# Change w/ every new project # Change w/ every new project
SOURCES = main.cpp row.cpp matrix.cpp SOURCES = main.cpp row.cpp matrix.cpp
OBJECTS = $(SOURCES:.cpp=.o) OBJECTS = $(SOURCES:.cpp=.o)
# Change w/ every new project # Change w/ every new project
EXECUTABLE = Lab3 EXECUTABLE = Lab3
all: $(EXECUTABLE) $(SOURCES) all: $(EXECUTABLE) $(SOURCES)
$(EXECUTABLE): $(OBJECTS) $(EXECUTABLE): $(OBJECTS)
$(CC) $(LFLAGS) -o $@ $(OBJECTS) $(CC) $(LFLAGS) -o $@ $(OBJECTS)
-include *.d -include *.d
%.o:%.cpp %.o:%.cpp
$(CC) $(CFLAGS) $< $(CC) $(CFLAGS) $<
clean: clean:
rm -f $(EXECUTABLE) rm -f $(EXECUTABLE)
rm -f $(OBJECTS) rm -f $(OBJECTS)
rm -f *.d rm -f *.d

View File

@@ -1,51 +1,51 @@
#ifndef row_h #ifndef row_h
#define row_h #define row_h
class Row{ class Row{
public: public:
/* Parameterized constructor /* Parameterized constructor
* Takes in length and creates a row matrix with values cleared * Takes in length and creates a row matrix with values cleared
* to zero * to zero
*/ */
Row(unsigned int length); Row(unsigned int length);
/* Copy constructor /* Copy constructor
* Create a new row matrix with the same size and values as the * Create a new row matrix with the same size and values as the
* from matrix * from matrix
*/ */
Row(const Row& from); Row(const Row& from);
/* Destructor /* Destructor
* Correctly delete any heap memory * Correctly delete any heap memory
*/ */
~Row(); ~Row();
/* Access operator (const version) /* Access operator (const version)
* Allow access to row matrix data * Allow access to row matrix data
* Should return an exception if column is too large * Should return an exception if column is too large
*/ */
double operator[](unsigned int column) const; double operator[](unsigned int column) const;
/* Access operator (non const version) /* Access operator (non const version)
* Allow access to row matrix data * Allow access to row matrix data
* Should return an exception if column is too large * Should return an exception if column is too large
*/ */
double& operator[] (unsigned int column); double& operator[] (unsigned int column);
/* Assignment operator /* Assignment operator
* 1. Check if two sides are the same object * 1. Check if two sides are the same object
* 2. Delete the current row matrix * 2. Delete the current row matrix
* 3. Create a new row matrix with the same size and values as * 3. Create a new row matrix with the same size and values as
* the rhs matrix * the rhs matrix
*/ */
Row& operator= (const Row& rhs); Row& operator= (const Row& rhs);
/* Clear all data values to zero /* Clear all data values to zero
*/ */
void clear(); void clear();
private: private:
// Row matrix data // Row matrix data
double * row_data; double * row_data;
// Size of row matrix // Size of row matrix
unsigned int length; unsigned int length;
}; };
#endif #endif

View File

@@ -1,25 +1,25 @@
# lab4 Makefile # lab4 Makefile
CC = g++ CC = g++
CFLAGS = -c -MMD -g CFLAGS = -c -MMD -g
LFLAGS = -lX11 LFLAGS = -lX11
# Change w/ every new project # Change w/ every new project
SOURCES = main.cpp gcontext.cpp x11context.cpp SOURCES = main.cpp gcontext.cpp x11context.cpp
OBJECTS = $(SOURCES:.cpp=.o) OBJECTS = $(SOURCES:.cpp=.o)
# Change w/ every new project # Change w/ every new project
EXECUTABLE = Lab4 EXECUTABLE = Lab4
all: $(EXECUTABLE) $(SOURCES) all: $(EXECUTABLE) $(SOURCES)
$(EXECUTABLE): $(OBJECTS) $(EXECUTABLE): $(OBJECTS)
$(CC) -o $@ $(OBJECTS) $(LFLAGS) $(CC) -o $@ $(OBJECTS) $(LFLAGS)
-include *.d -include *.d
%.o:%.cpp %.o:%.cpp
$(CC) $(CFLAGS) $< $(CC) $(CFLAGS) $<
clean: clean:
rm -f $(EXECUTABLE) rm -f $(EXECUTABLE)
rm -f $(OBJECTS) rm -f $(OBJECTS)
rm -f *.d rm -f *.d

View File

@@ -1,25 +1,25 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
/* /*
void swap(int x, int y) { void swap(int x, int y) {
int temp = x; int temp = x;
x = y; x = y;
y = temp; y = temp;
} }
*/ */
void swap(int* x, int* y) { void swap(int* x, int* y) {
int temp = *x; int temp = *x;
*x = *y; *x = *y;
*y = temp; *y = temp;
} }
int main() { int main() {
int A = 2; int A = 2;
int B = 8; int B = 8;
// Pass in addresses to be // Pass in addresses to be
swap(&A,&B); swap(&A,&B);
cout << "A: " << A << " B: " << B << endl; cout << "A: " << A << " B: " << B << endl;
return 0; return 0;
} }

View File

@@ -1,23 +1,23 @@
#makefile comment #makefile comment
CC = g++ CC = g++
CFLAGS = -C CFLAGS = -C
LFLAGS = LFLAGS =
SOURCES = math.cpp main.cpp SOURCES = math.cpp main.cpp
OBJECTS = $(SOURCES:.cpp=.o) OBJECTS = $(SOURCES:.cpp=.o)
EXECUTABLE = making_ex1 EXECUTABLE = making_ex1
all: $(EXECUTABLE) $(SOURCES) all: $(EXECUTABLE) $(SOURCES)
$(EXECUTABLE): $(OBJECTS) $(EXECUTABLE): $(OBJECTS)
$(CC) $(LFLAGS) -o $(EXECUTABLE) $(OBJECTS) $(CC) $(LFLAGS) -o $(EXECUTABLE) $(OBJECTS)
%.o:%.cpp %.o:%.cpp
$(CC) $(CFLAGS) $< $(CC) $(CFLAGS) $<
-include -include
clean: clean:
rm -f *.o rm -f *.o
rm -f $(EXECUTABLE) rm -f $(EXECUTABLE)
rm -f *.d rm -f *.d

View File

@@ -1,71 +1,71 @@
#include <iostream> #include <iostream>
#include "dog.h" #include "dog.h"
using namespace std; using namespace std;
Dog::Dog(int age, int namelength, char * name) { Dog::Dog(int age, int namelength, char * name) {
this->age = age; this->age = age;
this->namelength = namelength; this->namelength = namelength;
this->name = new char[namelength]; this->name = new char[namelength];
for(int i = 0; i < namelength; i++){ for(int i = 0; i < namelength; i++){
this->name[i] = name[i]; this->name[i] = name[i];
} }
} }
Dog::~Dog(){ Dog::~Dog(){
if(this->namelength > 0){ if(this->namelength > 0){
delete[] this->name; delete[] this->name;
} }
} }
Dog::Dog(const Dog& from){ Dog::Dog(const Dog& from){
this->age = from.age; this->age = from.age;
this->namelength = from.namelength; this->namelength = from.namelength;
this->name = new char[this->namelength]; this->name = new char[this->namelength];
for(int i = 0; i< this->namelength; i++){ for(int i = 0; i< this->namelength; i++){
this->name[i] = from.name[i]; this->name[i] = from.name[i];
} }
} }
Dog& Dog::operator=(const Dog& rhs){ Dog& Dog::operator=(const Dog& rhs){
if(&rhs != this){ if(&rhs != this){
// delete existing memory // delete existing memory
if(namelength > 0){ if(namelength > 0){
delete[] this->name; delete[] this->name;
} }
this->age = rhs.age; this->age = rhs.age;
this->namelength = rhs.namelength; this->namelength = rhs.namelength;
this->name = new char[namelength]; this->name = new char[namelength];
for(int i = 0; i < namelength; i++){ for(int i = 0; i < namelength; i++){
this->name[i] = rhs.name[i]; this->name[i] = rhs.name[i];
} }
} }
return *this; return *this;
} }
char Dog::operator[](int index) const{ char Dog::operator[](int index) const{
if(index >= namelength){ if(index >= namelength){
// Use with try/catch when called in main do exception e.what() // Use with try/catch when called in main do exception e.what()
throw(out_of_range("index is larger than name length")); throw(out_of_range("index is larger than name length"));
} }
return name[index]; return name[index];
} }
char& Dog::operator[](int index){ char& Dog::operator[](int index){
if(index >= namelength){ if(index >= namelength){
// Use with try/catch when called in main do exception e.what() // Use with try/catch when called in main do exception e.what()
throw(out_of_range("index is larger than name length")); throw(out_of_range("index is larger than name length"));
} }
return name[index]; return name[index];
} }
Dog Dog::puppy(int namelength, char * name){ Dog Dog::puppy(int namelength, char * name){
Dog p(0, namelength, name); Dog p(0, namelength, name);
return p; return p;
} }
ostream& operator<<(ostream& os, const Dog& d){ ostream& operator<<(ostream& os, const Dog& d){
for(int i = 0; i< d.namelength; i++) { for(int i = 0; i< d.namelength; i++) {
os << d[i]; os << d[i];
} }
return os; return os;
} }

View File

@@ -1,18 +1,18 @@
using namespace std; using namespace std;
class Dog { class Dog {
public: public:
Dog(int age, int namelength, char * name); Dog(int age, int namelength, char * name);
// has to be dog object, not reference // has to be dog object, not reference
static Dog Dog::puppy(int namelength, char *name); static Dog Dog::puppy(int namelength, char *name);
Dog(const Dog& from); Dog(const Dog& from);
~Dog(); ~Dog();
Dog& operator=(const Dog& rhs); Dog& operator=(const Dog& rhs);
char operator[](int index) const; char operator[](int index) const;
char& operator[](int index); char& operator[](int index);
friend ostream& operator<<(ostream& os, const Dog& d); friend ostream& operator<<(ostream& os, const Dog& d);
private: private:
int age; int age;
int namelength; int namelength;
char * name; char * name;
}; };

View File

@@ -1,16 +1,16 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int & multiply(int A, int B) { int & multiply(int A, int B) {
int i = A*B; int i = A*B;
return i; return i;
} }
int main() { int main() {
int A = 5; int A = 5;
int B = 3; int B = 3;
int C = multiply(A,B); int C = multiply(A,B);
cout << "A: " << A << " B: " << B << " C: " << C << endl; cout << "A: " << A << " B: " << B << " C: " << C << endl;
return 0; return 0;
} }

View File

@@ -1,15 +1,15 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() { int main() {
// int length; // int length;
// cin >> length; // cin >> length;
int array1[3] = {3, 10, 40}; int array1[3] = {3, 10, 40};
cout << array1 << endl; cout << array1 << endl;
cout << *array1 << endl; cout << *array1 << endl;
cout << array1 << endl; cout << array1 << endl;
return 0; return 0;
} }

27
Lecture/Week5/Lab5.txt Normal file
View File

@@ -0,0 +1,27 @@
Shape class
data:
color (required)
1st point
# points
functions:
constructor(color)? <- optional
shape::shape(...) <- do this in child?
copy constructor
assignment operator
destructor (virtual)
draw <- pure virtual
Shape::draw(Graphics context gc)
gc->setColor(color)
Shape * S1 = new Line(...)
Shape * S2 = new Triangle(...)
S1->draw(...) draw Line
S2->draw(...) draw Triangle
Image class

28
Lecture/Week5/main.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include <iostream>
using namespace std;
class Pet{
public:
string name;
void set_data(int data){}
};
class Cat: public Pet{
public:
int fish;
void set_data(int data){fish = data;}
};
class Bird: public Pet{
public:
int seed;
void set_data(int data){seed = data;}
};
int main(){
Pet* C1 = new Cat;
Pet* C2 = new Cat;
C1->name = "Cat1";
C1->set_data(1);
}