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

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

ID: 3566220 • Letter: C

Question

Create a base class named Book. Data fields include title and author; functions include those that can set, get 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 main()function that instantiate an object of class Book, an object class Fiction, and an object of class NonFiction and display the field of each class. Save the file as NameProjectS.cpp.

Explanation / Answer

#include<iostream>

using namespace std;

class book

{

public:

char title[20],author[20];

void set()

{

cout<<"Enter title and author:";

cin>>title;

cin>>author

}

void get()

{

}

void display()

{

cout<<title<<" "<<author;

}

};

class fiction::public book

{

int gradelevel;

public:

void set()

{

book.set();

cin>>gradelevel;

}

void display()

{

book.display();

cout<<" "<<gradelevel;

}

};

class nonfiction::public book

{

int no_of_pgs;

public:

void set()

{

book.set();

cin>>no_of_pgs;

}

void display()

{

book.display();

cout<<" "<<no_of_pgs;

}

};

int main()

{

fiction b1;

nonfiction b2;

b1.set();

b1.display();

b2.set();

b2.display();

}