Question

Write a class called Book that has two private member variables called page (integer) and topic...

Write a class called Book that has two private member variables called page (integer) and topic (string). It also has a static private member variable called count (integer). This class has only one constructor with default argument for page and topic. Write this constructor. Also overload the addition operator for this class such that it will add an integer value to the page variable of the book. For example when in main we say:

book2 = book1 + 4; then 4 will be added to the value of page. Also show how main can use the count variable to find out how many book objects are there.

The things that you should write:

1- class header defining member variables, addition operator overloading, setters, getters and constructor with default argument.

2- Implementation of constructor with default argument and the addition operator overloading as shown above.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>

using namespace std;

class Book {
private:
   int page;
   string topic;
public:
   static int count;
   Book();
   ~Book();
   void setPage(int p);
   void setTopic(string t);
   int getPage();
   string getTopic();
   Book &operator+(int num);
};

int Book::count = 0;

Book::Book() {
   count++;
   page = 0;
   topic = "";
}

Book::~Book() {
   count--;
}
void Book::setPage(int p) {
   page = p;
}
void Book::setTopic(string t) {
   topic = t;
}
int Book::getPage() {
   return page;
}
string Book::getTopic() {
   return topic;
}
Book &Book::operator+(int num) {
   page += num;
}

int main() {
   Book b1;
   Book b2;
   Book b;
   b.setPage(5);
   b.setTopic("topic");
   b = b+4;
   cout << "Page: " << b.getPage() << endl;
   cout << "Topic: " << b.getTopic() << endl;
   cout << Book::count << endl;
   return 0;
}

Page:9 Topic: topic Process exited after 0.1331 seconds with returnvalue 0 Press any key to continue - - -

Add a comment
Know the answer?
Add Answer to:
Write a class called Book that has two private member variables called page (integer) and topic...
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
  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • you will write a templated array class. Called MyArray with member variables ptr and array_size. This...

    you will write a templated array class. Called MyArray with member variables ptr and array_size. This array must use dynamic memory--see program shell, names should remain unchanged. The following is a list of required member functions of the array class along with a rubric: (1pts) program compiles (2pts) default constructor (2pts) parametered constructor which feeds MyArray a size. (10pts) copy constructor 8pts for performing a *deep* copy 2pts for correct syntax (10pts) overloaded = operator 7pts for functioning properly 3pts...

  • Create the class Book which has the following members: 1. private members: 1. title as String...

    Create the class Book which has the following members: 1. private members: 1. title as String 2. isbn as String 3. author as String 4. price as double 2. public members: 1. default constructor which initializes all data members to their default values. 2. non-default constructor which initializes all data members to parameters values. 3. toString which returns a representing String of the Book. 4. add all the setters and getters. Create the class EBook which is a subclass of...

  • Write a class declaration named Circle with a private member variable named radius.

     in C++ Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159*radius*radius. Add a default constructor to the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign its value to the radius...

  • Write a class declaration named Circle with a private member variable named radius.

    CODE IN C++Write a class declaration named Circle with a private member variable named radius. Write set and get functions to access the radius variable, and a function named getArea that returns the area of the circle. The area is calculated as 3.14159 * radius * radius. Add a default constructor the Circle class. The constructor should initialize the radius member to 0. Add an overloaded constructor to the Circle class. The constructor should accept an argument and assign radius...

  • Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'Mailorder...

    Question 3: Q3 (Polymorphism & virtual function) a) Consider the class, derived class, and the virtual functions as shown in Display for Q3. Create a derived class (of Sale) called 'MailorderSale' having one private member variable called 'shipping charge'. It should have constructor function and virtual function 'bill' (this function adds shipping charge to the price). Define all these functions (no need of default constructors). b) In the main function, define one object of DiscountSale with (11,10) initial values and...

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

  • JAVA Write a class called Pen that contains the following information: 1. Private instance variables for...

    JAVA Write a class called Pen that contains the following information: 1. Private instance variables for the price of the pen (float) and color of the pen (String). 2. A two-argument constructor to set each of the instance variables above. If the price is negative, throw an IllegalArgumentException stating the argument that is not correct. 3. Get and Set methods for each instance variable with the same error detection as the constructor. public class Pen {

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

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
ADVERTISEMENT