Schedule‎ > ‎

14X: Classes, Motors and Servos

Review of Objects and Classes

  • Previously we discussed how to code classes to create objects


  • Recall that an object in C++ is a location in memory containing a structured collection of variables and functions defined by its class
  • As an example, here is an "RED_LED" object in memory:
  • The example object has two pieces of data, a name and a price, structured one after the other in memory
  • In addition, the object has access to associated functions
  • To define the data structure of objects, we write a class


  • A class is a program-code "template" for creating objects
  • Objects are then a particular instance of a class, meaning an object has particular values
  • The particular values are stored in memory as defined by the class template
  • The data values are structured in the order defined by the class:
    class RGB_LED {
      _redPin = redPin;
      _greenPin = greenPin;
      _bluePin = bluePin;
      _commonAnode = commonAnode;
      pinMode(redPin, OUTPUT);
      pinMode(greenPin, OUTPUT);
      pinMode(bluePin, OUTPUT);

Information Hiding

  • Remember that we always code our class member variables as private
  • The keyword private restricts access to only member functions
  • Keeping member variables private is important so we can make design changes

Object Interface

  • To access private data, we code public member functions like:
        RGB_LED(int redPin, int greenPin, int bluePin, bool commonAnode);
        void setColor(int red, int green, int blue);
  • These public functions are the interface to our class
  • The interface is how we communicate with and use our objects

Constructing Objects

  • To create objects from the class, we construct an object like:
    RGB_LED rgb(11, 10, 9, true);
  • When the object is created, memory is allocated for the class variables
  • However, the memory is uninitialized
  • We can use the set functions to assign the memory values:
  • However, this is cumbersome and provides no guarantee that the programmer using our class will completely initialize the object data
  • A better solution is to code constructor functions

Constructor Functions

  • A constructor is a special type of function whose purpose is to initialize member variables
  • Whenever an object is created from a class, a constructor is always called automatically
  • A default constructor must set the member variables to default values:
    RGB_LED::RGB_LED() {
      _redPin = 11;
      _greenPin = 10;
      _bluePin = 9;
      _commonAnode = true;
  • Even though we should always code a default constructor, it is convenient to code other constructors like:
    RGB_LED::RGB_LED(int redPin, int greenPin, int bluePin, bool commonAnode) {
      _redPin = redPin;
      _greenPin = greenPin;
      _bluePin = bluePin;
      _commonAnode = commonAnode;
      pinMode(redPin, OUTPUT);
      pinMode(greenPin, OUTPUT);
      pinMode(bluePin, OUTPUT);
  • This lets us construct an object and initialize data members at the same time:
    RGB_LED rgb(11, 10, 9, true);
  • When we are done, we have a modular, reusable grouping of variables and functions
  • In this section we look at some ways we can make use of these modules

Try It: Design a Class

On paper, write the declaration for a class that represents a color . The class will need member variables for red, green and blue. We will want to be able to get (access) and set (change) the color values.

Phase One (5m)

  1. First, decide on an appropriate name for the class.
  2. Decide what member variables are needed and the appropriate data types.
  3. Decide what actions need to be taken for the color and declare appropriate member functions.
  4. Decide what constructors are needed to initialize the member variables.
  5. Decide what functions are needed to access or update the member variables.

Phase 2 (4m)

  1. Review your design with another member of the class.
  2. Update your design if you need to do so.
  3. Make sure your name is on your paper and then turn in your paper.
  4. Be prepared to share your design when called upon.

Check Yourself

  1. True or false: a class contains (encapsulates) both variables and functions.
  2. To allow only member functions and constructors of an object to access a member variable, use the keyword ________.
  3. True or false: good programming practice is to set the accessibility of all member variables to private.
  4. Public functions are the ________ of a class.
  5. True or false: the purpose of a constructor is to initialize all the member variables.

^ top