adplus-dvertising
frame-decoration

Question

What is the output of this program?
    #include <iostream>
    #include <vector>
    using namespace std;
    class Component
    { 
        public:
        virtual void traverse() = 0;
    };
    class Leaf: public Component
    {
        int value;
        public:
        Leaf(int val)
        {
            value = val;
        }
        void traverse()
        {
            cout << value << ' ';
        }
    };
    class Composite: public Component
    {
        vector < Component * > children;
        public:
        void add(Component *ele)
        {
            children.push_back(ele);
        }
        void traverse()
        {
            for (int i = 0; i < children.size(); i++)
                children[i]->traverse();
        }
    };
    int main()
    {
        Composite containers[4];
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 3; j++)
                containers[i].add(new Leaf(i *3+j));
            for (int k = 1; k < 4; k++)
                containers[0].add(&(containers[k]));
            for (int p = 0; p < 4; p++)
            {
                containers[p].traverse();
            }
    }

a.

345

b.

678

c.

901

d.

None of the mentioned

Answer: (d).None of the mentioned

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. What is the output of this program?