given the product.h and product.cpp file below, i want you to Write derived product…
- User
given the product.h and product.cpp file below, i want you to Write derived product class book.h and book.cpp and implementing the keywords() which returns the appropriate keywords to index the product, displayString() [to create a string that contains the product info] and dump() [that outputs the database format of the product info]. #include #include #include "product.h"
using namespace std;
Product::Product(const std::string category, const std::string name, double price, int qty) : name_(name), price_(price), qty_(qty), category_(category) {
}
Product::~Product() {
}
double Product::getPrice() const { return price_; }
std::string Product::getName() const { return name_; }
void Product::subtractQty(int num) { qty_ -= num; }
int Product::getQty() const { return qty_; }
/**
- default implementation...can be overriden in a future
- assignment */ bool Product::isMatch(std::vectorstd::string& searchTerms) const { return false; }
void Product::dump(std::ostream& os) const { os << category_ << "\n" << name_ << "\n" << price_ << "\n" << qty_ << endl; } #ifndef PRODUCT_H #define PRODUCT_H #include #include #include #include #include
class Product { public: Product(const std::string category, const std::string name, double price, int qty); virtual ~Product();
/** * Returns the appropriate keywords that this product should be associated with */ virtual std::set<std::string> keywords() const = 0; /** * Allows for a more detailed search beyond simple keywords */ virtual bool isMatch(std::vector<std::string>& searchTerms) const; /** * Returns a string to display the product info for hits of the search */ virtual std::string displayString() const = 0; /** * Outputs the product info in the database format */ virtual void dump(std::ostream& os) const; /** * Accessors and mutators */ double getPrice() const; std::string getName() const; int getQty() const; void subtractQty(int num);protected: std::string name_; double price_; int qty_; std::string category_;
}; #endif
- Assistant
- User
is the constructor for the product has to be that long? i meant can i just push isbn and author in the product since it is a derived class?
- Assistant
The design of constructors in a derived class like
Bookinvolves some considerations regarding the initialization of both the base and derived class members. Let's address each aspect:Constructor Design
-
Base Class Initialization:
- When you derive a class from a base class, the base class part must be initialized using its constructor. In the
Bookclass, theProductpart must be initialized using the constructor. This is mandatory because the base class may have private members that can only be initialized through its constructor.
- When you derive a class from a base class, the base class part must be initialized using its constructor. In the
-
- User
but the constructor of book will call the constructor of product correct?
- Assistant
Yes, that's correct. In C++, when you create an instance of a derived class, such as
Book, the constructor of the derived class will call the constructor of its base class, , to ensure that all attributes inherited from the base class are initialized properly. - User
why do you include it there if it is called automatically
- Assistant
- User
so for the constructor for book i have to include everything that product has? i cannot just include author and isbn in the parameter? is that ture
- Assistant
- User
why in the parameter it is isbn but in the private it is isbn_ (with a underdash_
- Assistant
The use of naming conventions like appending an underscore () to private member variable names is a common programming practice to distinguish between member variables (attributes) and other variables within a class or function. It helps to clarify which variables are class members and which are local variables or parameters within methods.