40 likes | 274 Views
Expense.cpp. /////////////////////////////////////////////////////////////////////////////////////////////////// // Expense.Cpp // Object-oriented expense ledger #include <iostream.h> // For cout, cin, etc.
E N D
Expense.cpp /////////////////////////////////////////////////////////////////////////////////////////////////// // Expense.Cpp // Object-oriented expense ledger #include <iostream.h> // For cout, cin, etc. /////////////////////////////////////////////////////////////////////////////////////////////////// class ledger // Declaration of class ledger { private: float list[100]; // Array for items int count; // Number of items entered public: void initcount(void); // Declare function void enteritem(void); // Declare function void printtotal(void); // Declare function }; /////////////////////////////////////////////////////////////////////////////////////////////////// void ledger::initcount(void) // A function in class ledger { // Initializes count to 0 count = 0; } /////////////////////////////////////////////////////////////////////////////////////////////////// void ledger::enteritem(void) // A function in class ledger { // Puts entry in list cout << “\nenter amount: ”; cin >> list[count++]; } /////////////////////////////////////////////////////////////////////////////////////////////////// void ledger::printtotal(void) // A function in class ledger { // Prints total of all entries float total = 0.0; for(int j = 0; j<count; j++) total + = list[j]; cout << “\n\ntotal is: ” << total << “\n”; } Object Oriented Programming Using C++ Class 5 - Overhead 1
Main Driver int main ( ) // main --handles user interface { // uses object “expenses” ledger expenses; // an instance of class ledger char option; expenses.initCount( ); // initialize count do { // display options cout << “\nOptions are: e -- enter expense” << “\n E -- print total expenses” << “\n q -- quit (terminate program)” << “\nType option letter: ”; cin >> option; // get option from user switch (option) { // act on option selected case ‘e’: expenses.enterItem( ); break; case ‘E’: expenses.printTotal( ); break; case ‘q’: break; default: cout << “\nUnknown command\n”; } } while(option ! = ‘q’); } // end main Object Oriented Programming Using C++ Class 5 - Overhead 2
Create a Time Class Part 1 /#include <iostream.h> // for cout, etc. //////////////////////////////////////////////////////////////////////////////////////////////////////// class time { private: int hrs; // hours int mins; // minutes int secs; // seconds public: time( ) // constructor { secs = 0; mins = 0; hrs = 0; } // initialize to 0:00:00 void printTime(void) // print time { cout << hrs << ‘ : ‘ << mins } << ‘ : ‘ << secs; void readTime(void) //read time from keyboard { cin >> hrs >> mins >> secs; } void addTime(time t) { secs += t.secs; if(secs > 59) {secs -= 60; ++mins; } mins += t.mins; if(mins > 59) { mins -= 60; ++hrs; } hrs += t.hrs; } }; Object Oriented Programming Using C++ Class5 - Overhead 3
Create a Time Class Part 2 //////////////////////////////////////////////////////////////////////////// main( ) { time t1, t2, tAnswer; cout << “\nEnter first time period”; t1.readTime( ); cout << “\nEnter second time period”; cout << “\nt1 =”; t1.printTime( ); cout << “\nt2 = ”; t2.printTime( ); tAnswer.addTime(t1); tAnswer.addTime(t2); cout << “\nSum of time periods = ”; tAnswer.printTime( ); } Object Oriented Programming Using C Class 5 - Overhead 4