diff --git a/Lectures/L26_SpecViaUML/DataSetTemplate/Makefile b/Lectures/L26_SpecViaUML/DataSetTemplate/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..d2c5aca0dd4a53631812d8d1109cd0107104b023 --- /dev/null +++ b/Lectures/L26_SpecViaUML/DataSetTemplate/Makefile @@ -0,0 +1,53 @@ +SRC_DIRS := src +C_SRCS := $(foreach srcdir,$(SRC_DIRS),$(wildcard $(srcdir)/*.c)) +CXX_SRCS := $(foreach srcdir,$(SRC_DIRS),$(wildcard $(srcdir)/*.cpp)) +C_OBJS := ${C_SRCS:.c=.o} +CXX_OBJS := ${CXX_SRCS:.cpp=.o} +OBJS := $(C_OBJS) $(CXX_OBJS) +INCLUDE_DIRS := include +LIBRARY_DIRS := +LIBRARIES := + +prog_NAME := experiment +prog_DIR := bin +prog_FULL := $(prog_DIR)/$(prog_NAME) +prog_SRC_DIRS := experimentation +prog_C_SRCS := $(foreach srcdir,$(prog_SRC_DIRS),$(wildcard $(srcdir)/*.c)) +prog_CXX_SRCS := $(foreach srcdir,$(prog_SRC_DIRS),$(wildcard $(srcdir)/*.cpp)) +prog_C_OBJS := ${prog_C_SRCS:.c=.o} +prog_CXX_OBJS := ${prog_CXX_SRCS:.cpp=.o} +prog_OBJS := $(prog_C_OBJS) $(prog_CXX_OBJS) +prog_INCLUDE_DIRS := +prog_LIBRARY_DIRS := +prog_LIBRARIES := + +all_OBJS := $(OBJS) $(prog_OBJS) $(test_OBJS) +DEP := $(all_OBJS:%.o=%.d) + +CXXFLAGS += -std=c++11 +CXXFLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir)) +LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir)) +LDFLAGS += $(foreach library,$(LIBRARIES),-l$(library)) + +.PHONY: experiment src clean + +experiment: CXXFLAGS += $(foreach includedir,$(prog_INCLUDE_DIRS),-I$(includedir)) +experiment: LDFLAGS += $(foreach librarydir,$(prog_LIBRARY_DIRS),-L$(librarydir)) +experiment: LDFLAGS += $(foreach library,$(prog_LIBRARIES),-l$(library)) + +experiment: $(prog_FULL) + ./$(prog_FULL) + +$(prog_FULL): $(prog_OBJS) $(OBJS) + $(LINK.cc) $^ -o $@ + +-include $(DEP) + +%.o: %.cpp + $(CXX) $(CXXFLAGS) -MMD -c $< -o $@ + +clean: + @- $(RM) $(prog_FULL) + @- $(RM) $(prog_OBJS) + @- $(RM) $(OBJS) + @- $(RM) $(DEP) diff --git a/Lectures/L26_SpecViaUML/DataSetTemplate/bin/.gitkeep b/Lectures/L26_SpecViaUML/DataSetTemplate/bin/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/Lectures/L26_SpecViaUML/DataSetTemplate/bin/experiment b/Lectures/L26_SpecViaUML/DataSetTemplate/bin/experiment new file mode 100755 index 0000000000000000000000000000000000000000..1d18dba6b3dd053f4af4df8ba610796138725f12 Binary files /dev/null and b/Lectures/L26_SpecViaUML/DataSetTemplate/bin/experiment differ diff --git a/Lectures/L26_SpecViaUML/DataSetTemplate/experimentation/main.cpp b/Lectures/L26_SpecViaUML/DataSetTemplate/experimentation/main.cpp new file mode 100644 index 0000000000000000000000000000000000000000..81f42b8a16ea36fe1c226bd850116eba7550f18a --- /dev/null +++ b/Lectures/L26_SpecViaUML/DataSetTemplate/experimentation/main.cpp @@ -0,0 +1,46 @@ +#include "PointT.h" +#include "BankAccount.h" +#include "DataSetTemplate.h" + +#include <iostream> +using namespace std; + +typedef DataSetTemplate<PointT> DataSetPointT; +typedef DataSetTemplate<BankAccount> DataSetBankAccount; + +int main() +{ + + //Test Data Set Inteface with PointT + PointT p1(0.0, 0.0); + PointT p2(3.0, 4.0); + PointT p3(7.0, 24.0); + PointT p4(5.0, 12.0); + + DataSetPointT ds; + ds.add(p1); + ds.add(p2); + ds.add(p3); + ds.add(p4); + + cout << "average = " << ds.getAverage() << "\n"; + + cout << "maximum = " << ds.getMaximum() << "\n"; + + //Test Data Set Inteface with Bank Account + + BankAccount b1; + b1.deposit(300); + BankAccount b2; + b2.deposit(100); + + DataSetBankAccount dsBank; + dsBank.add(b1); + dsBank.add(b2); + + cout << "average = " << dsBank.getAverage() << "\n"; + + cout << "maximum = " << dsBank.getMaximum() << "\n"; + + return 0; +} diff --git a/Lectures/L26_SpecViaUML/DataSetTemplate/include/BankAccount.h b/Lectures/L26_SpecViaUML/DataSetTemplate/include/BankAccount.h new file mode 100644 index 0000000000000000000000000000000000000000..3fd9408245b058d8863580a74e2df0f8d71421c7 --- /dev/null +++ b/Lectures/L26_SpecViaUML/DataSetTemplate/include/BankAccount.h @@ -0,0 +1,22 @@ +#ifndef BANKACCOUNT_H +#define BANKACCOUNT_H + +#include <iostream> + +class BankAccount +{ + public: + BankAccount(); + void deposit(double amount); + void withdraw(double amount); + double getBalance(); + + friend std::ostream& operator<< (std::ostream &out, const BankAccount &ba); + + double getMeasure(); + + private: + double balance; +}; + +#endif diff --git a/Lectures/L26_SpecViaUML/DataSetTemplate/include/DataSetTemplate.h b/Lectures/L26_SpecViaUML/DataSetTemplate/include/DataSetTemplate.h new file mode 100644 index 0000000000000000000000000000000000000000..15f22feafb589688a7360265a6c2b3e382a80988 --- /dev/null +++ b/Lectures/L26_SpecViaUML/DataSetTemplate/include/DataSetTemplate.h @@ -0,0 +1,19 @@ +#ifndef DATASETTEMPLATE_H +#define DATASETTEMPLATE_H + +template <class T> +class DataSetTemplate +{ + private: + double sum; + T maximum; + int count; + + public: + DataSetTemplate(); + void add(T x); + double getAverage(); + T getMaximum(); +}; + +#endif diff --git a/Lectures/L26_SpecViaUML/DataSetTemplate/include/PointT.h b/Lectures/L26_SpecViaUML/DataSetTemplate/include/PointT.h new file mode 100644 index 0000000000000000000000000000000000000000..65a934b24ddfbbbcd29460635b7cf37a96df44cd --- /dev/null +++ b/Lectures/L26_SpecViaUML/DataSetTemplate/include/PointT.h @@ -0,0 +1,24 @@ +#ifndef POINTT_H +#define POINTT_H + +#include <iostream> + +class PointT +{ + public: + PointT(); + PointT(double x, double y); + double xcoord(); + double ycoord(); + double distToOrigin(); + + friend std::ostream& operator<< (std::ostream &out, const PointT &point); + + double getMeasure(); + + private: + double xc; + double yc; +}; + +#endif diff --git a/Lectures/L26_SpecViaUML/DataSetTemplate/src/BankAccount.cpp b/Lectures/L26_SpecViaUML/DataSetTemplate/src/BankAccount.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2abe22d20604df3233bde6a7c4cb606104e5ab69 --- /dev/null +++ b/Lectures/L26_SpecViaUML/DataSetTemplate/src/BankAccount.cpp @@ -0,0 +1,34 @@ +#include "BankAccount.h" + +BankAccount::BankAccount() +{ + this->balance = 0.0; +} + +void BankAccount::deposit(double amount) +{ + this->balance += amount; +} + +void BankAccount::withdraw(double amount) +{ + this->balance -= amount; +} + +double BankAccount::getBalance() +{ + return balance; +} + +double BankAccount::getMeasure() +{ + return balance; +} + +std::ostream& operator<< (std::ostream &out, const BankAccount &ba) +{ + //From http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/ + out << "BankAccount(" << ba.balance << ") "; + + return out; +} diff --git a/Lectures/L26_SpecViaUML/DataSetTemplate/src/DataSetTemplate.cpp b/Lectures/L26_SpecViaUML/DataSetTemplate/src/DataSetTemplate.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee1d3bc05fa116eff8ae6e9b40da1d99e57afe34 --- /dev/null +++ b/Lectures/L26_SpecViaUML/DataSetTemplate/src/DataSetTemplate.cpp @@ -0,0 +1,37 @@ +#include "DataSetTemplate.h" + +#include "PointT.h" +#include "BankAccount.h" + +template <class T> +DataSetTemplate<T>::DataSetTemplate() : maximum() +{ + this->sum = 0.0; + this->count = 0; +} + +template <class T> +void DataSetTemplate<T>::add(T x) +{ + this->sum += x.getMeasure(); + if (count == 0 || maximum.getMeasure() < x.getMeasure()) maximum = x; + count++; +} + +template <class T> +double DataSetTemplate<T>::getAverage() +{ + if (count == 0) return 0; + else return sum/count; +} + +template <class T> +T DataSetTemplate<T>::getMaximum() +{ + return maximum; +} + +// explicit instantiations +template class DataSetTemplate<PointT>; +template class DataSetTemplate<BankAccount>; + diff --git a/Lectures/L26_SpecViaUML/DataSetTemplate/src/PointT.cpp b/Lectures/L26_SpecViaUML/DataSetTemplate/src/PointT.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3a3e5043509fa4ab060082eaa30bdf82f059ab97 --- /dev/null +++ b/Lectures/L26_SpecViaUML/DataSetTemplate/src/PointT.cpp @@ -0,0 +1,49 @@ +#include "PointT.h" +#include <cmath> + +PointT::PointT() +{ + this->xc = 0.0; + this->yc = 0.0; +} + +PointT::PointT(double x, double y) +{ + this->xc = x; + this->yc = y; +} + +double PointT::xcoord() +{ + return this->xc; +} + +double PointT::ycoord() +{ + return this->yc; +} + +double PointT::distToOrigin() +{ + double dx; + double dy; + + dx = this->xc; + dy = this->yc; + + return sqrt(pow(dx,2.0) + pow(dy,2.0)); +} + +double PointT::getMeasure() +{ + return this->distToOrigin(); +} + +std::ostream& operator<< (std::ostream &out, const PointT& point) +{ + //From http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/ + // Since operator<< is a friend of the Point class, we can access Point's members directly. + out << "PointT(" << point.xc << ", " << point.yc << ")"; + + return out; +}