Problem Statement: 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.
Note:- Scroll horizontally to see the full line of code.
#include<iostream>
#include<string.h>
using namespace std;
class publication{
protected:
string title;
float price;
public:
publication(){
title="Publication name";
}
~publication(){
// cout<<"Inside Destructor!!!";
}
};
class book:public publication{
protected:
int pages;
public:
void putdata(){
cout<<"Enter the Book details below:"<<endl;
cout<<"Title: ";
cin>>title;
cout<<"Price: ";
cin>>price;
try {
cout << "Pages: ";
cin>>pages;
if (pages < 0)
{
throw pages;
}
}
catch (int pages ) {
cout << "Exception Caught \n";
title="0";
price=0;
this->pages=0;
}
}
void getdata(){
cout<<"The Book details are as follows:"<<endl;
cout<<"Title: "<<title<<endl;
cout<<"Price: "<<price<<endl;
cout<<"Pages: "<<pages<<endl;
}
~book(){
// cout<<"Inside destructor!!!";
}
};
class tape:public publication{
protected:
float minutes;
public:
tape(){
minutes=0;
}
void putdata(){
cout<<"Enter the Tape details below:"<<endl;
cout<<"Title: ";
cin>>title;
cout<<"Price: ";
cin>>price;
try {
cout << "Minutes: ";
cin>>minutes;
if (minutes < 0)
{
throw minutes;
}
}
catch (float minutes ) {
cout << "Exception Caught \n";
title="0";
price=0;
this->minutes=0;
}
}
void getdata(){
cout<<"The Tape details are as follows:"<<endl;
cout<<"Title:"<<title<<endl;
cout<<"Price:"<<price<<endl;
cout<<"Minutes: "<<minutes<<endl;
}
~tape(){
// cout<<"Inside destructor!!!";
}
};
int main(){
book a;
tape b;
a.putdata();
a.getdata();
b.putdata();
b.getdata();
return 0;
}
Comments
Post a Comment