Answer to Question #50461 in C++ for zexy

Question #50461
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.
1
Expert's answer
2015-01-21T02:53:45-0500
Program books.cpp:

#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;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS