Visitor Pattern in C++ 11 I have referred some class diagram to actually create visitor.

#include &lt;iostream&gt;

using namespace std; //know should not be used, but for simplicity

class Book;
class Pen;
class Computer;

class ICartVisitor
{
public:
    virtual int Visit(Book &amp;) = 0;
    virtual int Visit(Pen &amp;) = 0;
    virtual int Visit(Computer &amp;) = 0;
};

class IElement
{
public:

    virtual int accept (ICartVisitor&amp; cartVisitor) = 0;
    virtual int getPrice () = 0;

};

class Book : public IElement
{

public:

    int accept (ICartVisitor&amp; cartVisitor)
    {
        return cartVisitor.Visit(*this);
    }

    int getPrice ()
    {
        return 100;
    }
};

class Pen : public IElement
{
public:

    int accept (ICartVisitor&amp; cartVisitor)
    {
        return cartVisitor.Visit(*this);

    }

    int getPrice ()
    {
        return 5;
    }
};

class Computer : public IElement
{
public:

    int accept (ICartVisitor&amp; cartVisitor)
    {
        return cartVisitor.Visit(*this);

    }

    int getPrice ()
    {
        return 10000;
    }
};

class CartVisitor : public ICartVisitor
{
public:

    //inlining all the functions for simplicity
    int Visit(Book &amp; book) 
    {
        return book.getPrice();
    }

    int Visit(Pen &amp; pen) 
    {
        return pen.getPrice();

    }

    int Visit(Computer &amp; computer) 
    {
        return computer.getPrice();

    }
};


int main(int argc, char* argv[])
{
    int total = 0;

    ICartVisitor *cartVisitor = new CartVisitor();

    total += cartVisitor-&gt;Visit(*(new Computer()));
    total += cartVisitor-&gt;Visit(*(new Book()));
    total += cartVisitor-&gt;Visit(*(new Pen()));

    cout&lt;&lt;total;

    return 0;
}
First: you leak memory. Terribly. Never use new unless you know what you are doing. Use smart pointers, or in this case just objects on the stack.

The visitor pattern is meant for separating algorithms from an object structure. There is no object structure / composition / hierarchy in your code, so there is no use for the visitor pattern.

Following your example, there should be a Cart class with an accept method, that iteratively calls accept for all elements in the cart.

Also the visitor pattern is supposed to prepare for unknown algorithms operating on data structures that should not know about the algorithms. So it does not make sense to use a return value here. If you want to accumulate a prize here, you would have to do it in the state of the visitor.

Also: Use consistent spacing between type&amp; variable etc. Use consistent capitalization of methods (visit). Avoid redundant newlines.