Answer to Question #225308 in C++ for Anuj

Question #225308

Write a program to define a class book that will contain Title, Author and Price of the book as data members. Define Null Constructor, parameterized constructor and copy constructor for the class and a function to display the details of an object. Use the new operator to initialize object of this class through a pointer and display the data member through a member function.


1
Expert's answer
2021-08-15T04:06:03-0400
#include <iostream>

using namespace std;

// corecudr
class Book {
private:
    string title, author;
    int price;
public:
    Book() {}
    Book(string title, string author, int price) : title(title), author(author), price(price) {}

    // Copy constructor
    Book(const Book &b) {
        title = b.title;
        author = b.author;
        price = b.price;
    }

    void display() {
        cout << "Title: " << title << '\n'
        << "Author: " << author << '\n'
        << "Price: " << price << '$' << '\n';
    }
};

int main()
{
    Book* book = new Book("Harry Potter", "J. K. Rowling", 14);
    book->display();

    return 0;
}

Output:
Title: Harry Potter
Author: J. K. Rowling
Price: 14$

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
New on Blog
APPROVED BY CLIENTS