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.
#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$
Comments
Leave a comment