Question

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

  1. 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 main()func- tion that demonstrates the use of the classes and their functions. Save the file as Books.cpp.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is your code...

#include<iostream>
#include<string>
using namespace std;
class Book
{
protected:
string ttl;
string aut;
public:
Book(string title="", string author="")
{
ttl=title;
aut=author;
}
void setTitle(string newTitle)
{
ttl=newTitle;
}
void setAuthor(string newAuthor)
{
aut=newAuthor;
}
void display()
{
cout<<"Title:"<<ttl<<endl; cout<<"Author:"<<aut<<endl;
}
};
class Fiction: public Book
{
private:
int numericgradelevel;
public:
Fiction(string Title, string Author,int Level): Book(Title,Author){numericgradelevel=Level;}
void setLevel(int Level)
{
numericgradelevel=Level;
}
void display()
{
Book::display();
cout<<"Numeric grade reading level:"<<numericgradelevel<<endl;}
};

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

int main()
{
Book ob1("The Haunted House","Richard");
Fiction ob2("The Night at Hotel","James",5);
NonFiction ob3("Zeology","Dr.Christine",578);
cout<<"Firstbook:"<<endl;
ob1.display();
cout<<"Second fictionbook:"<<endl;
ob2.display();
cout<<"Third nonfictionbook:"<<endl;
ob3.display();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Create a base class named Book. Data fields include title and author; functions include those that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class named Book that contains data fields for the title and number of pages....

    Create a class named Book that contains data fields for the title and number of pages. Include get and set methods for these fields. Next, create a subclass named Textbook, which contains an additional field that holds a grade level for the Textbook and additional methods to get and set the grade level field. Write an application that demonstrates using objects of each class. Save the files as Book.java, Textbook.java, and DemoBook.java.

  • A java class named Book contains: instance data for the title, author, publisher and copyright year...

    A java class named Book contains: instance data for the title, author, publisher and copyright year a constructor to accept and initialize the instance data variables set and get methods a toString() method to return a formatted, multi-line description of the book Produce a child class that inherits from Book. The class is called Dictionary. Dictonary must include: instance data that describes the number of words in the dictionary a constructor that takes in all information needed to describe a...

  • Create a class named Poem that contains the following fields: title - the name of the...

    Create a class named Poem that contains the following fields: title - the name of the poem (of type String) lines - the number of lines in the poem (of type int) Include a constructor that requires values for both fields. Also include get methods to retrieve field values. Create three subclasses: Couplet, Limerick, and Haiku. The constructor for each subclass requires only a title; the lines field is set using a constant value. A couplet has two lines, a...

  • Create a class named Horse that contains data fields for the name, color, and birth year....

    Create a class named Horse that contains data fields for the name, color, and birth year. Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field that holds the number of races in which the horse has competed and additional methods to get and set the new field. Write an application that demonstrates using objects of each class. Save the files as Horse.java, RaceHorse.java, and DemoHorses.java. Program 1 Submission Template Fields:...

  • 1. Create a class named Pizza with data fields dor desceiption ( such as sasuage and...

    1. Create a class named Pizza with data fields dor desceiption ( such as sasuage and onion) and price. include a constructor fhay requires arguments for borh fields ans a method to diplay the data. Create a subclass names DelicedPizza that inherits from Pizza bit adds a delivery fee and a delicery address. The description, price, and delicery address are required as arguments to the constructor. The delivery fee is $3 if the pizza ordered cost more that $15; otherwise...

  • Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a...

    Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races(of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. DemoHorses.java public class DemoHorses { public static void main(String args[])...

  • C++ Program 1. Create a class named BaseballGame that has fields for two team names and...

    C++ Program 1. Create a class named BaseballGame that has fields for two team names and a final score for each team. Include methods to set and get the values for each data field. Your application declares an array of 12 BaseballGame objects. Prompt the user for data for each object, and display all the values. Then pass each object to a method that displays the name of the winning team or “Tie” if the score is a tie. (main.cpp,...

  • C++ Visul Studio Create a class named Vehicle. The class has the following five member variables:...

    C++ Visul Studio Create a class named Vehicle. The class has the following five member variables: • Vehicle Name • Vehicle number • Sale Tax • Unit price • Total price Include set (mutator) and get (accessor) functions for each field except the total price field. The set function prompt the user for values for each field. This class also needs a function named computePrice() to compute the total price (quantity times unit price + salesTax) and a function to...

  • Create a class named BaseballGame that contains data fields for two team names and scores for...

    Create a class named BaseballGame that contains data fields for two team names and scores for each team in each of nine innings. names should be an array of two strings and scores should be a two-dimensional array of type int; the first dimension indexes the team (0 or 1) and the second dimension indexes the inning. Create get and set methods for each field. The get and set methods for the scores should require a parameter that indicates which...

  • C# Programming : Create a housing application for a property manager. Include a base class named...

    C# Programming : Create a housing application for a property manager. Include a base class named Housing. Include data characteristics such as address and year built. Include a virtual method that returns the total projected rental amount. Define an interface named IUnits that has a method that returns the number of units. The MultiUnit class should implement this interface. Create subclasses for MultiUnit and SingleFamily. SingleFamily should include characteristics such as size in square feet and availability of garage. MultiUnit...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT