Repository navigation
Expand file tree
/
Copy patha3.cpp
More file actions
112 lines (96 loc) · 3.67 KB
/
Copy patha3.cpp
File metadata and controls
112 lines (96 loc) · 3.67 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
112
/* Imagine a publishing company which does marketing for book and audio cassette versions.
Create a class publication that stores the title (a string) and price (type float) of publications.
From this class derive two classes: book which adds a page count (type int) and tape which adds a playing
time in minutes (type float). Write a program that instantiates the book and tape class,
allows user to enter data and displays the data members. If an exception is caught, replace all the
data member values with zero values. */
#include <iostream>
using namespace std;
// To create a class Publication that stores title and price of publication
class Publication {
public:
string title;
float price;
// Default constructor to initailise title and price
Publication() {
title = " ";
price = 0.0;
cout << "\nEnter the title of the publication." << endl;
cin >> title;
cout << "\nEnter the price of the publication." << endl;
cin >> price;
}
};
// To derive properties of base class Publication into derived class Book
class Book : public Publication {
public:
int pagecount;
// Default constructor to initailise page count of book
Book() {
pagecount = 0;
}
void getdata() {
// try: represents a block of code that can throw an exception
try {
cout << "\nEnter the number of pages of the book." << endl;
cin >> pagecount;
if(pagecount<=0) {
throw pagecount; // throw: used to throw an exception
}
}
// catch: represents a block of code executed when an exception is thrown
catch(...) { // special syntax to catch exception of any data type
cout << "Invalid page count!" << endl;
pagecount = 0;
}
}
void display() {
cout << "\nName of the book: " << title << endl;
cout << "Price of the book: " << price << endl;
cout << "Number of pages in the book: " << pagecount << endl;
}
};
// To derive properties of base class Publication into derived class Tape
class Tape : public Publication {
public:
float playtime;
// Default constructor to initailise play time of audio cassette
Tape() {
playtime = 0.0;
}
void getdata() {
// try: represents a block of code that can throw an exception
try{
cout << "\nEnter the playing time of the audio tape in minutes." << endl;
cin >> playtime;
if(playtime<=0) {
throw playtime; // throw: used to throw an exception
}
}
// catch: represents a block of code executed when an exception is thrown
catch(...) {
cout << "Invalid playtime!" << endl;
playtime = 0;
}
}
void display() {
cout << "\nName of the audio tape: " << title << endl;
cout << "Price of the audio tape: " << price << endl;
cout << "Playing time of the audio tape: " << playtime << " minutes" << endl;
}
};
int main() {
string ans;
cout << "\nIs the publication information about a book or an audio tape? (Enter book/audio)" << endl;
cin >> ans;
if(ans=="book") {
Book obj1; // Instantiation of class Book
obj1.getdata();
obj1.display();
}
else {
Tape obj2; // Instantiation of class Tape
obj2.getdata();
obj2.display();
}
}