Docsity

Memory Layout Analysis of given C++ classes, Assignments of Programming Languages

Toggle sidebar
bg1
Homework 8
Electronic submission due the end of Apr 29, 2009
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(); }
};
1
pf2

Partial preview of the text

Download Memory Layout Analysis of given C++ classes and more Assignments Programming Languages in PDF only on Docsity!

Homework 8

Electronic submission due the end of Apr 29, 2009

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(); }

  1. Give the memory layout for the classes Element, Symbol, Number, List.
  2. Give the memory layout for the res pointer after the memory allocation in main.