Part C - Encapsulation
Input and Output
Workshop 7
In this workshop, you code a class that provides input and output
functionality through customized extraction and insertion operators.
Learning Outcomes
Upon successful completion of this workshop, you will have
demonstrated the abilities to
- overload the extraction and insertion operators for objects of your own class
- implement cascading in your customization
Price List - All Groups
Item Class
Design and code a class named Item that holds the price of a stock item.
Place your class definition in a header file named
Item.h and your function
definitions in an implementation file named Item.cpp
Upon instantiation, an Item
object may receive no information or two pieces of
information
- the item number
- the item's price
A valid number is positive-valued. If upon instantiation
the object receives an invalid number or a negative
price, the object takes on a safe empty state.
Your class definition includes the following public member functions
and helper operators:
- void operator=(double p) -
resets the item's price to p
- bool empty() const -
returns false if the current object
contains valid data; true if it is in
a safe empty state
- void display(std::ostream& os) const
- inserts into os the item number left-justified in a field
width of 8 followed by the item's price right-justified in a
field of 10 to 2 decimal places
- std::istream& operator>>(std::istream& is, Item& i)
- extracts data from is and stores it in i
if the data is valid
- std::ostream& operator<<(std::ostream& os, const Item& i)
- inserts into os the data in i in the
format described above
Place your class definition in a header file named
Item.h and your function
definitions in an implementation file named Item.cpp.
Client Module
The client program that uses your Item
class accepts input for up to ten (10) items and displays the
information in tabular form as shown on the right.
// Workshop 7 - Price List
// w7.cpp
#include <iostream>
#include "Item.h"
const int MAX_NO = 10;
int main() {
int n = 0;
bool quit = false;
Item item[MAX_NO];
std::cout << "Price List\n";
std::cout << "==========\n";
do {
std::cin >> item[n];
if (item[n].empty())
quit = true;
else
n++;
} while (!quit && n < MAX_NO);
std::cout << std::endl;
if (!item[0].empty()) item[0] = 45.33;
std::cout << " Number Price\n";
std::cout << "------------------\n";
for (int i = 0; i < n; i++)
std::cout << item[i] << std::endl;
}
|
Price List
==========
Item Number : 234
Price : 45.32
Item Number : 235
Price : 67.42
Item Number : 236
Price : 89.76
Item Number : 0
Price : 0
Number Price
-------------------
234 45.33
235 67.42
236 89.76
|
Upgrade - Group Y
Design and code the Item class described
above. Upgrade your class to include the address of a C-style
null-terminated string that holds an item's description.
Your design does not place any limitiation on the number of characters
in the string.
Since your Item class can process
a description without limitation on length, your design should
use dynamic memory allocation and include
- a copy constructor
- an assignment operator
- a destructor
Client Module
The implementation file for the main module that uses your
Item class is listed on the left, while
the results are shown on the right:
// Workshop 7 - Price List
// w7.cpp
#include <iostream>
#include "Item.h"
const int MAX_NO = 10;
int main() {
int n = 0;
bool quit = false;
Item item[MAX_NO];
std::cout << "Price List\n";
std::cout << "==========\n";
do {
std::cin >> item[n];
if (item[n].empty())
quit = true;
else
n++;
} while (!quit && n < MAX_NO);
std::cout << std::endl;
if (!item[0].empty()) item[0] = 45.33;
std::cout
<< " Number Price Description\n";
std::cout
<< "------------------------------\n";
for (int i = 0; i < n; i++)
std::cout << item[i] << std::endl;
}
|
Price List
==========
Item Number : 234
Price : 45.32
Description : Shirt
Item Number : 235
Price : 67.42
Description : Drill
Item Number : 236
Price : 89.76
Description : Frying Pan
Item Number : 0
Price : 0
Description :
Number Price Description
------------------------------
234 45.33 Shirt
235 67.42 Drill
236 89.76 Frying Pan
|
SUBMISSION
Typescript
Create a typescript of your complete solution
using the following commands:
+ At the prompt, type: script w7.txt
+ At the prompt, type: whoami
+ At the prompt, type: cat Item.h Item.cpp
+ At the prompt, type: g++ -o w7 w7.cpp Item.cpp
+ At the prompt, type: w7
+ At the input prompt, enter test data
+ At the prompt type: exit
|
These commands will produce a file named w7.txt. Download this file to your local
computer.
Moodle
Upload your typescript file to Moodle:
- Login to Moodle
- Select your course code
- Select W7 under Assignments and Tasks
- Upload w7.txt
- Write a short note to your instructor
- press "Edit"
- enter your conclusions about this workshop in the notes textbox
- press "Save Changes"
- When ready to submit, press "Send for Marking"
Other Submissions
Submit your typescript file as your instructor has specified.
|