-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03_book.cpp
86 lines (67 loc) · 1.7 KB
/
03_book.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// // Implement a program that involves a class Book with attributes bookId and price. Dynamically create an object of Book and use it.
// // Header files
#include <iostream>
#include <string.h>
// // use namespace
using namespace std;
// // define class Book
class Book
{
private:
// // instance member variables
unsigned int bookId;
double price;
public:
// // constructors
Book() {}
Book(int bookId, double price)
{
this->bookId = bookId;
this->price = price;
}
// // instance member function to set Book Id
void setBookId(int bookId)
{
if (bookId < 0) // if bookId is negative make it positive
bookId = -bookId;
this->bookId = bookId;
}
// // instance member function to get Book Id
unsigned int getBookId()
{
return bookId;
}
// // instance member function to set Price
void setPrice(double price)
{
if (price < 0) // if price is negative make it positive
price = -price;
this->price = price;
}
// // instance member function to get price
double getPrice()
{
return price;
}
};
// // Main Function Start
int main()
{
int bookId;
double price;
// // Get book book bookId
cout << "\nEnter Book Id => ";
cin >> bookId;
// // Get book Price
cout << "\nEnter Book Price => ";
cin >> price;
Book *b1 = new Book(bookId, price); // create an object of Book dynamically
cout << "\nBook Id => " << b1->getBookId();
cout << "\nBook Title => " << b1->getPrice();
// // deallocate memory
delete b1;
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End