FictionAttrs represents the attributes unique to fiction books while inheriting shared title and author fields from Attributes. It stores year and genre, and extends matching by checking those fields after the common attributes match. section 2.3.3
Listing 2.12 (Program 2.3 Books-3): FictionAttrs.h
#include <iostream>
#include <string>
#include "Attributes.h"
using namespace std;
enum class Genre ...
inline ostream& operator <<(ostream& ostr, const Genre& genre) ...
class FictionAttrs : public Attributes
{
public:
FictionAttrs(const string title,
const string last, const string first,
const int yr, const Genre gen)
: Attributes(title, last, first), #1
Year(yr), genre(gen) {} #2
int get_year() const { return year; }
Genre get_genre() const { return genre; }
bool is_match(const FictionAttrs& target_attrs) const
{
if (!Attributes::is_match(target_attrs)) return false; #3
return ( (target_attrs.get_year() == 0) #4
|| (target_attrs.get_year() == year)) #4
&& ( (target_attrs.get_genre() == Genre::UNSPECIFIED) #4
|| (target_attrs.get_genre() == genre)); #4
}
private:
int year; #5
Genre genre; #5
};
inline ostream& operator <<(ostream& ostr, const FictionAttrs& attrs)
{
ostr << "{TITLE: '" << attrs.get_title()
<< "', LAST: '" << attrs.get_last()
<< "', FIRST: '" << attrs.get_first()
<< "', YEAR: " << attrs.get_year()
<< ", GENRE: " << attrs.get_genre() << "}";
return ostr;
}
#1 First initialize the common attributes.
#2 Then initialize the year and genre for fiction books.
#3 First match the common attributes.
#4 Then match the year and genre for fiction books.
#5 Fiction book year and genre attributes
The declaration shows constructor delegation to Attributes, fiction-specific initialization, accessors, and the two-stage is_match() operation. section 2.3.3
Adding a fiction book
catalogue.add(new FictionAttrs("Little Women", "Alcott", "Louisa", 1868, Genre::CLASSICS)); section 2.3.3
