In particular, I would like to be able to use ostream operator <<in two derived classes from the base class.
The program I am creating must print product details for different "products" in a "virtual store." Among the products are two different types of books. Each of these books should contain its own:
ID number
Author
NumberOfPages
Year
In addition, the type ChildrensBookmust contain a minimum age, and TextBookmust contain a grade.
I defined a class Bookand received classes from it ChildrensBookand TextBook. My question is to use ostream operator <<for printing information.
Can I define a common <function in the Book class that will print all the information common to both derived classes and then refer to it when overriding <in derived classes?
For instance,
ostream& operator<<(ostream& bookOutput, const Book& printBook) {
return bookOutput << printBook.ID << "Name " << printBook.name << "year:" << printBook.year";
}
And then in the derived class somehow:
ostream& operator<<(ostream& TextBookOutput, const TextBook& printTextBook) {
return TextBookOutput << "TextBook: "
<< "[Here is where I want to print out all the details of the book that are members of the base class]" << "Grade:" << printTextBook.grade;
}
So, I think my question can be summarized as follows: can I call the parent statement from the child statement, and if so, what syntax do I use?
Another idea that arose for me was to write a function for a child element that uses the parent print statement, and then call that function from the child print statement. This would mean that I did not try to call the operator when overriding it, but still calls for using the parent operator and redefining the child operator separately.