You are on page 1of 5

//book.

#ifndef BOOK_H

#define BOOK_H

#include <string>

class Book {

private:

std::string title;

std::string author;

int year;

public:

Book(const std::string& title, const std::string& author, int year);

std::string getTitle() const;

std::string getAuthor() const;

int getYear() const;

};

#endif
//book.cpp

#include "Book.h"

Book::Book(const std::string& title, const std::string& author, int year)

: title(title), author(author), year(year) {}

std::string Book::getTitle() const {

return title;

std::string Book::getAuthor() const {

return author;

int Book::getYear() const {

return year;

}
//library.h

#ifndef LIBRARY_H

#define LIBRARY_H

#include <vector>

#include "Book.h"

class Library {

private:

std::vector<Book> books;

public:

void addBook(const Book& book);

void listBooks() const;

};

#endif
//library.cpp

#include "Library.h"

#include <iostream>

void Library::addBook(const Book& book) {

books.push_back(book);

void Library::listBooks() const {

std::cout << "Books in the library:\n";

for (const auto& book : books) {

std::cout << "Title: " << book.getTitle() << ", Author: " << book.getAuthor() << ", Year: " <<
book.getYear() << "\n";

}
//main.cpp

#include <iostream>

#include "Library.h"

int main() {

Library library;

Book book1("The Great Gatsby", "F. Scott Fitzgerald", 1925);

Book book2("To Kill a Mockingbird", "Harper Lee", 1960);

library.addBook(book1);

library.addBook(book2);

library.listBooks();

return 0;

You might also like