HI everyone,
I am doing my first programming project using C++ and found out a sample project from Jeremy Sam’s github. I would love to understand it more thoroughly. In this project, he designed an inventory management system for a book shop which is used for Seller (can update price and stock, check transactions) and Customers (can view book detail, purchase book) . Could anyone explain more in detail some of my concerns as below:
- Line 35, 36: Why should we use the constructor for these public variables here?
class book
{
private:
int bookcode;
string author;
string title;
double price;
string publisher;
int stock;
public:
book(int u,string x,string y,string z, double w, int v) //constructor
{
bookcode=u;
title=x;
author=y;
publisher=z;
price=w;
stock=v;
-
Should we define functions such as Update (price and stock of a book) in private variables because those should not be accessible without an authorized person (seller), and we define functions such as Search Book (current searching method by Author and Book’s title) and function Show Book Detail in public variables because those should be publicly accessible (from both Customer and Seller side)?
-
Line 174: Our data has 5 books (p1, p2, p3, p4, p5). The search function for books available for purchase is given as below. Does that mean the process will be (1) combine the book p6 constructor with user-input of author and name, (2) get a search function from that constructor object and compare author and publisher name with each book from p1 to p5 and (3) return the result accordingly.
{
string a;
string b;
cout<<"Enter author name: ";
cin>>a;
cout<<"Enter publisher name: ";
cin>>b;
book p6(a,b);
if(p6.search(p1)==1)
p1.noOfcopies();
Currently this is how the book is searched from Customer’s side and declared in public:
public:
book(string x,string y) //constructor
{
author=x;
publisher=y;
}
int search(book x) //searching the book in the list
{
if(author==x.author&&publisher==x.publisher)
return 1;
else
return 0;
book p1(111,"Computer_Architecture","William_Stallings","Pearson",211.5,10);
book p2(112,"Operating_Systems","Abraham_Silberchatz","Wiley",250,14);
book p3(113,"C++","Herbert_Shildt","McGraw_Hill",312.5,14);
book p4(114,"Digital_Electronics","Morris_Mano","Pearson",156,7);
book p5(115,"Data_Structures","Ellis_Horowitz","University_Press",235,4);
Thank you