xCircle

Book Store program using Classes and Exception Handling

Dec 23rd, 2022 (edited)
1,377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | Source Code | 0 0
  1. // Practical 3:
  2. // @author xCircle (Pranav)
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7. class Publication
  8. {
  9.     public:
  10.     string title;
  11.     float price;
  12.  
  13.     void update()
  14.     {
  15.         cout<<"Enter the Title of the book : ";
  16.         cin>>title;
  17.         cout<<"\nEnter the price of : ";
  18.         cin>>price;
  19.         if(price==0)
  20.         {
  21.             throw int(price);
  22.         }
  23.     }
  24.  
  25.     void publish()
  26.     {
  27.         cout<<"\nBook:- "<<title<<"\nPrice:- Rs"<<price;
  28.     }
  29. };
  30.  
  31. class Book : public Publication
  32. {
  33.     public:
  34.     int pageCount;
  35.  
  36.     void getData()
  37.     {
  38.         cout<<"\nEnter the No. of pages : ";
  39.         cin>>pageCount;
  40.         if(pageCount==0)
  41.         {
  42.             throw pageCount;
  43.         }
  44.     }
  45.  
  46.     void putData()
  47.     {
  48.         cout<<"\nPages:- "<<pageCount;
  49.     }
  50. };
  51.  
  52. class Tape : public Publication
  53. {
  54.     public:
  55.     float playTime;
  56.  
  57.     void getData()
  58.     {
  59.         cout<<"\nEnter the play time in minuites : ";
  60.         cin>>playTime;
  61.         if(playTime==0)
  62.         {
  63.             throw int(playTime);
  64.         }
  65.     }
  66.  
  67.     void putData()
  68.     {
  69.         cout<<"\nPlay Time:- "<<playTime<<"min";
  70.     }
  71. };
  72.  
  73. int main()
  74. {
  75.     Book b1;
  76.     Tape t1;
  77.     try{
  78.         b1.update();
  79.         b1.getData();
  80.         t1.getData();
  81.     }
  82.     catch(int e)
  83.     {
  84.         cout<<"*Error* \nException Caught";
  85.         b1.title="0";
  86.         b1.price=0;
  87.         b1.pageCount=0;
  88.         t1.playTime=0;
  89.     }
  90.     b1.publish();
  91.     b1.putData();
  92.     t1.putData();
  93. }
Tags: C99
Advertisement
Add Comment
Please, Sign In to add comment