Question
#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
Posted under Object Oriented Programming Using C++
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?
Similar Questions
Discover Related MCQs
Q. Which container provides random access iterators?
View solution
Q. Which is used for manually writing lookup table?
View solution
Q. How the member functions in the container can be accessed?
View solution
Q. Which of the following type does the container should define?
View solution
Q. Which are the parameters for the content of the buffer?
View solution
Q. What do vectors represent?
View solution
Q. In which type of storage location are the vector members stored?
View solution
Q. How many vector container properties are there in c++?
View solution
Q. Pick out the correct statement about vector.
View solution
Q. Which is optional in the declaration of vector?
View solution
Q. How many items are there in sequence container?
View solution
Q. Which of the following class template are based on arrays?
View solution
Q. Which of the following will return the new element at the end of container?
View solution
Q. How the list containers are implemented?
View solution
Q. Which of the following does not support any insertion or deletion?
View solution
Q. What do container adapter provide to interface?
View solution
Q. What does the sequence adaptor provide?
View solution
Q. Which are presented in the container adaptors?
View solution
Q. In which context does the stack operates?
View solution
Q. Which operator is used in priority queue?
View solution
Suggested Topics
Are you eager to expand your knowledge beyond Object Oriented Programming Using C++? We've curated a selection of related categories that you might find intriguing.
Click on the categories below to discover a wealth of MCQs and enrich your understanding of Computer Science. Happy exploring!