Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++, Create a base class named Book. Data fields include title and author;

ID: 3851926 • Letter: U

Question

Using C++,

Create a base class named Book. Data fields include title and author; functions include those that can set and display the fields.

Derive two classes from the Book class: Fiction, which also contains a numeric grade reading level, and NonFiction, which contains a variable to hold the number of pages.

The functions that set and display data field values for the subclasses should call the appropriate parent class functions to set and display the common fields, and include specific code pertaining to the new subclass fields.

Write a test program that demonstrates the use of the classes and their functions.

Explanation / Answer

#include<iostream>
#include<string>
 
using namespace std;
class Book
{
protected:
    string title;
    string author;
public:
    Book(string Title="", stringAuthor="") {title=Title;author=Author;}
    void setTitle(string newTitle){title=newTitle;}
    void setAuthor(string newAuthor){author=newAuthor;}
    void display() {cout<<"Title:"<<title<<endl; cout<<"Author:
"<<author<<endl;}

};
class Fiction: publicBook
{
    private:
    int level;//numeric grade reading level
    public:
        Fiction(string Title, string Author,int Level): Book(Title,Author){level=Level;}
        void setLevel(int Level) {level=Level;}
        void display() {Book::display();cout<<"Numeric grade reading level:
"<<level<<endl;}

};
 
class NonFiction:public Book
{
private:
    int numbersPages;//hold the number of pages
public:
    NonFiction(string Title, string Author, intnumber): Book(Title,Author){numbersPages=number;}
    void setNumber(int Number){numbersPages=Number;}
    void display() {Book::display();cout<<"Hold the number of pages:
"<<numbersPages<<endl;}

};
 
int main()
{
 
    Bookfirst("Alphabet","People");
    Fictionsecond("Algebra","Pythagoras",3);
    NonFiction third("Alice in Wonderland","Lewis Carroll",150);
    cout<<"Firstbook:"<<endl;
    first.display();
 
    cout<<"Second fictionbook:"<<endl;
    second.display();
 
    cout<<"Third nonfictionbook:"<<endl;
    third.display();
    return 0;
}

this code is saved as :"books.cpp"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote