

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Suppose we have the following C++ code.
#include <stdio.h> class Element { public: virtual void print() {} }; class Symbol : public Element { private: const char* content; public: Symbol(const char* _content) : content(_content) {} const char* get_content() { return content; } virtual void print() { printf(" %s ",content); } }; class Number : public Element { private: int value; public: Number(int _value) : value(_value) {} int get_value() { return value; } virtual void print() { printf(" %d ", value); } }; class List : public Element { private: Element head; Element tail; public: List(Element _head, Element _tail) : head(_head), tail(_tail) {} Element* get_head() { return head; } Element* get_tail() { return tail; } virtual void print() { head->print(); tail->print(); } };
int main() { Element * res = new List(new Number(1), new List(new Symbol("abc"), new Element())); res->print(); }