JMU
Classes and Objects in C++: An Introduction
for Java Programmers


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Basics
Constructors
Constructors (cont.)
The explicit Modifier
An Example

An Encapsulation of a Shopper in a "Shopping Club"

cppexamples/oopbasics/shopping/Shopper.h
        #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
        
An Example (cont.)

The Implementation of the Shopper

cppexamples/oopbasics/shopping/Shopper.cpp
        #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;
}

        
An Example (cont.)

Using a Shopper

cppexamples/oopbasics/shopping/Driver1.cpp
        #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;
}
        
Constructors Revisited
Friends
Friends (cont.)

An Example

    class Car {   
      private:

        int     year;
	char    *make, *model; 

      public:

        .
        . Some member functions
        .

	friend class Garage;
    };
    
Friends (cont.)

An Example (cont.)

    class Garage {

       .
       . Some member variables and functions
       .

    };
  
Static Members
Static Attributes(cont.)

The Motivating Example - One Approach that Won't Work

    class Order {
      public:
        int    accountNumber, orderNumber, productNumber;
    };
    
Static Attributes(cont.)

The Motivating Example - One Approach that Won't Work (cont.)

    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
    }
    
Static Attributes (cont.)

The Motivating Example - Another Approach that Won't Work

    class Order {
      public:

        int    accountNumber, orderNumber, productNumber;
	int    lastOrderNumber;
    };
    
Static Attributes (cont.)

The Motivating Example - Another Approach that Won't Work (cont.)

    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!
    }
    
Static Attributes (cont.)

The Motivating Example - Using a Static Attribute

    class Order {
      public:
        int          accountNumber, orderNumber, productNumber;
	static int   lastOrderNumber;
    };
    
Static Attributes (cont.)

The Motivating Example - Using a Static Attribute (cont.)

    int main(void) {
        Order   jmuOrder;

	jmuOrder.accountNumber = 1001;
	jmuOrder.productNumber =    5;

	// Use the next available order number

        ++Order::lastOrderNumber;
	jmuOrder.orderNumber = Order::lastOrderNumber;
    }
    
Static Attributes (cont.)

One Detail - Initialization of Static Attributes

Static Methods
Static Methods (cont.)

Continuing the Above Eample

    class Order {
      public:
       int    accountNumber, orderNumber, productNumber;
       static int getOrderNumber(void);

      private:
	  static int    lastOrderNumber;
    };
    
Common Uses of Static Members
The Specialization/Generalization Relationship
Specialization/Generalization (cont.)
Specialization in C++
The Inheritance Modifier in C++
Constructors and Inheritance in C++
An Example

An Encapsulation of an ExecutiveShopper in a "Shopping Club"

cppexamples/oopbasics/shopping/ExecutiveShopper.h
        #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
        
An Example (cont.)

Implementation of ExecutiveShopper

cppexamples/oopbasics/shopping/ExecutiveShopper.cpp
        #include "ExecutiveShopper.h"

ExecutiveShopper::ExecutiveShopper(int accountNumber, double cashbackRate)
    : Shopper(accountNumber) {
  setFee(100.00);
  rate = cashbackRate;
}

double ExecutiveShopper::getCashback(void) {
  return rate * getPurchases();
}