In: Computer Science
Please answer it in C++
(a) Declare a class called Book representing a textbook.
A textbook has a title (eg. “Programming in C++”)
A textbook has an author (eg. “Helen Johnstone”)
A textbook has a size in number of pages (eg. 550)
A textbook has a price (eg. $135.99).
These attributes should be represented as the data members of the class and be hidden from the outside of the class.
The class should also have a constructor used to initialise the data members of the objects of this class when they are created. The default values for the data members will be set to 0 for all numerical data members and to “unknown” for the string data members.
The class should also have a void member function called show() with no parameters which, when invoked will print the book attributes on a single line, on the screen.
(b) Given the class definition in part a above, write two statements that would create two Book objects b1 and b2, first one with the default characteristics, and the second one with the following characteristics:
Title: Thinking in C++
Author: Bruce Eckel
Size: 575 pages
Price: $45.00
Please see the below Output:
Please find the below code:
// C++ program to demonstrate
// accessing of data members
#include <bits/stdc++.h>
using namespace std;
class Book
{
// Access specifier
private:
// Data Members
string title;
string author;
int numberOfPages;
double price;
public:
//Constructor
Book()
{
title = "Unknown";
author = "Unknown";
numberOfPages = 0;
price = 00.00;
}
// Member Functions()
void show()
{
cout << "Title: " << title << " Author: " << author << " Size: " << numberOfPages << " pages " << " Price: $" << price <<endl;
}
// Member Functions()
void show(string stitle, string sauthor, int inumberofpage, double dprice)
{
cout << "Title: " << stitle <<endl << "Author: " << sauthor <<endl << "Size: " << inumberofpage << " pages "<<endl << "Price: $" << dprice <<endl;
}
};
int main() {
// Declare an object of class geeks
Book obj1;
Book obj2;
// accessing member function
obj1.show();
// accessing member function
obj2.show("Thinking in C++","Bruce Eckel",575,45.00);
return 0;
}