NAME : Prajwal Prashant Charthad
ROLL NO: 133
Assignment no :4
Title: 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 a publication. 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 classes , allows user to
enter data and displays the data members. If an exception is caught, replace all the
data member values with zero values.
Program:
#include
<iostream>
#include <string>
#include<stdexce
pt>
usingnamespacestd;
classPublication{ protecte
d:
stringtitle
;
floatprice
;
public:
Publication():title(""),price(0.
0){} virtual void getData() {
try{
cout<<"Entertitle:
"; [Link]();
getline(cin, title);
cout<<"Enterprice:
"; cin >> price;
if([Link]())throwruntime_error("Invalidpriceinput");
} catch (...)
{ title = "";
price=0.0;
[Link]();
[Link](10000,'\n');
cout<<"Error:[Link]."<<endl;
}
}
virtual void displayData()
const {cout <<"Title: "<<
title << endl; cout<<"Price:
$"<<price<<en
};
classBook:publicPublication{ pri
vate:
intpageCount;
public:
Book() : pageCount(0)
{}
voidgetData()override{
Publication::getData();
try{
cout<<"Enterpagecount:"
; cin >> pageCount;
if([Link]())throwruntime_error("Invalidpagecountinput");
} catch (...)
{ title = "";
price=0.0;
pageCount=0;
[Link]();
[Link](10000,'\n');
cout<<"Error:[Link]."<<endl;
}
}
voiddisplayData()constoverri
de{ Publication::displayData(
);
cout<<"PageCount:"<<pageCount<<endl;
}
};
classTape:publicPublication{ pri
vate:
floatplayingTim
e; public:
Tape():playingTime(0
.0){} void getData()
override {
Publication::getData
(); try {
cout<<"Enterplayingtimeinminutes:
"; cin >> playingTime;
if([Link]())throwruntime_error("Invalidplayingtimeinput");
} catch (...)
{ title = "";
price=0.0;
playingTime = 0.0;
[Link]();
[Link](10000,'\
n');
cout<<"Error:[Link]."<<endl;
}
}
voiddisplayData()constoverri
de{ Publication::displayData(
);
cout<<"PlayingTime(minutes):"<<playingTime<< endl;
}
};
int main()
{
Bookboo
k; Tape
tape;
cout<<"EnterdataforBook:"<<endl;
[Link](); cout<<"\
nDisplayingBookdata:"<<endl;
[Link](); cout<<"\
nEnterdataforTape:"<<endl; [Link]();
cout<<"\nDisplayingTapedata:"<<endl;
[Link]();
return0;
}
OUTPUT: