Starting from:

$15

Laboratory Exercise Week 8 solution

Task One: Debugging
Virtual.cpp and Throw.cpp contain various errors; syntax, logical and/or with respect to the sample output.
Fix the bugs so the programs can be compiled and run correctly producing the following output
**************************
A x: 4
A x: 6
B y: 5
**************************
SixSevenThreeFour Five
**************************
Task Two: A leaky program
The program Leaky.cpp has memory leaks. Find them and fix them. Remember you can use bcheck to check for memory leaks.
Task Three: Exceptions
Write code in a file Book.cpp that contains a Book class with fields for a book identification number, title, authors, number of pages and price. The book title and author should be stored as character pointers, and the constructor should allocate 80 characters of new memory for each. You should include appropriate insertion and extraction operators for the Book. Remember to define destructor for the class Book.
Create a BookException class inherits runtime_error in the file Book.cpp that holds a Book and an error message. When a Book is being created, two exceptions are to be looked for based on the following rules:
1. The number of pages should be divisible by four.
2. The price should be between $5.00 and $250.00 inclusive.
If one of these conditions is not met, you should create and throw an appropriate
BookException object. Override what() function in the class BookException to return exception message.
Write a main() function in the file Book.cpp which creates an array of two Book objects. If a BookException object is thrown during the data entry of a Book, the user should be required to enter data for a new Book to replace the invalid Book. Thus you keep on entering Book details until you have two valid books. Then display those two books.

Testing task Three:
Compile your program by CC –o task3 Book.cpp
Your program outputs may look like following (Red data means inputs from keyboard). Id number: 1
Title: How to program C++
Authors: Paul Deitel, Harvey Deitel
Number of pages: 1027
The no. 1 book error.
Id number: 1
Title: How to program C++
Authors: Paul Deitel, Harvey Deitel
Number of pages: 1027
Price: 0.00
Incorrect number of pages.
Id number: 1
Title: How to program C++
Authors: Paul Deitel, Harvey Deitel
Number of pages: 1028
Price: 1128.3
The no. 1 book error.
Id number: 1
Title: How to program C++
Authors: Paul Deitel, Harvey Deitel
Number of pages: 1028
Price: 1128.30
Incorrect price.
Id number: 1
Title: How to program C++
Authors: Paul Deitel, Harvey Deitel
Number of pages: 1028
Price: 112.83
Id number: 2
Title: A structure programming approach using C++
Authors: Behrouz A. Forouzan, Richard F. Gilberg
Number of pages: 882
The no. 2 book error.
Id number: 2
Title: A structure programming approach using C++
Authors: Behrouz A. Forouzan, Richard F. Gilberg
Number of pages: 882
Price: 0.00
Incorrect number of pages.
Id number: 2
Title: A structure programming approach using C++
Authors: Behrouz A. Forouzan, Richard F. Gilberg
Number of pages: 884
Price: 1.235
The no. 2 book error.
Id number: 2
Title: A structure programming approach using C++
Authors: Behrouz A. Forouzan, Richard F. Gilberg
Number of pages: 884
Price: 1.24
Incorrect price.
Id number: 2
Title: A structure programming approach using C++
Authors: Behrouz A. Forouzan, Richard F. Gilberg
Number of pages: 884
Price: 123.5
Two books are:
Id number: 1
Title: How to program C++
Authors: Paul Deitel, Harvey Deitel
Number of pages: 1028
Price: 112.83
Id number: 2
Title: A structure programming approach using C++
Authors: Behrouz A. Forouzan, Richard F. Gilberg
Number of pages: 884
Price: 123.50
You can use the input data file input_books.txt to test your program:
task3 < input_books.txt

More products