Skip to content
Snippets Groups Projects
Commit 21ce2b7b authored by W. Spencer Smith's avatar W. Spencer Smith
Browse files

Experiment with dataset implemented using templates

parent 2b2685f8
No related branches found
No related tags found
No related merge requests found
Showing
with 284 additions and 0 deletions
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)
File added
#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;
}
#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
#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
#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
#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;
}
#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>;
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment