Creating a class which uses the concept of inheritance, displays data and data members and uses the concept of exception handling using the C++ language.
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.
Objectives:
To learn the concept of inheritance and exception handling.
Algorithm:
- Start
- Create
classes Publication book and tape.
- Publication
class having data member title, price.
- Class
Book having data members pages and member functions getData() and putData().
- Class
Tape having data members minutes and member functions getData() and putData().
- Create
an object book class book and object of classtape.
- Stop.
Program:
# include<iostream>
# include<string.h>
using namespace std;
class publication // declaring class Publication
{
private:
string
title;
float
price;
public:
void
add()
{
cout
<< "\nEnter the Publication information : " << endl;
cout
<< "Enter Title of the Publication : ";
cin.ignore();
getline(cin,
title);
cout
<< "Enter Price of Publication : ";
cin
>> price;
}
void
display()
{
cout
<< "\n--------------------------------------------------";
cout
<< "\nTitle of Publication : " << title;
cout
<< "\nPublication Price : " << price;
}
};
class book : public publication // declaring class
book which inherits class publication in public mode.
{
private:
int
page_count;
public:
void
add_book()
{
try
{
add();
cout
<< "Enter Page Count of Book : ";
cin
>> page_count;
if
(page_count <= 0)
{
throw
page_count;
}
}
catch(...)
{
cout
<< "\nInvalid Page Count!!!";
page_count
= 0;
}
}
void
display_book()
{
display();
cout
<< "\nPage Count : " <<page_count;
cout
<< "\n--------------------------------------------------\n";
}
};
class tape : public publication // declaring class
tape which inherits class publication in public mode
{
private:
float
play_time;
public:
void
add_tape()
{
try
{
add();
cout
<< "Enter Play Duration of the Tape in minutes:";
cin
>> play_time;
if
(play_time <= 0)
throw
play_time;
}
catch(...)
{
cout
<< "\nInvalid Play Time!!!";
play_time
= 0;
}
}
void display_tape()
{
display();
cout
<< "\nPlay Time : " <<play_time << "
min";
cout
<< "\n--------------------------------------------------\n";
}
};
int main()
{
book
b1[10]; // object of class book
tape
t1[10]; // object of class tape
int
ch, b_count = 0, t_count = 0;
do
{
cout
<< "\n* * * * * PUBLICATION DATABASE SYSTEM * * * * *";
cout
<< "\n--------------------MENU-----------------------";
cout
<< "\n1. Add Information to Books";
cout
<< "\n2. Add Information to Tapes";
cout
<< "\n3. Display Books Information";
cout
<< "\n4. Display Tapes Information";
cout
<< "\n5. Exit";
cout
<< "\n\nEnter your choice : ";
cin
>> ch;
switch(ch)
{
case
1:
b1[b_count].add_book();
b_count
++;
break;
case
2:
t1[t_count].add_tape();
t_count
++;
break;
case
3:
cout
<< "\n* * * * BOOK PUBLICATION DATABASE SYSTEM * * * *";
for
(int j=0;j < b_count;j++)
{
b1[j].display_book();
}
break;
case
4:
cout
<< "\n* * * * TAPE PUBLICATION DATABASE SYSTEM * * * *";
for
(int j=0;j < t_count;j++)
{
t1[j].display_tape();
}
break;
case
5:
exit(0);
}
}while
(ch != 5);
return 0;
}
Output:
* * * * * PUBLICATION DATABASE SYSTEM * * * * *
--------------------MENU-----------------------
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Enter your choice : 1
Enter the Publication information :
Enter Title of the Publication : Object Oriented
Programing
Enter Price of Publication : 360
Enter Page Count of Book : 410
* * * * * PUBLICATION DATABASE SYSTEM * * * * *
--------------------MENU-----------------------
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Enter your choice : 2
Enter the Publication information :
Enter Title of the Publication : Storytel
Enter Price of Publication : 250
Enter Play Duration of the Tape in minutes:280
* * * * * PUBLICATION DATABASE SYSTEM * * * * *
--------------------MENU-----------------------
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Enter your choice : 3
* * * * BOOK PUBLICATION DATABASE SYSTEM * * * *
--------------------------------------------------
Title of Publication : Object Oriented Programing
Publication Price : 360
Page Count : 410
--------------------------------------------------
* * * * * PUBLICATION DATABASE SYSTEM * * * * *
--------------------MENU-----------------------
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Enter your choice : 4
* * * * TAPE PUBLICATION DATABASE SYSTEM * * * *
--------------------------------------------------
Title of Publication : Storytel
Publication Price : 250
Play Time : 280 min
--------------------------------------------------
* * * * * PUBLICATION DATABASE SYSTEM * * * * *
--------------------MENU-----------------------
1. Add Information to Books
2. Add Information to Tapes
3. Display Books Information
4. Display Tapes Information
5. Exit
Enter your choice : 5
--------------------------------
Process exited after 130.7 seconds with return value 0
Press any key to continue . . .
Conclusion:
Hence,
we have successfully understand the Concept of Inheritance and exception
handling.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home