supervision_2.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Programming in C and C++ -- 2020/21 -- Supervision 2</title>
  5. </head>
  6. <style>
  7. code, pre {
  8. font-size: 1.2em;
  9. color: darkslateblue;
  10. }
  11. </style>
  12. <body>
  13. <h1>Programming in C and C++ (2020/21)<br/>Supervision 2</h1>
  14. <h2>1</h2>
  15. <p>If a function <code>f</code> has a static instance of a class as a local variable, when is the class constructor called?</p>
  16. <hr />
  17. <h2>2</h2>
  18. <p>Write a class <code>Matrix</code> which allows a programmer to define 2 &times; 2 matrices. Overload the common operators (e.g. <code>+</code>, <code>-</code>, <code>*</code>, and <code>/</code>). Can you easily extend your design to matrices of arbitrary size?</p>
  19. <hr />
  20. <h2>3</h2>
  21. <p>Write a class <code>Vector</code> which allows a programmer to define a vector of length two. Modify your <code>Matrix</code> and <code>Vector</code> classes so that they interoperate correctly (e.g. <code>v2 = m*v1</code> should work as expected).</p>
  22. <hr />
  23. <h2>4</h2>
  24. <p>Using meta programming, write a templated class <code>Prime</code>, which evaluates whether a literal integer constant (e.g. <code>7</code>) is prime or not at compile time.</p>
  25. <p>How can you be sure that your implementation of class <code>Prime</code> has been evaluated at compile time?</p>
  26. <hr />
  27. <h2>5</h2>
  28. <p>Write a class <code>LinkedList</code> which stores zero or more positive integers internally as a linked list on the heap. The class should provide appropriate constructors and destructors and a method <code>pop()</code> to remove items from the head of the list. The method <code>pop()</code> should return <code>-1</code> if there are no remaining items. Your implementation should override the copy constructor and assignment operator to copy the linked list structure between class instances. You might like to test your implementation with the following:</p>
  29. <pre>int main() {
  30. int test[] = {1,2,3,4,5};
  31. LinkList l1(test+1,4), l2(test,5);
  32. LinkList l3 = l2, l4;
  33. l4 = l1;
  34. printf("%d %d %d\n", l1.pop(), l3.pop(), l4.pop());
  35. return 0;
  36. }
  37. </pre>
  38. <p>Hint: heap allocation and deallocation should occur exactly once!</p>
  39. <h2>6</h2>
  40. <p>Why should destructors in an abstract class almost always be declared <code>virtual</code>?</p>
  41. <h2>7</h2>
  42. <p>Provide an implementation for:</p>
  43. <pre>
  44. Stack(const Stack&amp; s);
  45. Stack&amp; operator=(const Stack&amp; s);</pre>
  46. <p>as declared in the slides for lecture 12.</p>
  47. <h2>8</h2>
  48. <p>Provide an implementation for:</p>
  49. <pre>
  50. template&lt;class T&gt; T Stack&lt;T&gt;::pop();
  51. template&lt;class T&gt; Stack&lt;T&gt;::~Stack();</pre>
  52. <p>as declared in the slides for lecture 12.</p>
  53. <h2>9</h2>
  54. <p>A hardware engineer stores a FIFO queue of bits in an <code>int</code> on a platform with<br>
  55. 32-bit ints and 8-bit <code>char</code>s using the following C++ class:</p>
  56. <pre><code>class BitQueue {
  57. int valid_bits; // the number of valid bits held in queue
  58. int queue; // least significant bit is most recent bit added
  59. public:
  60. BitQueue(): valid_bits(0), queue(0) {}
  61. void push(int val, int bsize);
  62. int pop(int bsize);
  63. int size();
  64. };
  65. </code></pre>
  66. <ol>
  67. <li>
  68. <p>Write an implementation of <code>BitQueue::size</code> which should return the number<br>
  69. of bits currently held in <code>queue</code>.</p>
  70. </li>
  71. <li>
  72. <p>Write an implementation of <code>BitQueue::push</code> which places the <code>bsize</code> least<br>
  73. significant bits from <code>val</code> onto <code>queue</code> and updates <code>valid_bits</code>. An<br>
  74. exception should be thrown in cases where data would otherwise be lost.</p>
  75. </li>
  76. <li>
  77. <p>Write an implementation of <code>BitQueue::pop</code>, which takes <code>bsize</code> bits from<br>
  78. queue, provides them as the <code>bsize</code> least significant bits in the return<br>
  79. value, and updates <code>valid_bits</code>. An exception should be thrown when any<br>
  80. requested data is unavailable.</p>
  81. </li>
  82. <li>
  83. <p>The hardware engineer has built a communication device together with a C++<br>
  84. library function <code>send</code> to transmit data with the following declaration:<br>
  85. void send(char);<br>
  86. Use the <code>BitQueue</code> class to write a C++ definition for:</p>
  87. <pre><code> void sendmsg(const char* msg);
  88. </code></pre>
  89. <p>Each of the characters in <code>msg</code> should be encoded, in index order, using the<br>
  90. following binary codes: <code>'a'=0</code>, <code>'b'=10</code>, <code>'c'=1100</code>, and <code>'d'=1101</code>. All<br>
  91. other characters should be ignored. Successive binary codes should be<br>
  92. bit-packed together and the code <code>111</code> should be used to denote the end of<br>
  93. the message. Chunks of 8-bits should be sent using the send function and any<br>
  94. remaining bits at the end of a message should be padded with zeros. For<br>
  95. example, executing</p>
  96. <pre><code> sendmsg("abcd");
  97. </code></pre>
  98. <p>should call the send function twice, with the binary values <code>01011001</code><br>
  99. followed by <code>10111100</code>.<br>
  100. </p>
  101. </li>
  102. </ol>
  103. </body>
  104. </html>