Q1. Create a base class named Book. Data fields include title and author; functions ......................................?

Q1. 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 Non Fiction, 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 main() function that demonstrates the use of the classes and their functions.

                                            ANSWER 


#include <iostream>
#include <string>

using namespace std;
class Book
{
protected:
    string title;
    string author;

public:
    Book(string Title = " ", string Author = " ")
    {
        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 : public Book
{
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, int number) : 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()
{

    Book first("Alphabate", "People");

    Fiction second("Algebra", "Pythagoras", 3);

    NonFiction third("Alice in Wonderland", "Lewis Carroll", 284);

    cout << "Firstbook:" << endl;

    first.display();
    cout << endl;
    cout << "Second fictionbook:" << endl;
    second.display();
    cout << endl;
    cout << "Third nonfictionbook:" << endl;
    third.display();
    return 0;
}

  
                                                         OUTPUT 
  
Firstbook:
Title:Alphabate
Author:People

Second fictionbook:
Title:Algebra
Author:Pythagoras
Numeric grade reading level:2

Third nonfictionbook:
Title:Alice in Wonderland
Author:Lewis Carroll
Hold the number of pages:300

                                                            OUTPUT IN TARMINAL




                                               THANKS FOR WATCHING

Comments

Popular posts from this blog

Basic Data types | HackerRank Solution...

There are three people sitting in a room - Alice, Bob, and Charlie. They need to decide on the temperature to set on the air conditioner. Everyone has a demand each:

Using HTML CSS and JavaScript Create a Responsive Personal Portfolio Website Design