supervision_2.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Programming in C and C++ -- 2018/19 -- Supervision 2</title>
  5. </head>
  6. <body>
  7. <h1>Programming in C and C++ (2018/19)<br/>Supervision 2</h1>
  8. <h2>1</h2>
  9. <p>If a function <code>f</code> has a static instance of a class as a local variable, when might the class constructor be called?</p>
  10. <h2>2</h2>
  11. <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>
  12. <h2>3</h2>
  13. <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>
  14. <h2>4</h2>
  15. <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>
  16. <h2>5</h2>
  17. <p>How can you be sure that your implementation of class <code>Prime</code> has been evaluated at compile time?</p>
  18. <h2>6</h2>
  19. <p>Write an implementation of a class <code>LinkList</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>
  20. <pre>int main() {
  21. int test[] = {1,2,3,4,5};
  22. LinkList l1(test+1,4), l2(test,5);
  23. LinkList l3 = l2, l4;
  24. l4 = l1;
  25. printf("%d %d %d\n", l1.pop(), l3.pop(), l4.pop());
  26. return 0;
  27. }
  28. </pre>
  29. <p>Hint: heap allocation and deallocation should occur exactly once!</p>
  30. <h2>7</h2>
  31. <p>Why should destructors in an abstract class almost always be declared <code>virtual</code>?</p>
  32. <h2>8</h2>
  33. <p>Provide an implementation for:</p>
  34. <pre>
  35. Stack(const Stack&amp; s);
  36. Stack&amp; operator=(const Stack&amp; s);</pre>
  37. <p>as declared in the slides for lecture 7.</p>
  38. <h2>9</h2>
  39. <p>Provide an implementation for:</p>
  40. <pre>
  41. template&lt;class T&gt; T Stack&lt;T&gt;::pop();
  42. template&lt;class T&gt; Stack&lt;T&gt;::~Stack();</pre>
  43. <p>as declared in the slides for lecture 7.</p>
  44. <h2>10</h2>
  45. <p>A hardware engineer stores a FIFO queue of bits in an <code>int</code> on a platform with<br>
  46. 32-bit ints and 8-bit <code>char</code>s using the following C++ class:</p>
  47. <pre><code>class BitQueue {
  48. int valid_bits; // the number of valid bits held in queue
  49. int queue; // least significant bit is most recent bit added
  50. public:
  51. BitQueue(): valid_bits(0), queue(0) {}
  52. void push(int val, int bsize);
  53. int pop(int bsize);
  54. int size();
  55. };
  56. </code></pre>
  57. <ol>
  58. <li>
  59. <p>Write an implementation of <code>BitQueue::size</code> which should return the number<br>
  60. of bits currently held in <code>queue</code>.</p>
  61. </li>
  62. <li>
  63. <p>Write an implementation of <code>BitQueue::push</code> which places the <code>bsize</code> least<br>
  64. significant bits from <code>val</code> onto <code>queue</code> and updates <code>valid_bits</code>. An<br>
  65. exception should be thrown in cases where data would otherwise be lost.</p>
  66. </li>
  67. <li>
  68. <p>Write an implementation of <code>BitQueue::pop</code>, which takes <code>bsize</code> bits from<br>
  69. queue, provides them as the <code>bsize</code> least significant bits in the return<br>
  70. value, and updates <code>valid_bits</code>. An exception should be thrown when any<br>
  71. requested data is unavailable.</p>
  72. </li>
  73. <li>
  74. <p>The hardware engineer has built a communication device together with a C++<br>
  75. library function <code>send</code> to transmit data with the following declaration:<br>
  76. void send(char);<br>
  77. Use the <code>BitQueue</code> class to write a C++ definition for:</p>
  78. <pre><code> void sendmsg(const char* msg);
  79. </code></pre>
  80. <p>Each of the characters in <code>msg</code> should be encoded, in index order, using the<br>
  81. following binary codes: <code>'a'=0</code>, <code>'b'=10</code>, <code>'c'=1100</code>, and <code>'d'=1101</code>. All<br>
  82. other characters should be ignored. Successive binary codes should be<br>
  83. bit-packed together and the code <code>111</code> should be used to denote the end of<br>
  84. the message. Chunks of 8-bits should be sent using the send function and any<br>
  85. remaining bits at the end of a message should be padded with zeros. For<br>
  86. example, executing</p>
  87. <pre><code> sendmsg("abcd");
  88. </code></pre>
  89. <p>should call the send function twice, with the binary values <code>01011001</code><br>
  90. followed by <code>10111100</code>.<br>
  91. </p>
  92. </li>
  93. </ol>
  94. </body>
  95. </html>