#include <iostream> #include <vector> #define ENUM enum class using namespace std; ENUM Color{ red, green, blue }; ENUM Size{ small, medium, large }; //Product structure struct Product { std::string name; Color color; Size size; }; //************************************** //Interface for Specification and Filter template <typename T> struct Specification { virtual ~Specification() {} virtual bool is_satisfied(T* item) = 0; }; template <typename T> struct Filter { virtual std::vector<T*> filter(std::vector<T*> items, Specification<T>& spec) = 0; }; //*************************************************** template <typename T> struct AndSpecification : Specification<T> { Specification<T>& first; Specification<T>& second; AndSpecification(Specification<T>&& first, Specification<T>&& second) : first(first), second(second) {} bool is_satisfied(T* item) override { return first.is_satisfied(item) && second.is_satisfied(item); } }; struct BetterFilter : Filter<Product> { std::vector<Product* > filter(std::vector<Product*> items, Specification<Product>& spec); }; struct ColorSpecification : Specification<Product> { Color color; ColorSpecification(Color color) :color(color) {} bool is_satisfied(Product* item); }; struct SizeSpecification : Specification<Product> { Size size; SizeSpecification(Size size) :size(size) {} bool is_satisfied(Product* item); }; std::vector<Product*> BetterFilter::filter(std::vector<Product *> list, Specification<Product> &spec) { std::vector<Product *> result; for (auto& i : list) { if (spec.is_satisfied(i)) { result.push_back(i); } } return result; } //**************************** //Defn for is_satisfied bool ColorSpecification::is_satisfied(Product* item) { return item->color == this->color; } bool SizeSpecification::is_satisfied(Product* item) { return item->size == this->size; } //**************************** //Operator overloaded template <typename T> AndSpecification<T> operator&&(Specification<T>&& one, Specification<T>&& two) { return AndSpecification<T>{std::move(one), std::move(two)}; } /* AndSpecification<Product> operator&&(Specification<Product>&& one, Specification<Product>&& two) { return AndSpecification<Product>(one, two); } */ int main() { Product apple{ "Apple", Color::green, Size::small }; Product tree{ "Tree", Color::green, Size::large }; Product house{ "House", Color::blue, Size::large }; vector<Product*> list{ &apple, &tree, &house }; BetterFilter bf; //AndSpecification<Product> green_and_large{ green, large }; AndSpecification<Product> spec = ColorSpecification(Color::green) && SizeSpecification(Size::large); for (auto& i : bf.filter(list, spec)) { cout << i->name << " is green and large\n"; } return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}