Skip to content

Instantly share code, notes, and snippets.

@balp
Last active November 11, 2018 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balp/98f50573793c7f22b297d947cf2b351e to your computer and use it in GitHub Desktop.
Save balp/98f50573793c7f22b297d947cf2b351e to your computer and use it in GitHub Desktop.
SOLID
cmake_minimum_required(VERSION 3.5.1)
project(solid)
enable_testing()
add_library(single-responsibility
single.cpp
open-closed.cpp
liskov.cpp
)
set_property(TARGET single-responsibility PROPERTY CXX_STANDARD 14)
#include <vector>
#include <numeric>
#include <string>
const double pi = 3.141592653589793;
class Shape { // e.g. Interface
public:
virtual ~Shape() {}
virtual double area() =0;
virtual double volume() =0;
};
class Circle : public Shape {
double m_radius;
public:
Circle(double radius) : m_radius(radius) {}
double area() override { return (m_radius * m_radius * pi) ; }
};
class Square : public Shape {
double m_length;
public:
Square(double length) : m_length(length) {}
double area() override { return (m_length * m_length) ; }
};
class AreaCalculator {
public:
AreaCalculator(std::vector<Shape*> shapes) : m_shapes(shapes) {}
double sum() {
return std::accumulate(m_shapes.begin(),
m_shapes.end(),
0,
[](double sum, Shape* shape){return (sum + shape->area());});
}
std::string output() { return "Area is " + std::to_string(sum()) + "."; }
std::string htmloutput() { return "<p>Area is " + std::to_string(sum()) + ".</p>"; }
protected:
std::vector<Shape*> m_shapes;
};
class VolumeCalculator : public AreaCalculator {
public:
double sum() {
return std::accumulate(m_shapes.begin(),
m_shapes.end(),
0,
[](double sum, Shape* shape){return (sum + shape->volume());});
}
};
class
void example()
{
};
#include <vector>
const double pi = 3.141592653589793;
class Shape {
public:
virtual ~Shape() {}
};
class Circle : public Shape {
public:
double m_radius;
Circle(double radius) : m_radius(radius) {}
};
class Square : public Shape {
public:
double m_length;
Square(double length) : m_length(length) {}
};
double Area(std::vector<Shape*> shapes)
{
double area = 0.0;
for(auto & shape : shapes) {
if(typeid(*shape) == typeid(Circle)) {
area += (dynamic_cast<Circle*>(shape))->m_radius * (dynamic_cast<Circle*>(shape))->m_radius * pi;
} else if(typeid(*shape) == typeid(Square)) {
area += (dynamic_cast<Square*>(shape))->m_length * (dynamic_cast<Square*>(shape))->m_length;
}
}
return area;
}
#!/bin/bash
if [[ ! -d build ]]; then
mkdir -p build
fi
cd build
cmake .. -DCMAKE_BUILD_TYPE=DEBUG && cmake --build . && ctest --output-on-failure
#include <vector>
#include <numeric>
#include <string>
const double pi = 3.141592653589793;
class Shape { // e.g. Interface
public:
virtual ~Shape() {}
virtual double area() =0;
};
class Circle : public Shape {
double m_radius;
public:
Circle(double radius) : m_radius(radius) {}
double area() override { return (m_radius * m_radius * pi) ; }
};
class Square : public Shape {
double m_length;
public:
Square(double length) : m_length(length) {}
double area() override { return (m_length * m_length) ; }
};
class AreaCalculator {
public:
AreaCalculator(std::vector<Shape*> shapes) : m_shapes(shapes) {}
double sum() {
return std::accumulate(m_shapes.begin(),
m_shapes.end(),
0,
[](double sum, Shape* shape){return (sum + shape->area());});
}
std::string output() { return "Area is " + std::to_string(sum()) + "."; }
std::string htmloutput() { return "<p>Area is " + std::to_string(sum()) + ".</p>"; }
private:
std::vector<Shape*> m_shapes;
};
void example()
{
std::vector<Shape*> shapes = {
new Circle(2),
new Square(5),
new Square(6)
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment