visitor in C++

```
#include <iostream>
using namespace std;

class Stock {
public:
    virtual void accept(class Visitor *) = 0;
};
  
class Apple : public Stock {
  public:
    void buy() { cout << "Apple::buy"; }
    void sell() { cout << "Apple::sell"; }
    void accept(Visitor *);
};
  
class Google : public Stock {
  public:
    void buy() { cout << "Google::buy"; }
    void sell() { cout << "Google::sell"; }
    void accept(Visitor *);
};

class Visitor {
public:
    virtual void visit(Apple *) = 0;
    virtual void visit(Google *) = 0;
};

void Apple::accept(Visitor *v) { v->visit(this); }
  
void Google::accept(Visitor *v) { v->visit(this); }

class BuyVisitor : public Visitor {
  public:
    void visit(Apple *a) {
        cout<<"[";
        a->buy();
        cout <<"]"<<endl;
    }
    void visit(Google *g) {
        cout<<"[";
        g->buy();
        cout <<"]"<<endl;
    }
};
  
class SellVisitor : public Visitor {
  public:
    void visit(Apple *a) {
        cout<<"[";
        a->sell();
        cout <<"]"<<endl;
    }
    void visit(Google *g) {
        cout<<"[";
        g->sell();
        cout <<"]"<<endl;
    }
};
  
  
int main() {
    Stock *set[] = { new Apple, new Google, new Google,
                     new Apple, new Apple, 0};
  
    BuyVisitor buy;
    for (int i = 0; set[i]; i++) {
        set[i]->accept(&buy);
    }

    SellVisitor sell;
    for (int i = 0; set[i]; i++) {
        set[i]->accept(&sell);
    }
}
```
Output,
```
[Apple::buy]
[Google::buy]
[Google::buy]
[Apple::buy]
[Apple::buy]
[Apple::sell]
[Google::sell]
[Google::sell]
[Apple::sell]
[Apple::sell]
```