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 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
// One class is to hold the information for a DVD
class DVD
{
public:
DVD() = default ;
DVD( std::string title, double len, int year,
std::string first_actor = "", std::string second_actor = "" )
: title(title), year(year), length(len),
firstMainActor(first_actor), secondMainActor(second_actor)
{
if( year < 1600 ) year = 1600 ;
if( length < 0 ) length = 0 ;
}
const std::string& album_name() const { return title ; }
// etc.
private:
std::string title = "unspecified" ;
int year = 2019 ;
double length = 0 ;
std::string firstMainActor;
std::string secondMainActor;
// https://en.cppreference.com/w/cpp/language/operators#Stream_extraction_and_insertion
friend std::ostream& operator<< ( std::ostream& stm, const DVD& dvd )
{
return stm << "dvd{ title: " << std::quoted(dvd.title)
<< ", year: " << dvd.year
<< ", duration: " << dvd.length << " minutes }" ;
}
};
// The other class is supposed to maintain the dvd collection
class dvd_collection
{
public:
void add( DVD dvd ) { collection.push_back( std::move(dvd) ) ; }
void add( std::string title, double len, int year,
std::string first_actor = "", std::string second_actor = "" )
{
// https://en.cppreference.com/w/cpp/container/vector/emplace_back
collection.emplace_back( title, len, year, first_actor, second_actor ) ;
}
void print() const
{
for( const DVD& dvd : collection ) std::cout << dvd << '\n' ;
}
// TO DO: erase/remove
private:
std::vector<DVD> collection ; // https://cal-linux.com/tutorials/vectors.html
};
int main()
{
dvd_collection my_dvds ;
my_dvds.add( "Stalker (Tarkovsky)", 180, 1979 ) ;
my_dvds.add( "Tokyo Story (Ozu)", 135, 1953 ) ;
my_dvds.add( "Werckmeister Harmoniak (Tarr)", 160, 2000 ) ;
my_dvds.print() ;
}
|