Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoklavat committed Feb 20, 2021
1 parent eefbacd commit e609f22
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
48 changes: 48 additions & 0 deletions 61-DoubleDispatch.cpp
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);
}
54 changes: 54 additions & 0 deletions 62-Visitors.cpp
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);
}
7 changes: 5 additions & 2 deletions MODERNCPP_REMINDER.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
* Declaration: introduce names into program.
* Default Constructor: constructor that can be called with no arguments, or that can take arguments, provided they are given default values.
* Definition: declarations that fully define entity introduced by declaration.
* Dereferencing: referring to object pointed to by pointer. indirection.
* Dereferencing: referring to object pointed to by pointer. indirection.
* Double Dispatch: shows how to select virtual function based on two types. Visitors shows how to use double dispatch to add multiple functions to class hierarchy with only single additional virtual function in hierarchy.
* Downcast: casting from base class to derived class.
* Dynamic Binding: member function is selected dynamically (at run-time) based on type of object, not type of pointer/reference to that object.
* Dynamic-Data: member object’s exact class can change dynamically over time.
Expand Down Expand Up @@ -106,7 +107,9 @@
* Structure: aggregate of elements of arbitrary types.
* Suffix: affix which is placed after stem of word.
* Syntax Analysis: Parsing. set of rules, principles, and processes that govern structure of sentences in language. takes input from lexical analyzer in form of token streams and analyzes source code (token stream) against production rules to detect errors in code.
* Template: entity that defines one of following
* Template: mechanism that allows type or value to be parameter in definition of class, function, or type alias.
* Template Instantiation: process of generating class or function from template plus template argument list.
* Template Specialization: version of template for specific template argument list.
* Translation Unit: unit is what compiler proper works on and what C++ language rules describe.
* Trivial Type: type that has trivial default constructor, copy and move operations. trivial means no need to do work.
* Trivially Copyable Type: type that has nontrivial copy operation, move operation, or destructor. can be implemented as bitwise copy.
Expand Down

0 comments on commit e609f22

Please sign in to comment.