This assignment consists of two parts. In the first part, you need to fix a piece of code that has two major problems with object-oriented design and memory management. This part is very short — your fix should be less than 10 lines of code. In the second part of the assignment, you are going to implement two classes describing quite useful mathematical objects — cplexes and qnions. This second part is longer and is primarily focused on operator overloading.
For the first part of this assignment, you need to fix a piece of object-oriented C++ code written by a person having poor knowledge of both OOP and memory management. You are given file bad-tweet.cpp that contains class tweet (both its interface and implementation, although it is not a very good design) and a small program that uses tweet. class tweet represents a short text message, similar to those seen on Twitter (hence, the name). A tweet includes a text message, the length of this message, and a timestamp.
The small program tries to create three tweets having equal timestamps and different text messages, print their contents on the screen and, finally, dispose the tweets properly. A timestamp is assigned to the tweet at the moment of its creation, inside the constructor. Thus, if we just create three individual tweet objects, one after another, they all will have different timestamps. In order to resolve this problem, the author of the code decided to do the following: a dummy tweet is created, and then each new tweet is duplicated from the dummy (in code, it is called "proto-tweet"). Thus, each copy will have the same timestamp as the dummy, and, initially, same text messages. In order to make three tweets to have different messages, method set_text is called for each of them, passing appropriate texts.
There are two major problems with this code. The first problem can be easily seen with the naked eye. If we look at the printed tweets, while they indeed have the same timestamp (which is correct), they also have the same text message (which is wrong):
tweet { len = 26 text = 'Is it friday yet?' timestamp = 'Tue Aug 20 00:23:17 2013' } tweet { len = 46 text = 'Is it friday yet?' timestamp = 'Tue Aug 20 00:23:17 2013' } tweet { len = 17 text = 'Is it friday yet?' timestamp = 'Tue Aug 20 00:23:17 2013' }
The second problem becomes obvious as soon as you check this program with valgrind for memory usage errors. In particular, valgrind finds three incidents of a memory block having been leaked (that is, allocated, but not released):
valgrind --tool=memcheck --leak-check=full ./bad-tweet ==18152== ==18152== HEAP SUMMARY: ==18152== in use at exit: 36 bytes in 3 blocks ==18152== total heap usage: 22 allocs, 19 frees, 5,833 bytes allocated ==18152== ==18152== 12 bytes in 1 blocks are definitely lost in loss record 1 of 3 ... ==18152== LEAK SUMMARY: ==18152== definitely lost: 36 bytes in 3 blocks ==18152== ==18152== ERROR SUMMARY: 3 errors from 3 contexts
Your goal is to fix both problems making as little code modifications as possible. (An ideal solution for this part of the assignment involves writing less than 10 lines of code.) Here are a few rules-hints:
In C++, most common operators, such as + or -, are usually used with objects of mathematical nature — integers, reals, and so forth. For the second part of this assignment, you will implement two (interrelated) classes that describe two kinds of mathematical objects — cplexes and qnions. (While these names look fictional, objects of a very similar nature are actively used by mathematicians, physicists, and math-aware computer scientists.)
A cplex is just a pair of real numbers (doubles, in C++ words). The first of these two numbers is usually called the real part or Re, and the second is the imaginary part or Im. For example, for a cplex x = (3, -5.2), its real part is Re(x) = 3, and its imaginary part is Im(x) = -5.2.
For cplexes, the following operations are defined (there are others, but for this assignment, we are interested only in these few):
cplex is the first class you need to implement. The interface for this class is given in cplex.h — do not change it. You are also given file cplex.cpp containing the implementation of class cplex, but this file is almost empty. Your task is to implement everything mentioned in cplex.h and not implemented in cplex.cpp (a few methods/operators are implemented for you). All the operators mentioned in cplex.h reflect the operations defined above. There is no operator for taking the absolute value, so there is a dedicated method abs. There are also a few methods that allow to get/set Re and Im of a cplex.
The second class you need to implement describes a qnion. A qnion is just a pair of cplexes, which makes the structure of a qnion somewhat similar to the one of a cplex (which is a pair of reals). These two cplexes are referred to as parts of a qnion. However, unlike the parts of a cplex, the parts of a qnion do not have fancy names, so we are going to call the first part a and the second part b. For example, for a qnion {(1, 2); (3, 4)}, part a is cplex (1, 2) and part b is cplex (3, 4).
The following operations are defined for qnions. Since we already know how cplexes work, the following operations are expressed in terms of operations on cplexes (and this is how operations on qnions should be coded):
The interface for class qnion is given in qnion.h, and you do not need to change it. Implementation is in qnion.cpp, and you need to complete it. Your goal is to implement everything declared in qnion.h and not implemented in qnion.cpp. When implementing operators, such as addition, for qnions, you are not allowed to directly work with real and imaginary parts of cplexes qnions consist of. Just use the high-level operations on cplexes, such as their addition and multiplication.
To test your code, you are provided with two googletest test suites. One contains tests for cplex, another contains tests for qnion. You are also given a short example.cpp just to give you an idea of how we can use qnions in code. In order to runs tests, the procedure is similar to the one from the previous programming assignment:
make gtest
make test
./cplex_tests
./qnion_tests
Submit cplex.cpp and qnion.cpp. Do not forget to write your names in the header. As usually, log in to csil.cs.ucsb.edu and run
turnin pa3@cs32 ./bad-tweet.cpp ./cplex.cpp ./qnion.cppand follow instructions. You can submit your work multiple times. Submissions are not accepted after the deadline.