Question

In: Computer Science

Please answer it in C++ (a) Declare a class called Book representing a textbook.      A...

Please answer it in C++

(a) Declare a class called Book representing a textbook.     

A textbook has a title (eg. “Programming in C++”)

A textbook has an author (eg. “Helen Johnstone”)

A textbook has a size in number of pages (eg. 550)

A textbook has a price (eg. $135.99).

These attributes should be represented as the data members of the class and be hidden from the outside of the class.

The class should also have a constructor used to initialise the data members of the objects of this class when they are created. The default values for the data members will be set to 0 for all numerical data members and to “unknown” for the string data members.

The class should also have a void member function called show() with no parameters which, when invoked will print the book attributes on a single line, on the screen.

(b) Given the class definition in part a above, write two statements that would create two Book objects b1 and b2, first one with the default characteristics, and the second one with the following characteristics:

            Title: Thinking in C++

            Author: Bruce Eckel

            Size: 575 pages

            Price: $45.00

Solutions

Expert Solution

Please see the below Output:

Please find the below code:

// C++ program to demonstrate 
// accessing of data members 

#include <bits/stdc++.h> 
using namespace std; 
class Book 
{ 
        // Access specifier 
        private: 

        // Data Members 
        string title; 
        string author; 
        int numberOfPages;
        double price;
    
    public: 
     //Constructor
     Book()
     {
         title = "Unknown";
         author = "Unknown";
         numberOfPages = 0;
         price = 00.00;
     }
     
        // Member Functions() 
        void show() 
        { 
          cout << "Title: " << title << "  Author: " << author  << " Size: " << numberOfPages << " pages " << " Price: $" << price <<endl; 
        } 
        
        // Member Functions() 
        void show(string stitle, string sauthor, int inumberofpage, double dprice) 
        { 
          cout << "Title: " << stitle <<endl << "Author: " << sauthor <<endl << "Size: " << inumberofpage << " pages "<<endl << "Price: $" << dprice <<endl; 
        } 
}; 

int main() { 

        // Declare an object of class geeks 
        Book obj1; 
        Book obj2;
        
        // accessing member function 
        obj1.show(); 
        
                // accessing member function 
        obj2.show("Thinking in C++","Bruce Eckel",575,45.00); 
        
        return 0; 
} 

Related Solutions

Complete the following using c++ (a) Declare a class called Capacitor representing the electronic component capacitor,...
Complete the following using c++ (a) Declare a class called Capacitor representing the electronic component capacitor, which has the following characteristics: Capacitance in Farads (example value: 0.47 mF) Voltage rating in Volts (example value: 25.0 V) Temperature rating in Celcius (example value: 85.0 oC) Type ("Paper", "Polyester", "Electrolytic", "Ceramic"). These attributes should be represented as the data members of the class and be hidden from the outside of the class. The class should also have a constructor used to initialise...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Model a book Draw a class diagram representing a book defined by the following statement: “A...
Model a book Draw a class diagram representing a book defined by the following statement: “A book is composed of a number of parts, which in turn are composed of a number of chapters. Chapters are composed of sections.” First, focus only on classes and associations. Add multiplicity to the class diagram you produced. Refine the class diagram to include the following attributes: • Book includes a publisher, publication date, and an ISBN • Part includes a title and a...
Create “New Class…” named Book to store the details of a book Declare 3 fields for...
Create “New Class…” named Book to store the details of a book Declare 3 fields for the details of the book: field named author of type String field named title of type String field named callNumber of type String Add overloaded constructors with the following headers and make sure each constructor has only 1 statement in the body that makes an internal method call to setBookDetails: public Book(String author, String title, String callNumber) public Book(String author, String title) HINT: Initialize...
How does one declare a class variable in C#?
How does one declare a class variable in C#?
Please solve in C++ only class is not templated You need to write a class called...
Please solve in C++ only class is not templated You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element 2.public void remove(int index); // removes the item from the list that has the given index 3.public void remove(Object item); // finds the item from...
. Create a class called Book to represent a book. A Book should include four pieces...
. Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. Inaddition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information...
Task : (Please Answer in C#) Class Polynomials The class Polynomials is a collection of polynomials...
Task : (Please Answer in C#) Class Polynomials The class Polynomials is a collection of polynomials of either implementation stored in an instance of the generic library class List. class Polynomials { private List L; // Creates an empty list L of polynomials public Polynomials ( ) { … } // Retrieves the polynomial stored at position i in L public Polynomial Retrieve (int i) { … } // Inserts polynomial p into L public void Insert (Polynomial p) {...
Please Use C++ to finish as the requirements. Implement a class called SinglyLinkedList. In the main...
Please Use C++ to finish as the requirements. Implement a class called SinglyLinkedList. In the main function, instantiate the SinglyLinkedList class. Your program should provide a user loop and a menu so that the user can access all the operators provided by the SinglyLinkedList class. DestroyList InitializeList GetFirst InsertFirst, InsertLast, Insert DeleteFirst, DeleteLast, Delete IsEmpty Length Print, ReversePrint
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT