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

Experiment with interfaces and strategy design pattern

parent 5143f6be
No related branches found
No related tags found
No related merge requests found
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 "DataSetInterface.h"
#include <iostream>
using namespace std;
int main()
{
DataSetInterface ds;
PointT p1(0.0, 0.0);
PointT p2(3.0, 4.0);
PointT p3(7.0, 24.0);
PointT p4(5.0, 12.0);
ds.add(&p1);
ds.add(&p2);
ds.add(&p3);
ds.add(&p4);
cout << "average = " << ds.getAverage() << "\n";
Measurable* maxM = ds.getMaximum();
PointT* maxP = (PointT*) maxM;
//maxP = dynamic_cast<PointT*>(maxM);
cout << "maximum = " << maxP << "\n";
return 0;
}
#ifndef DATASETINTERFACE_H
#define DATASETINTERFACE_H
#include "Measurable.h"
class DataSetInterface
{
private:
double sum;
Measurable* maximum;
int count;
public:
DataSetInterface();
void add(Measurable* x);
double getAverage();
Measurable* getMaximum();
};
#endif
#ifndef MEASURABLE_H
#define MEASURABLE_H
class Measurable
{
public:
virtual double getMeasure() = 0;
};
#endif
#ifndef POINTT_H
#define POINTT_H
#include "Measurable.h"
#include <iostream>
class PointT : public Measurable
{
public:
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 "DataSetInterface.h"
#include "Measurable.h"
DataSetInterface::DataSetInterface() : maximum()
{
this->sum = 0.0;
this->count = 0;
}
void DataSetInterface::add(Measurable* x)
{
this->sum += x->getMeasure();
if (count == 0 || maximum->getMeasure() < x->getMeasure()) maximum = x;
count++;
}
double DataSetInterface::getAverage()
{
if (count == 0) return 0;
else return sum/count;
}
Measurable* DataSetInterface::getMaximum()
{
return maximum;
}
#include "PointT.h"
#include <cmath>
#include "Measurable.h"
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