Preference: An Enumerated Type
- Enumerations (Section 19.3)
- Types (Section 19.5)
- Conversions (Section 19.6)
Your assignment is to design, implement, and test a Preference class.
Preference is a class that models one field in an ID3 tag. It is intended to permit the user to specify the quality of an MP3 file (e.g., excellent, good, fair, poor). We need a uniform preference system that will enumerate a fixed set of value choices so that comparisons and subrange queries are possible.
Here is the public interface of the class definition to get you started.
public: Preference(int value=0); Preference(QString prefstr); /** If possible, set the host value from the given string and @return true. Otherwise return false. */ bool fromString(QString); /** @return a list of all the acceptable Preference names, ordered by value (increasing) */ virtual QStringList getNames() const;
The Assignment
- Complete the class definition in preference.h.
- Implement all the functions in preference.cpp so that your class can pass the test case below.
- Write a main.cpp that calls this test case and tests your Preference class.
- Generate or write a qmake project file that can build the application.
- Verify that make dist creates a dist target (a tarball).
A test case that shows how the Preference class must work is provided in Example 25.2. Your Preference class must pass that test.
Example 25.2. ../src/libs/filetagger/testpreference.cpp
#include "testpreference.h" #include "preference.h" #include "qstd.h" using namespace qstd; void TestPreference::test() { Preference verygood("Very Good"); Preference verygood2("Very Good"); Preference excellent("Excellent"); Preference fair("Fair"); Preference good("Good"); Preference none("None"); Preference poor("Poor"); Preference badtaste("Bad Taste"); Preference undefined("undefined"); ASSERT_EQUALS(verygood, verygood2); ASSERT_NOTEQUALS(verygood, fair); ASSERT_EQUALS(undefined, 0); ASSERT_TRUE(none > undefined); ASSERT_TRUE(poor < none); ASSERT_TRUE(badtaste < poor); ASSERT_TRUE(verygood > good); ASSERT_TRUE(good > fair); ASSERT_TRUE(fair > none); ASSERT_TRUE(fair < verygood); ASSERT_TRUE(verygood < excellent); ASSERT_EQUALS(verygood.toString(), "Very Good"); qDebug() << verygood.toString(); Preference q("notsogood"); ASSERT_EQUALS(0, (int)q); qDebug() << q.getNames().join(", "); /* Optional - Case Ignore conversions? */ // Does this print "fair is fair" or "fair is 4"? cout << "Fair is " << fair; Preference verygoodlc("very good"); ASSERT_EQUALS(verygood, verygoodlc); } |
The header file is provided to you in Example 25.3 for completeness.
Example 25.3. ../src/libs/filetagger/testpreference.h
#include
|
When the test case is run, the output should look like this.
Категории |