-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet04.cpp
More file actions
111 lines (91 loc) · 2.62 KB
/
Copy pathSet04.cpp
File metadata and controls
111 lines (91 loc) · 2.62 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* [OOP Practical Eaxm 2025 - Set 04]
a) You are required to design a Library Book Management System using OOP.
Create a base class Book with: Data members: title, author, price
Constructors: Default constructor, Parameterized constructor. A function showBook() to display details.
b) Derive two classes: TextBook: Additional data: subject, Override showBook()
class ReferenceBook: Additional data: edition, Override showBook()
Write a program to: Create objects of both derived classes
Use a Book* pointer to call showBook() (runtime polymorphism)
c) Overload the + operator in the Book class to add prices of two books:
float operator+(Book b);
Usage:
float total = bl + b2: Display the total price.
d) In main():
Create 1 TextBook and 1 ReferenceBook
Display both using base class pointer
Add their prices using overloaded + operator
Display final amount to pay
*/
#include <iostream>
using namespace std;
class Book {
protected:
string title;
string author;
float price;
public:
Book() {
title = "NULL";
author = "NULL";
price = 0.0;
}
Book(string t, string a, float p) {
title = t;
author = a;
price = p;
}
// Virtual function for runtime polymorphism
virtual void showBook() {
cout << "Title : " << title << endl;
cout << "Author : " << author << endl;
cout << "Price : Rs. " << price << endl;
}
// Operator Overloading (+)
float operator+(Book b) {
return this->price + b.price;
}
virtual ~Book() {}
};
class TextBook : public Book {
private:
string subject;
public:
TextBook(string t, string a, float p, string s)
: Book(t, a, p) {
subject = s;
}
void showBook() override {
cout << "\n--- Text Book Details ---\n";
Book::showBook();
cout << "Subject: " << subject << endl;
}
};
class ReferenceBook : public Book {
private:
int edition;
public:
ReferenceBook(string t, string a, float p, int e)
: Book(t, a, p) {
edition = e;
}
void showBook() override {
cout << "\n--- Reference Book Details ---\n";
Book::showBook();
cout << "Edition: " << edition << endl;
}
};
int main() {
TextBook tb("OOP with C++", "Bjarne Stroustrup", 450.0, "Computer Science");
ReferenceBook rb("Database System", "Korth", 650.0, 7);
// Base class pointer
Book* b;
// Runtime Polymorphism
b = &tb;
b->showBook();
b = &rb;
b->showBook();
// Operator Overloading usage
float totalPrice = tb + rb;
cout << "\nTotal Amount to Pay: Rs. " << totalPrice << endl;
return 0;
}