1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <string>
#include <memory>
class Item {
public:
using pointer_type = std::shared_ptr<Item>;
using string_type = std::string;
private:
// int isbn_;
string_type isbn_ ; // shouldn't this be a string?
string_type title_;
bool status_ = false;
string_type borrower_;
public:
Item( string_type isbn, string_type title, bool status = false, string_type borrower = {} )
: isbn_( std::move(isbn) ), title_( std::move(title) ), status_(status), borrower_( std::move(borrower) ) {}
virtual ~Item() = default ;
Item( const Item& ) = default ;
Item( Item&& ) = default ;
Item& operator= ( const Item& ) = default ;
Item& operator= ( Item&& ) = default ;
virtual string_type to_string() const // *** const
{
return "isbn: " + isbn_ + ", title: '" + title_ + "', status: " +
( status_ ? "true" : "false" ) + ", borrower: '" + borrower_ + "'" ;
}
virtual std::ostream& print_to( std::ostream& stm ) const
{ return stm << type_name() + "{ " << to_string() << " }" ; }
protected: virtual string_type type_name() const { return "Item" ; }
friend std::ostream& operator<< ( std::ostream& stm, const Item& it )
{ return it.print_to(stm) ; }
};
struct Book : Item {
public:
Book( string_type isbn, string_type title, string_type author,
bool status = false, string_type borrower = {} )
: Item( std::move(isbn), std::move(title), status, std::move(borrower) ),
author_( std::move(author) ) {}
virtual string_type to_string() const override
{ return Item::to_string() + ", author: '" + author_ + "'" ; }
protected: virtual string_type type_name() const override { return "Book" ; }
private: string_type author_ ;
};
int main()
{
Item a( "978-1491903995", "Effective Modern C++" ) ;
Book b( "978-0321992789", "PPP-2", "Stroustrup", true, "masterinex" ) ;
std::cout << a << '\n' << b << '\n' ;
}
|