Created
March 10, 2013 00:32
-
-
Save arunenigma/5126511 to your computer and use it in GitHub Desktop.
Oops Simple Example: Cash Register
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <iomanip> | |
| using namespace std; | |
| /* | |
| A simulated cash register that tracks item count and total amount due | |
| */ | |
| class CashRegister { | |
| public: | |
| void clear(); | |
| void add_item(double price); | |
| double get_total() const; | |
| int get_count() const; | |
| private: | |
| int item_count; | |
| double total_price; | |
| }; | |
| void CashRegister::clear() { | |
| item_count = 0; | |
| total_price = 0; | |
| } | |
| void CashRegister::add_item(double price) { | |
| item_count ++; | |
| total_price = total_price + price; | |
| } | |
| double CashRegister::get_total() const { | |
| return total_price; | |
| } | |
| int CashRegister::get_count() const { | |
| return item_count; | |
| } | |
| void display(CashRegister reg) { // not a member function | |
| cout << reg.get_count() << " $" << fixed << setprecision(2) << reg.get_total() << endl; | |
| } | |
| int main() { | |
| CashRegister c; | |
| c.clear(); | |
| c.add_item(124.41); | |
| display(c); | |
| c.add_item(1234.23); | |
| display(c); | |
| c.add_item(232.23); | |
| display(c); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment