// C++ Program to create
// a vector of class objects
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Adapted from Google AI: Printing c++ vector with class object example
// IDEOne link: 68BQzj
class Book {
private:
// data members
string title; // The title of the book
string author; // The primary author of the book
int copyRightYear; // The copyright year for the book
float price; // The price of the book (US Dollars)
public:
// member function prototypes
void assign (string, string, int, float);
string getTitle();
string getAuthor();
int getCopyRightYear();
float getPrice();
// Friend Function: Declaring the operator as a friend allows it to access
// private data members (like title, author, copyright year and price) directly.
// Declare the stream insertion operator as a friend function
// to allow it to access private members
friend ostream& operator<<(ostream& out, const Book& b);
}; // class Book
// operator Overload: By overloading the << operator for your custom class, you define
// a standard way for your objects to interact with output streams like std::cout.
// This makes the printing code in main much cleaner.
// Overload the stream insertion operator (<<) outside the class
// This function tells `std::cout` how to print a Book object
ostream& operator<<(ostream& out, const Book& b) {
out << "\"" << b.title << "\" by " << b.author << " published in " <<
b.copyRightYear << " for " << "$" << b.price;
return out; // Important to return the output stream
}
// A "constructor" that will create a new book
void Book::assign (string bookTitle, string bookAuthor, int bookDate, float bookPrice)
{
title = bookTitle;
author = bookAuthor;
copyRightYear = bookDate;
price = bookPrice;
}
// A "getter" function that will retrieve the title value in the book class
string Book::getTitle() {
return title;
}
// A "getter" function that will retrieve the primary book author value
string Book::getAuthor() {
return author;
}
// A "getter" function that will retrieve the year the book was copyrighted
int Book::getCopyRightYear() {
return copyRightYear;
}
// A "getter" function that will retrieve the list price of the book
float Book::getPrice() {
return price;
}
int main()
{
vector<Book> library; // This declares a vector to store/process Book class objects
int n; // number of books to read in from input
string myTitle; // storage to read in book title from input
string myAuthor; // storage to read in book author from input
int myCopyRightYear; // storage to read in book copyright year from input
float myPrice; // storage to read in book price from input
cout << "Enter number of books: ";
cin >> n;
// Read in each book from standard input, create an object, and push object data into vector
for (int i = 0; i < n; ++i) {
Book b; // Note that book object gets allocated, utilized, and then deallocated
// through each loop interation (essentially reused)
// read in book attribute information from standard input
cout << "\nEnter Title: ";
std::getline(std::cin >> std::ws, myTitle); // account for new line characters
cout << "\nEnter Author: ";
std::getline(std::cin >> std::ws, myAuthor); // account for new line characters
cout << "\nEnter Copy Right Year: ";
cin >> myCopyRightYear;
cout << "\nEnter Price: ";
cin >> myPrice;
// call contructor to create the Book object with the inputted information
b.assign (myTitle, myAuthor, myCopyRightYear, myPrice);
// push the Book object into the vector
library.push_back(b);
} // for
// if our Book class data was public instead of private, you could just access
// each book item directly from the vector, using a simple for loop as shown
// below.
// for (i=0; i < library.size(); i++) {
// cout << library[i].title << " " << library[i].author << ... and so on
// }
// ... but since our data members are private, use this type of loop
// Range-based for loop: for (const auto& l : library) is a modern C++ way to
// iterate over every element in a container. const auto& is efficient and prevents
// accidental modification of the objects while printing.
cout << "\n\n--- List of Books ---\n" << endl;
for (const auto& l : library) {
// The overloaded operator << is called automatically for each object
cout << l << endl;
} // for
} // main