- Forward


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


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Basics
Back SMYC Forward
  • Organization:
    • A class consists of distinct declarations and definitions/implementations
    • Normally, they are even kept in separate files
    • (This is very different from Java)
  • Visibility/Accessibility:
    • Variables and methods can be either public, private, or protected
    • (The syntax is very different from Java)
Constructors
Back SMYC Forward
  • Purpose:
    • Initialize the attributes of an object
  • Declaration:
    • No return type
    • Same name as the class
Constructors (cont.)
Back SMYC Forward
  • The Default Constructor:
    • If no constructor is specified, a public constructor (with no parameters) is provided
  • The Copy Constructor:
    • A public constructor that is passed an object (really a reference) of the type of the class and constructs a copy
    • A default implementation is provided by the compiler (but it will only work properly for simple classes)
    • (More on this after we discuss references)
The explicit Modifier
Back SMYC Forward
  • An Observation:
    • The compiler is allowed to make one implicit conversion when matching actual and formal parameters
  • A Consequence:
    • This can lead to unintended consequences with constructors, especially those that have only one parameter
  • A Common Practice:
    • Declare one-parameter constructors to be explicit
An Example
Back SMYC Forward

An Encapsulation of a Shopper in a "Shopping Club"

cppexamples/oopbasics/shopping/Shopper.h
 
An Example (cont.)
Back SMYC Forward

The Implementation of the Shopper

cppexamples/oopbasics/shopping/Shopper.cpp
 
An Example (cont.)
Back SMYC Forward

Using a Shopper

cppexamples/oopbasics/shopping/Driver1.cpp
 
Constructors Revisited
Back SMYC Forward
  • Member Initializers:
    • Shopper(int accountNumber): purchases(0.00)
  • One constructor can't call another constructor (so move common construction tasks to another method)
  • A class's constructor can not have arguments of the class's own type but you can pass a reference (more on this after we discuss references)
Friends
Back SMYC Forward
  • The Idea:
    • A mechanism for making the private attributes and methods of a class available to some, but not all, classes or functions
  • A Motivating Example:
    • We have a Car class with some private attributes and we have a Garage class that has an array of Car objects
Friends (cont.)
Back SMYC Forward

An Example

class Car { private: int year; char *make, *model; public: . . Some member functions . friend class Garage; };
Friends (cont.)
Back SMYC Forward

An Example (cont.)

class Garage { . . Some member variables and functions . };
Static Members
Back SMYC Forward
  • Recall:
    • In general, a member is associated with an instance of an object rather than with a class
    • Sometimes, however, we want to be able to have variables or methods associated with the class itself
  • A Motivating Example:
    • You are building an order entry system that automatically assigns order numbers
Static Attributes(cont.)
Back SMYC Forward

The Motivating Example - One Approach that Won't Work

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

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.)
Back SMYC Forward

The Motivating Example - Another Approach that Won't Work

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

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.)
Back SMYC Forward

The Motivating Example - Using a Static Attribute

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

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.)
Back SMYC Forward

One Detail - Initialization of Static Attributes

  • Non-constant static attributes must be initialized in the implementation file:
    • int Order::lastOrderNumber = 0;
  • Constant static attributes can be initialized in the header file:
    • static const int LEFT = 0;
    • static const int RIGHT = 0;
Static Methods
Back SMYC Forward
  • Recall:
    • Sometimes you want a method to be associated with a class rather than instances of the class
  • An Important Note:
    • Static member functions can only use static member variables because a static member function can be used even when no instances of its class exist
Static Methods (cont.)
Back SMYC Forward

Continuing the Above Eample

class Order { public: int accountNumber, orderNumber, productNumber; static int getOrderNumber(void); private: static int lastOrderNumber; };
Common Uses of Static Members
Back SMYC Forward
  • Keeping track of the number of objects of a particular class that have been instantiated
  • Class-specific constants
  • A library of methods (e.g., mathematical functions). (Though one can use C-style functions which often results in code that is more readable.)
The Specialization/Generalization Relationship
Back SMYC Forward
  • The "is-a" relationship:
    • An object of class A "is-an" object of class B if A is/does everything that B is/does and more
  • Other Terminology:
    • Superclass -- Subclass
    • Parent Class -- Child Class
    • Base Class -- Derived Class
Specialization/Generalization (cont.)
Back SMYC Forward
  • Inheritance:
    • The child class inherits the attributes of the parent
    • The child class inherits the methods of the parent
  • Visibility/Accessibilty:
    • The child class cannot access private attributes
    • The child class cannot use private methods
Specialization in C++
Back SMYC Forward
  • Specialization Syntax:
    • class child: modifier parent [, modifier parent ]...
  • Scope Operator Syntax:
    • classname::member
The Inheritance Modifier in C++
Back SMYC Forward
  • public:
    • private members remain private
    • protected members remain protected
    • public members remain public
  • protected:
    • private members remain private
    • protected members remain protected
    • public members become protected
  • private:
    • private members remain private
    • protected members become private
    • public members become private
Constructors and Inheritance in C++
Back SMYC Forward
  • Construction Order:
    • The constructor in the parent class is called (automatically) before the constructor in the child
  • Calling a Parent's Constructor:
    • Can only be done using a member initializer
An Example
Back SMYC Forward

An Encapsulation of an ExecutiveShopper in a "Shopping Club"

cppexamples/oopbasics/shopping/ExecutiveShopper.h
 
An Example (cont.)
Back SMYC Forward

Implementation of ExecutiveShopper

cppexamples/oopbasics/shopping/ExecutiveShopper.cpp
 
There's Always More to Learn
Back -