Classes and Objects in C++: An Introduction
for Java Programmers |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
explicit
Shopper
in a "Shopping Club"#ifndef edu_jmu_cs_Shopper_h #define edu_jmu_cs_Shopper_h /** * A shopper in a shopping club. * * @author Prof. David Bernstein, James Madison University */ class Shopper { private: double fee, purchases; int account; protected: /** * Set the annual fee for this member. * * @param annualFee The annual fee */ void setFee(double annualFee); public: /** * Explicit Value Constructor. * * @param accountNumber The account number */ explicit Shopper(int accountNumber); /** * Increase the total value of purchases made by this Shopper. * * @param amount The amount of the new purchase */ void addToPurchases(double amount); /** * Get the total value of purchases made by this Shopper. * * @return The total value of purchases made to date */ double getPurchases(void); }; #endif
Shopper
#include "Shopper.h" Shopper::Shopper(int accountNumber) { account = accountNumber; setFee(45.00); purchases = 0.00; } void Shopper::addToPurchases(double amount) { purchases += amount; } double Shopper::getPurchases(void) { return purchases; } void Shopper::setFee(double annualFee) { fee = annualFee; }
Shopper
#include <iostream> #include <string> #include "Shopper.h" using namespace std; /** * An example that illustrates the use of the Shopper and * ExecutiveShopper classes. * * @author Prof. David Bernstein, James Madison University */ int main(void) { Shopper alice = Shopper(1001); cout << "Alice's purchases (before): " << alice.getPurchases() << "\n"; alice.addToPurchases(154.60); cout << "Alice's purchases (after): " << alice.getPurchases() << "\n"; return 1; }
Shopper(int accountNumber): purchases(0.00)
Car
class with some private
attributes and we have a Garage
class that has an array of Car
objects
class Car { private: int year; char *make, *model; public: . . Some member functions . friend class Garage; };
class Garage { . . Some member variables and functions . };
class Order { public: int accountNumber, orderNumber, productNumber; };
void main(void) { Order jmuOrder; jmuOrder.accountNumber = 1001; jmuOrder.productNumber = 5; // Use the next available order number ++jmuOrder.orderNumber; // This doesn't do what we want }
class Order { public: int accountNumber, orderNumber, productNumber; int lastOrderNumber; };
int main(void) { Order jmuOrder; jmuOrder.accountNumber = 1001; jmuOrder.productNumber = 5; // Use the next available order number ++jmuOrder.lastOrderNumber; // This still jmuOrder.orderNumber = lastOrderNumber; // won't work! }
class Order { public: int accountNumber, orderNumber, productNumber; static int lastOrderNumber; };
int main(void) { Order jmuOrder; jmuOrder.accountNumber = 1001; jmuOrder.productNumber = 5; // Use the next available order number ++Order::lastOrderNumber; jmuOrder.orderNumber = Order::lastOrderNumber; }
int Order::lastOrderNumber = 0;
static const int LEFT = 0;
static const int RIGHT = 0;
class Order { public: int accountNumber, orderNumber, productNumber; static int getOrderNumber(void); private: static int lastOrderNumber; };
private
attributesprivate
methods
ExecutiveShopper
in a "Shopping Club"#ifndef edu_jmu_cs_ExecutiveShopprt_h #define edu_jmu_cs_ExecutiveShopprt_h #include "Shopper.h" /** * An executive shopper member in a shopping club * (i.e., a member that receives cash back on purchases). * * @author Prof. David Bernstein, James Madison University */ class ExecutiveShopper : public Shopper { private: double rate; public: /** * Explicit Value Constructor. * * @param accountNumber The account number * @param cashbackRate The cash back rate */ ExecutiveShopper(int accountNumber, double cashbackRate); /** * Get the current cash back payment for * this ExecutiveShopper * * @return The cash back payment */ double getCashback(void); }; #endif
ExecutiveShopper
#include "ExecutiveShopper.h" ExecutiveShopper::ExecutiveShopper(int accountNumber, double cashbackRate) : Shopper(accountNumber) { setFee(100.00); rate = cashbackRate; } double ExecutiveShopper::getCashback(void) { return rate * getPurchases(); }