|
@@ -0,0 +1,112 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+
|
|
|
+<html>
|
|
|
+
|
|
|
+<head>
|
|
|
+ <title>Programming in C and C++ -- 2018/19 -- Supervision 2</title>
|
|
|
+</head>
|
|
|
+
|
|
|
+<body>
|
|
|
+ <h1>Programming in C and C++ (2018/19)<br/>Supervision 2</h1>
|
|
|
+
|
|
|
+ <h2>1</h2>
|
|
|
+ <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>
|
|
|
+
|
|
|
+ <h2>2</h2>
|
|
|
+ <p>Write a class <code>Matrix</code> which allows a programmer to define 2 × 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>
|
|
|
+
|
|
|
+ <h2>3</h2>
|
|
|
+ <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>
|
|
|
+
|
|
|
+ <h2>4</h2>
|
|
|
+ <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>
|
|
|
+
|
|
|
+ <h2>5</h2>
|
|
|
+ <p>How can you be sure that your implementation of class <code>Prime</code> has been evaluated at compile time?</p>
|
|
|
+
|
|
|
+ <h2>6</h2>
|
|
|
+ <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>
|
|
|
+<pre>int main() {
|
|
|
+ int test[] = {1,2,3,4,5};
|
|
|
+ LinkList l1(test+1,4), l2(test,5);
|
|
|
+ LinkList l3 = l2, l4;
|
|
|
+ l4 = l1;
|
|
|
+ printf("%d %d %d\n", l1.pop(), l3.pop(), l4.pop());
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+</pre>
|
|
|
+<p>Hint: heap allocation and deallocation should occur exactly once!</p>
|
|
|
+
|
|
|
+ <h2>7</h2>
|
|
|
+
|
|
|
+ <h2>8</h2>
|
|
|
+ <p>Why should destructors in an abstract class almost always be declared <code>virtual</code>?</p>
|
|
|
+
|
|
|
+ <h2>9</h2>
|
|
|
+ <p>Provide an implementation for:</p>
|
|
|
+ <pre>
|
|
|
+ Stack(const Stack& s);
|
|
|
+ Stack& operator=(const Stack& s);</pre>
|
|
|
+ <p>as declared in the slides for lecture 7.</p>
|
|
|
+
|
|
|
+ <h2>10</h2>
|
|
|
+ <p>Provide an implementation for:</p>
|
|
|
+ <pre>
|
|
|
+ template<class T> T Stack<T>::pop();
|
|
|
+ template<class T> Stack<T>::~Stack();</pre>
|
|
|
+ <p>as declared in the slides for lecture 7.</p>
|
|
|
+
|
|
|
+ <h2>11</h2>
|
|
|
+ <p>A hardware engineer stores a FIFO queue of bits in an <code>int</code> on a platform with<br>
|
|
|
+ 32-bit ints and 8-bit <code>char</code>s using the following C++ class:</p>
|
|
|
+ <pre><code>class BitQueue {
|
|
|
+ int valid_bits; // the number of valid bits held in queue
|
|
|
+ int queue; // least significant bit is most recent bit added
|
|
|
+public:
|
|
|
+ BitQueue(): valid_bits(0), queue(0) {}
|
|
|
+ void push(int val, int bsize);
|
|
|
+ int pop(int bsize);
|
|
|
+ int size();
|
|
|
+};
|
|
|
+</code></pre>
|
|
|
+ <ol>
|
|
|
+ <li>
|
|
|
+ <p>Write an implementation of <code>BitQueue::size</code> which should return the number<br>
|
|
|
+ of bits currently held in <code>queue</code>.</p>
|
|
|
+ </li>
|
|
|
+ <li>
|
|
|
+ <p>Write an implementation of <code>BitQueue::push</code> which places the <code>bsize</code> least<br>
|
|
|
+ significant bits from <code>val</code> onto <code>queue</code> and updates <code>valid_bits</code>. An<br>
|
|
|
+ exception should be thrown in cases where data would otherwise be lost.</p>
|
|
|
+ </li>
|
|
|
+ <li>
|
|
|
+ <p>Write an implementation of <code>BitQueue::pop</code>, which takes <code>bsize</code> bits from<br>
|
|
|
+ queue, provides them as the <code>bsize</code> least significant bits in the return<br>
|
|
|
+ value, and updates <code>valid_bits</code>. An exception should be thrown when any<br>
|
|
|
+ requested data is unavailable.</p>
|
|
|
+ </li>
|
|
|
+ <li>
|
|
|
+ <p>The hardware engineer has built a communication device together with a C++<br>
|
|
|
+ library function <code>send</code> to transmit data with the following declaration:<br>
|
|
|
+ void send(char);<br>
|
|
|
+ Use the <code>BitQueue</code> class to write a C++ definition for:</p>
|
|
|
+ <pre><code> void sendmsg(const char* msg);
|
|
|
+ </code></pre>
|
|
|
+ <p>Each of the characters in <code>msg</code> should be encoded, in index order, using the<br>
|
|
|
+ following binary codes: <code>'a'=0</code>, <code>'b'=10</code>, <code>'c'=1100</code>, and <code>'d'=1101</code>. All<br>
|
|
|
+ other characters should be ignored. Successive binary codes should be<br>
|
|
|
+ bit-packed together and the code <code>111</code> should be used to denote the end of<br>
|
|
|
+ the message. Chunks of 8-bits should be sent using the send function and any<br>
|
|
|
+ remaining bits at the end of a message should be padded with zeros. For<br>
|
|
|
+ example, executing</p>
|
|
|
+ <pre><code> sendmsg("abcd");
|
|
|
+ </code></pre>
|
|
|
+ <p>should call the send function twice, with the binary values <code>01011001</code><br>
|
|
|
+ followed by <code>10111100</code>.<br>
|
|
|
+ </p>
|
|
|
+ </li>
|
|
|
+ </ol>
|
|
|
+
|
|
|
+</body>
|
|
|
+
|
|
|
+</html>
|