fork download
  1. // C++ Program to create
  2. // a vector of class objects
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. // Adapted from Google AI: Printing c++ vector with class object example
  10. // IDEOne link: 68BQzj
  11.  
  12. class Book {
  13.  
  14. private:
  15.  
  16. // data members
  17. string title; // The title of the book
  18. string author; // The primary author of the book
  19. int copyRightYear; // The copyright year for the book
  20. float price; // The price of the book (US Dollars)
  21.  
  22. public:
  23.  
  24. // member function prototypes
  25. void assign (string, string, int, float);
  26. string getTitle();
  27. string getAuthor();
  28. int getCopyRightYear();
  29. float getPrice();
  30.  
  31. // Friend Function: Declaring the operator as a friend allows it to access
  32. // private data members (like title, author, copyright year and price) directly.
  33.  
  34. // Declare the stream insertion operator as a friend function
  35. // to allow it to access private members
  36. friend ostream& operator<<(ostream& out, const Book& b);
  37.  
  38. }; // class Book
  39.  
  40. // operator Overload: By overloading the << operator for your custom class, you define
  41. // a standard way for your objects to interact with output streams like std::cout.
  42. // This makes the printing code in main much cleaner.
  43.  
  44. // Overload the stream insertion operator (<<) outside the class
  45. // This function tells `std::cout` how to print a Book object
  46. ostream& operator<<(ostream& out, const Book& b) {
  47. out << "\"" << b.title << "\" by " << b.author << " published in " <<
  48. b.copyRightYear << " for " << "$" << b.price;
  49. return out; // Important to return the output stream
  50. }
  51.  
  52. // A "constructor" that will create a new book
  53. void Book::assign (string bookTitle, string bookAuthor, int bookDate, float bookPrice)
  54. {
  55. title = bookTitle;
  56. author = bookAuthor;
  57. copyRightYear = bookDate;
  58. price = bookPrice;
  59. }
  60.  
  61. // A "getter" function that will retrieve the title value in the book class
  62. string Book::getTitle() {
  63. return title;
  64. }
  65.  
  66. // A "getter" function that will retrieve the primary book author value
  67. string Book::getAuthor() {
  68. return author;
  69. }
  70.  
  71. // A "getter" function that will retrieve the year the book was copyrighted
  72. int Book::getCopyRightYear() {
  73. return copyRightYear;
  74. }
  75.  
  76. // A "getter" function that will retrieve the list price of the book
  77. float Book::getPrice() {
  78. return price;
  79. }
  80.  
  81. int main()
  82. {
  83. vector<Book> library; // This declares a vector to store/process Book class objects
  84. int n; // number of books to read in from input
  85. string myTitle; // storage to read in book title from input
  86. string myAuthor; // storage to read in book author from input
  87. int myCopyRightYear; // storage to read in book copyright year from input
  88. float myPrice; // storage to read in book price from input
  89.  
  90. cout << "Enter number of books: ";
  91. cin >> n;
  92.  
  93. // Read in each book from standard input, create an object, and push object data into vector
  94. for (int i = 0; i < n; ++i) {
  95.  
  96. Book b; // Note that book object gets allocated, utilized, and then deallocated
  97. // through each loop interation (essentially reused)
  98.  
  99. // read in book attribute information from standard input
  100. cout << "\nEnter Title: ";
  101. std::getline(std::cin >> std::ws, myTitle); // account for new line characters
  102.  
  103. cout << "\nEnter Author: ";
  104. std::getline(std::cin >> std::ws, myAuthor); // account for new line characters
  105.  
  106. cout << "\nEnter Copy Right Year: ";
  107. cin >> myCopyRightYear;
  108.  
  109. cout << "\nEnter Price: ";
  110. cin >> myPrice;
  111.  
  112. // call contructor to create the Book object with the inputted information
  113. b.assign (myTitle, myAuthor, myCopyRightYear, myPrice);
  114.  
  115. // push the Book object into the vector
  116. library.push_back(b);
  117.  
  118. } // for
  119.  
  120.  
  121. // if our Book class data was public instead of private, you could just access
  122. // each book item directly from the vector, using a simple for loop as shown
  123. // below.
  124.  
  125. // for (i=0; i < library.size(); i++) {
  126. // cout << library[i].title << " " << library[i].author << ... and so on
  127. // }
  128.  
  129. // ... but since our data members are private, use this type of loop
  130.  
  131. // Range-based for loop: for (const auto& l : library) is a modern C++ way to
  132. // iterate over every element in a container. const auto& is efficient and prevents
  133. // accidental modification of the objects while printing.
  134. cout << "\n\n--- List of Books ---\n" << endl;
  135. for (const auto& l : library) {
  136. // The overloaded operator << is called automatically for each object
  137. cout << l << endl;
  138. } // for
  139.  
  140. } // main
  141.  
Success #stdin #stdout 0.01s 5320KB
stdin
2
Learn C Programming
Tim Niesen
2026
54.99
The Dude Abides
Jeffrey Lebowski
2012
34.99
stdout
Enter number of books: 
Enter Title: 
Enter Author: 
Enter Copy Right Year: 
Enter Price: 
Enter Title: 
Enter Author: 
Enter Copy Right Year: 
Enter Price: 

--- List of Books ---

"Learn C Programming" by Tim Niesen published in 2026 for $54.99
"The Dude Abides" by Jeffrey Lebowski published in 2012 for $34.99