Skip to content

Instantly share code, notes, and snippets.

@sturmer sturmer/Shapes.cc
Created Dec 22, 2013

Embed
What would you like to do?
Example of std::none_of
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
using std::vector;
using std::none_of;
struct Shape {
Shape() {}
virtual float area() = 0;
virtual ~Shape() {}
};
class Rectangle : public Shape {
private:
float height;
float width;
public:
float area() override
{
return height * width;
}
Rectangle(float h, float w) : height(h), width(w) {}
};
class Circle : public Shape {
private:
float radius;
public:
float area() override
{
return radius * radius * 3.14; // approx
}
Circle(float radius) : radius(radius) {}
};
class Triangle : public Shape {
};
int main(int argc, const char *argv[])
{
constexpr float radius1{3.f};
constexpr float radius2{2.5f};
constexpr float height1{2.f};
constexpr float height2{3.f};
constexpr float width1{4.f};
constexpr float width2{1.5f};
Circle c1(radius1);
Circle c2(radius2);
Rectangle r1(height1, width1);
Rectangle r2(height1, width2);
Rectangle r3(height2, width1);
Rectangle r4(height2, width2);
vector<Shape*> v{&c1, &c2, &r1, &r2, &r2, &r4};
auto is_triangle = [](Shape* s) { return dynamic_cast<Triangle*>(s) != nullptr; };
bool no_triangle = none_of(v.cbegin(),
v.cend(),
is_triangle);
if (no_triangle)
cout << "No triangle\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.