0% found this document useful (0 votes)
31 views1 page

C++ Book Class Example Code

The document contains a C++ program that defines a 'Book' class with attributes for title, author, and price. It includes methods to set and get the price, as well as to display book details. The main function creates a book object, displays its details, updates the price, and then shows the updated price.

Uploaded by

BilalSaqib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views1 page

C++ Book Class Example Code

The document contains a C++ program that defines a 'Book' class with attributes for title, author, and price. It includes methods to set and get the price, as well as to display book details. The main function creates a book object, displays its details, updates the price, and then shows the updated price.

Uploaded by

BilalSaqib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <iostream>

#include <string>
using namespace std;

class Book {
private:
string title;
string author;
double price;

public:
// Constructor
Book(string t, string a, double p) {
title = t;
author = a;
price = p;
}

// Method to set price


void setPrice(double p) {
price = p;
}

// Method to get price


double getPrice() {
return price;
}

// Method to show book details


void showDetails() {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "Price: $" << price << endl;
}
};

int main() {
Book b1("Object-Oriented Programming", "John Smith", 45.99);
[Link]();
[Link](50.00);
cout << "\nUpdated Price: $" << [Link]() << endl;
return 0;
}

You might also like