1/21/2015 Object Oriented Design 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 members the four data Invoice( string, string, int, int ); // members set and get functions for the four data members void setPartNumber( string ); // part number setgetPartNumber(); and get functions for the four data string void setPartDescription( string ); // part description members string getPartDescription(); void setQuantity( int ); // quantity Set/get part number int getQuantity(); void setPricePerItem( int );description // price per item Set/get part int getPricePerItem(); Set/get quantity // calculates invoice amount by multiplying quantity x price per item price per item intSet/get 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; include definition of class Invoice from Invoice.h #include "Invoice.h" 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 // get part number string Invoice::getPartNumber() { return partNumber; } // end function getPartNumber 4 1/21/2015 Part Description // set part description void Invoice::setPartDescription( string description ) { partDescription = description; // no validation needed } // end function setPartDescription // get part description string Invoice::getPartDescription() { return partDescription; } // end function getPartDescription 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 if ( count <= 0 ) // if quantity is not positive { quantity = 0; // set quantity to 0 cout << "\nquantity cannot be negative. quantity set to 0.\n"; } // end if } // end function setQuantity // 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 if ( price <= 0 ) // if price is not positive { pricePerItem = 0; // set pricePerItem to 0 cout << "\npricePerItem cannot be negative. " << "pricePerItem set to 0.\n"; } // end if } // end function setPricePerItem // get price per item int Invoice::getPricePerItem() { return pricePerItem; } // end function getPricePerItem Calculate Invoice Amount // calculates invoice amount by multiplying quantity x price per item int Invoice::getInvoiceAmount() { return getQuantity() * getPricePerItem(); } // end function getInvoiceAmount 6 1/21/2015 // Exercise 3.13 Solution: ex03_13.cpp // Create and manipulate an Invoice object. #include <iostream> using std::cout; using std::cin; using std::endl; // include definition of class Invoice from Invoice.h #include "Invoice.h" // function main begins program execution int main() { // create an Invoice object Invoice invoice( "12345", "Hammer", 100, 5 ); // display the invoice data members and calculate the amount cout << "Part number: " << invoice.getPartNumber() << endl; cout << "Part description: " << invoice.getPartDescription() << endl; cout << "Quantity: " << invoice.getQuantity() << endl; cout << "Price per item: $" << invoice.getPricePerItem() << endl; cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl; // modify the invoice data members invoice.setPartNumber( "123456" ); invoice.setPartDescription( "Saw" ); invoice.setQuantity( -5 ); // negative quantity, so quantity set to 0 invoice.setPricePerItem( 10 ); cout << "\nInvoice data members modified.\n\n"; // display the modified invoice data members and calculate new amount cout << "Part number: " << invoice.getPartNumber() << endl; cout << "Part description: " << invoice.getPartDescription() << endl; cout << "Quantity: " << invoice.getQuantity() << endl; cout << "Price per item: $" << invoice.getPricePerItem() << endl; cout << "Invoice amount: $" << invoice.getInvoiceAmount() << endl; return 0; // indicate successful termination } // end main 7 1/21/2015 Demo: Compile and Run on Linux Demo: Compile and Run on Windows 8 1/21/2015 Submission Format • Home1 – -- Question1 -- Class1.h -- Class1.cc -- test1.cc -- Question2 -- Class2.h -- Class2.cc -- test2.cc Introduction • Before writing a program to solve a problem, we must have a thorough understanding of the problem and a carefully planned approach to solving it. • When writing a program, we must also understand the types of building blocks that are available and employ proven program construction techniques. • We will discuss the theory and principles of structured programming. 9 1/21/2015 Algorithms • Any solvable computing problem can be solved by the execution of a series of actions in a specific order. • An algorithm is procedure for solving a problem in terms of – the actions to execute and – the order in which the actions execute • Specifying the order in which statements (actions) execute in a computer program is called program control. 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 Control Structures (cont.) • An activity diagram models the workflow (also called the activity) of a portion of a software system. • Such workflows may include a portion of an algorithm, such as the sequence structure in Fig. 4.2. • Activity diagrams are composed of special-purpose symbols, such as action state symbols (a rectangle with its left and right sides replaced with arcs curving outward), diamonds and small circles; these symbols are connected by transition arrows, which represent the flow of the activity. • Activity diagrams help you develop and represent algorithms, but many programmers prefer pseudocode. • Activity diagrams clearly show how control structures operate. • Action states represent actions to perform. – Each contains an action expression that specifies a particular action to perform. 12 1/21/2015 Control Structures (cont.) • The arrows in the activity diagram are called transition arrows. – Represent transitions, which indicate the order in which the actions represented by the action states occur. • The solid circle at the top of the diagram represents the activity’s initial- state—the beginning of the workflow before the program performs the modeled activities. • The solid circle surrounded by a hollow circle that appears at the bottom of the activity diagram represents the final state—the end of the workflow after the program performs its activities. 13 1/21/2015 4.5 if Selection Statement (cont.) • A decision can be based on any expression—if the expression evaluates to zero, it’s treated as false; if the expression evaluates to nonzero, it’s treated as true. • C++ provides the data type bool for variables that can hold only the values true and false— each of these is a C++ keyword. if…else Double-Selection Statement (cont.) • Figure 4.5 illustrates the the if…else statement’s flow of control. 14 1/21/2015 15 1/21/2015 16 1/21/2015 17 1/21/2015 18 1/21/2015 Exercise -- Activity Diagram 19 1/21/2015 End 20