Académique Documents
Professionnel Documents
Culture Documents
Lecture 5: Program
Example
3.13 (Invoice Class) Create a class called Invoice that a hardware store might
use to represent an invoice for an item sold at the store. An Invoice should
include four pieces of information as data members—a part number (type
string), a part description (type string), a quantity of the item being
purchased (type int) and a price per item (type int). [Note: In subsequent
chapters, we’ll use numbers that contain decimal points (e.g., 2.75)—
called floating-point values—to represent dollar amounts.] Your class
should have a constructor that initializes the four data members. Provide a
set and a get function for each data member. In addition, provide a
member function named get-InvoiceAmount that calculates the invoice
amount (i.e., multiplies the quantity by the price per item), then returns
the amount as an int value. If the quantity is not positive, it should be set
to 0. If the price per item is not positive, it should be set to 0. Write a test
program that demonstrates class Invoice’s capabilities.
1
1/21/2015
Class Invoice
• Data members
The number of the part being sold
Description of the part being sold
How many of the items are being sold
Price per item
private:
string partNumber; // the number of the part being sold
string partDescription; // description of the part being sold
int quantity; // how many of the items are being sold
int pricePerItem; // price per item
Class Invoice
Function members
• Function members
public:
// constructor
constructor initializes initializes the four data
the four data members
Invoice( string, string, int, int );
// members
set and get functions for the four data members
void setPartNumber( string ); // part number
setgetPartNumber();
string and get functions for the four data
void setPartDescription( string ); // part description
members
string getPartDescription();
Set/get part number
void setQuantity( int ); // quantity
int getQuantity();
Set/get
void part
setPricePerItem(
int getPricePerItem();
int );description
// price per item
Set/get quantity
// calculates invoice amount by multiplying quantity x price per item
intSet/get price per item
getInvoiceAmount();
2
1/21/2015
// Invoice.h
// Definition of Invoice class that represents an invoice for an item sold at a hardware store.
#include <string> // program uses C++ standard string class
using std::string;
class Invoice // Invoice class definition
{
public:
// constructor initializes the four data members
Invoice( string, string, int, int );
void setPartNumber( string ); // part number
string getPartNumber();
void setPartDescription( string ); // part description
string getPartDescription();
void setQuantity( int ); // quantity
int getQuantity();
void setPricePerItem( int ); // price per item
int getPricePerItem();
int getInvoiceAmount(); // calculates invoice amount
private:
string partNumber; // the number of the part being sold
string partDescription; // description of the part being sold
int quantity; // how many of the items are being sold
int pricePerItem; // price per item
}; // end class Invoice
Source Code
//Invoice.cpp
// Member-function definitions for class Invoice.
#include <iostream>
using std::cout;
using std::endl;
3
1/21/2015
Constructor
// Invoice constructor initializes the class's four data members
Invoice::Invoice( string number, string description, int count,
int price )
{
setPartNumber( number ); // store partNumber
setPartDescription( description ); // store partDescription
setQuantity( count ); // validate and store quantity
setPricePerItem( price ); // validate and store pricePerItem
} // end Invoice constructor
Part Number
// set part number
void Invoice::setPartNumber( string number )
{
partNumber = number; // no validation needed
} // end function setPartNumber
4
1/21/2015
Part Description
// set part description
void Invoice::setPartDescription( string description )
{
partDescription = description; // no validation needed
} // end function setPartDescription
Quantity
// set quantity; if not positive, set to 0
void Invoice::setQuantity( int count )
{
if ( count > 0 ) // if quantity is positive
quantity = count; // set quantity to count
// get quantity
int Invoice::getQuantity()
{
return quantity;
} // end function getQuantity
5
1/21/2015
Price
// set price per item; if not positive, set to 0.0
void Invoice::setPricePerItem( int price )
{
if ( price > 0 ) // if price is positive
pricePerItem = price; // set pricePerItem to price
6
1/21/2015
7
1/21/2015
8
1/21/2015
Submission Format
• Home1 –
-- Question1
-- Class1.h
-- Class1.cc
-- test1.cc
-- Question2
-- Class2.h
-- Class2.cc
-- test2.cc
Introduction
9
1/21/2015
Algorithms
Pseudocode
• Pseudocode (or “fake” code) is an artificial and informal language
that helps you develop algorithms.
• Similar to everyday English
• Convenient and user friendly.
• Helps you “think out” a program before attempting to write it.
• Carefully prepared pseudocode can easily be converted to a
corresponding C++ program.
• Normally describes only executable statements.
• Declarations (that do not have initializers or do not involve
constructor calls) are not executable statements.
• Fig. 4.1 corresponds to the algorithm that inputs two integers from
the user, adds these integers and displays their sum.
10
1/21/2015
Control Structures
• Normally, statements in a program execute one after the other in
the order in which they’re written.
– Called sequential execution.
• Various C++ statements enable you to specify that the next
statement to execute may be other than the next one in sequence.
– Called transfer of control.
• All programs could be written in terms of only three control
structures
– the sequence structure
– the selection structure and
– the repetition structure
• When we introduce C++’s implementations of control structures,
we’ll refer to them in the terminology of the C++ standard
document as “control statements.”
11
1/21/2015
12
1/21/2015
13
1/21/2015
14
1/21/2015
15
1/21/2015
16
1/21/2015
17
1/21/2015
18
1/21/2015
19
1/21/2015
End
20