liveBook · Manning

FictionAttrs

As explained in Object-Oriented Software Design in C++

A book-specific class representing attributes and matching behavior specialized for fiction books.

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

How Object-Oriented Software Design in C++ covers it

How deep it goes
Defines inheritance, construction, accessors, and layered matching, while the broader subclass design later exposes runtime type-checking costs.
Examples
Uses Little Women with year 1868 and Genre::CLASSICS as a construction example.
What you can do after
Apply it via C++ inheritance to construct fiction attributes and compare shared and fiction-specific fields.
How it connects
Connects Attributes, Book, inheritance, polymorphism, and the Open-Closed Principle.

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.