In: Computer Science
Write a program in C++ using the following 2 Files: Book.h and Source.cpp
1.) In Book.h, declare a struct Book with the following variables:
- isbn: string
- title: string
- price: float
2.) In main (Source.cpp), declare a Book object named: book
- Use an initialization list to initialize the object with these values:
isbn à 13-12345-01
title à “Great Gatsby”
price à 14.50
3.) Pass the Book object to a function named: showBook
- The function display the book.
(Note: Objects should almost always be passed by reference)
- The function does not return a value.
/* OUTPUT
Here is the book:
ISBN: 13-12345-01
Title: Great Gatsby
Price: $14.50
*/
Code:
Book.h
#ifndef BOOK_H
#define BOOK_H
#include <string> // use to declare string
// struct book
struct Book
{
std::string isbn;
std::string title;
float price;
};
#endif
Source.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "Book.h"
using namespace std;
// function to show book details
void showBook(struct Book *book)
{
cout << "Here is the book:" << endl;
cout << "ISBN: " << book->isbn <<
endl;
cout << "Title: " << book->title
<< endl;
// price is declared upto two decimal places
cout << "Price: $" << setprecision(2)
<< fixed << book->price << endl;
}
int main()
{
// book object declared using initilizaiton list
struct Book myBook = {"13-12345-01", "Great Gatsby",
14.50};
// passing above book object by reference to print
it's details
showBook(&myBook);
return 0;
}
OUTPUT: