-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//61-DoubleDispatch | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
class Circle; | ||
class Triangle; | ||
|
||
class Shape{ | ||
public: | ||
virtual bool intersect(const Shape&) const = 0; | ||
virtual bool intersect(const Circle&) const = 0; | ||
virtual bool intersect(const Triangle&) const = 0; | ||
}; | ||
|
||
class Circle: public Shape{ | ||
public: | ||
bool intersect(const Shape&) const override; | ||
virtual bool intersect(const Circle&) const override; | ||
virtual bool intersect(const Triangle&) const override; | ||
}; | ||
|
||
class Triangle: public Shape{ | ||
public: | ||
bool intersect(const Shape&) const override; | ||
virtual bool intersect(const Circle&) const override; | ||
virtual bool intersect(const Triangle&) const override; | ||
}; | ||
|
||
bool Circle::intersect(const Shape& s) const {return s.intersect(*this);} | ||
bool Circle::intersect(const Circle&) const {std::cout << "intersect(circle, circle)" << std::endl; return true;} | ||
bool Circle::intersect(const Triangle&) const {std::cout << "intersect(circle, triangle)" << std::endl; return true;} | ||
|
||
bool Triangle::intersect(const Shape& s) const {return s.intersect(*this);} | ||
bool Triangle::intersect(const Circle&) const {std::cout << "intersect(triangle, circle)" << std::endl; return true;} | ||
bool Triangle::intersect(const Triangle&) const {std::cout << "intersect(triangle, triangle)" << std::endl; return true;} | ||
|
||
void Test(Triangle& t, Circle& c){ | ||
std::vector<std::pair<Shape*, Shape*>> vs {{&t, &t}, {&t, &c}, {&c, &t}, {&c, &c}}; | ||
for(auto p: vs) | ||
p.first->intersect(*p.second); | ||
} | ||
|
||
int main(){ | ||
Triangle t; | ||
Circle c; | ||
Test(t, c); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//62-Visitors | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
class Visitor; | ||
|
||
class Node{ | ||
public: | ||
virtual void accept(Visitor&) = 0; | ||
}; | ||
|
||
class Expression: public Node{ | ||
public: | ||
void accept(Visitor&) override; | ||
}; | ||
|
||
class Statement: public Node{ | ||
public: | ||
void accept(Visitor&) override; | ||
}; | ||
|
||
class Visitor{ | ||
public: | ||
virtual void accept(Expression&) = 0; | ||
virtual void accept(Statement&) = 0; | ||
}; | ||
|
||
void Expression::accept(Visitor& v){v.accept(*this);} | ||
void Statement::accept(Visitor& v){v.accept(*this);} | ||
|
||
class Visitor1: public Visitor{ | ||
void accept(Expression&){std::cout << "Visitor1 to Expression" << std::endl;} | ||
void accept(Statement&){std::cout << "Visitor1 to Statement" << std::endl;} | ||
}; | ||
|
||
class Visitor2: public Visitor{ | ||
void accept(Expression&){std::cout << "Visitor2 to Expression" << std::endl;} | ||
void accept(Statement&){std::cout << "Visitor2 to Statement" << std::endl;} | ||
}; | ||
|
||
void Test(Expression& e, Statement& s){ | ||
Visitor1 v1; | ||
Visitor2 v2; | ||
std::vector<std::pair<Node*, Visitor*>> vn {{&e, &v1}, {&s, &v1}, {&e, &v2}, {&s, &v2}}; | ||
for(auto p: vn) | ||
p.first->accept(*p.second); | ||
} | ||
|
||
int main(){ | ||
Expression e; | ||
Statement s; | ||
Test(e, s); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters