Skip to main content

OOP 3

 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.

The code for above problem is as follows:

   
 //@author: SPPU CSE GURU  
   
 #include <iostream>  
  using namespace std;  
  class Publication  
  {  
       private:  
            string title;  
            float price;  
       public:  
       Publication()               //constructor  
       {  
            title="";  
            price=0.0;  
       }  
       Publication(string title, float price)  
       {  
            this->title=title;  
            this->price=price;  
       }  
         
       void getData()  
       {  
            cout<<"\nEnter title and price\n";  
            cin>>title>>price;  
       }  
         
       void putData()  
       {  
            try  
            {  
                 if(title.length()<3)  
                      throw title;  
                        
                 if(price<=0.0)  
                      throw price;  
            }  
            catch(string)  
            {  
                 cout<<"\nError: Title below 3 characters is not allowed";  
                 title="";  
            }  
              
            catch(float f)  
            {  
                 cout<<"\nError: Price not valid: \t"<<f;  
                price=0.0;  
            }       
            cout<<"\nTitle is :"<<title;  
            cout<<"\nPrice is :"<<price;  
       }  
         
  };  
    
    
  class Book: public Publication  
  {  
       private:  
            int pages;  
       public:  
            Book():Publication()  
            {  
                 pages=0;  
            }  
            Book(string title, float price, int pages):Publication(title,price)  
            {  
                 this->pages=pages;  
            }  
              
       void getData()  
       {  
           Publication::getData();  
            cout<<"\nEnter no. of pages in book\n";  
            cin>>pages;  
       }  
         
       void putData()  
       {  
            Publication::putData();  
            try  
            {  
                 if(pages<0)  
                      throw pages;  
            }  
            catch(int f)  
            {  
                 cout<<"\nError: Pages not valid: \t"<<f;  
                 pages=0;  
            }  
         
       cout<<"\nPages are :"<<pages;  
       }  
         
   
  };  
    
  class Tape: public Publication  
  {  
       private:  
            float playtime;  
       public:  
       Tape():Publication()  
       {  
            playtime=0.0;  
      }  
       Tape(string title, float price, float playtime):Publication(title,price)  
       {  
            this->playtime=playtime;  
       }  
         
       void getData()  
       {  
            Publication::getData();  
            cout<<"\nEnter play time of Tape\n";  
            cin>>playtime;  
       }  
         
       void putData()  
       {  
            Publication::putData();  
            try  
           {  
                 if(playtime<0.0)  
                      throw playtime;  
            }  
            catch(float f)  
            {  
                 cout<<"\nError: Playtime not valid: \t"<<f;  
                 playtime=0.0;  
            }  
            cout<<"\nPlaytime is :"<<playtime;  
       }  
         
        
  };  
    
  int main()  
  {  
       Book book;  
      Tape tape;  
       cout<<"\n***************BOOK**************\n";  
       book.getData();  
       cout<<"\n***************TAPE**************\n";  
      tape.getData();  
       cout<<"\n***************BOOK**************\n";  
       book.putData();  
       cout<<"\n***************TAPE**************\n";  
       tape.putData();  
       return 0;  
  }  

Comments

Popular posts from this blog

Circle Triangle Pattern

  Group A   Practical 3 a   Problem Statement :   a) Write C++ program to draw the following pattern. Use DDA line and Bresenham‘s circle drawing algorithm. Apply the concept of encapsulation. Check Out Code Here  ðŸ‘‡ Output :  Code can get updated so also come back later to see if there is any changes. Also if there is any problem with code you can comment below. If you like it, do share with your friends.😊

DSL A2

  Write a Python program to store marks scored in subject “Fundamental of Data Structure” by N students in the class. Write functions to compute following:  a) The average score of class  b) Highest score and lowest score of class  c) Count of students who were absent for the test  d) Display mark with highest frequency The Code for above problem is as follows : """ @author: SPPU SE GURU """ total=int(input ("Enter total no. of students in your class ")) pre=int(input("No. of students appeared for FDS exam : ")) U=[] for i in range (total): i=i+1 U.append(i) R=[] for i in range(pre): roll=int(input("Enter Roll no. of students present for test :- ")) R.append(roll) M=[] for i in range(pre): print("Enter Roll no. ",R[i],end=" ") marks=int(input("Marks:- ")) M.append(marks) print("********** Solutions Are as fo...

DSL E29

 Data Structure Lab : Practical 29 : Problem Statement:  Queues are frequently used in computer programming, and a typical example is the creation of a job queue by an operating system. If the operating system does not use priorities, then the jobs are processed in the order they enter the system. Write C++ program for simulating job queue. Write functions to add job and delete job from queue Check Out Code Here  ðŸ‘‡ // Author : SPPU CSE GURU #include<iostream> using namespace std; class Queue{ private: int front,rear,max; public: int Q[10]; Queue() { front=0; rear=-1; max=10; } void EnQueue(); void DeQueue(); void Display(); }; void Queue::EnQueue() { int data; cout<<"Enter Job no. to Add "<<endl; cin>>data; if(rear...