Home‎ > ‎CS 11M‎ > ‎

Introduction to Classes

Introduction 

There are two kinds of data types in C++: fundamental types which are built into the language and user-defined types. 
  • Built in types:
    • int, unsigned int (including longs)
    • double and float 
    • char 
  • User-defined types:
    • Serial (is of type HardwareSerial)
    • cout (is of type std::ostream)
    • vector (a.k.a. std::vector)
    • string (a.k.a. std::string)
In this lecture you will learn how to define your own type and use it. 

Defining a Data Type 

Data types in C++ are a mixture of other data types and functions. These are called members
  • Data types are called classes
  • Classes contain data and functions called members
  • Members can be public or private

Your First Data Type

The program below defines a data type named "Mike". 

#include <ArduinoSTL.h>

using namespace std;

class Mike {

};

void setup() {
  Serial.begin(9600);
}

void loop() {
  Mike mikem; 
}

Notice:
  • The program declares a variable named mikem of type Mike
The datatype Mike doen't have anything but a name. The next task to give it more functionality. 

A Class for a Color

Now let's define a data type that holds a color value in terms of Red, Green and Blue intensity. 

#include <ArduinoSTL.h>

using namespace std;

class Color {

public: 

  int red; 
  int green; 
  int blue;

};

void setup() {
  Serial.begin(9600);
}

void loop() {
  Color c; 
  c.red = 128;
  c.green = 64; 
  c.blue = 0;   
}

Notice a few things about color: 
  • The public: keyword. This makes the integers red, green and blue accessible outside the class. 
  • We create a variable named c of type Color.
  • We assign the integers inside of c using a dot (.). 

Instances and Classes 

Classes are like blueprints for making instances of objects
  • What's the class and what's the instance?
  • What's the class and what's the instance?

    Color c; 
When you have a class you have a template for making objects. You can make as many as you like: 

Color okay;
Color warning; 
Color emergency; 
Color sleeping; 

Each of the colors can be different. Each one contains three integers named red, blue and green. Below is an example program that shows how you can have multiple colors and pass a color to a function: 

#include <ArduinoSTL.h>

using namespace std;

const int redLED = 11; 
const int greenLED = 10; 
const int blueLED = 9; 

class Color {

public: 

  int red; 
  int green; 
  int blue;

};

void setup() {
  Serial.begin(9600);
}

void setColor(Color c) {
  analogWrite(redLED, 255 - c.red);
  analogWrite(greenLED, 255 - c.green); 
  analogWrite(blueLED, 255 - c.blue);  
}

void loop() {

  Color go; 
  go.red = 0;
  go.green = 255; 
  go.blue = 0; 

  Color stop; 
  stop.red = 255; 
  stop.green = 0; 
  stop.blue = 0; 
  
  Color slow; 
  slow.red = 128; 
  slow.green = 128; 
  slow.blue = 0; 

  setColor(stop); 
  delay(3000);
  setColor(go);
  delay(3000);
  setColor(slow);
  delay(2000);
}

Member Functions

So far our class only has member variables. Member functions allow us to make special actions that act on an instance of the class. You've already used many member functions:
  1. Serial.begin() (a.k.a. HardwareSerial::begin())
  2. Vector::push_back() (e.g. myVect.push_back(4))
  3. Vector::size() (e.g. myVect.size())
  4. String::length() (e.g. myStr.length())
  5. String::substr() (e.g. myStr.substr(0,1))
Let's add to the class above and give it a member function that displays the color on the LEDs:

class Color {

public: 

  int red; 
  int green; 
  int blue;

  void display() {
    analogWrite(redLED, 255 - red);
    analogWrite(greenLED, 255 - green); 
    analogWrite(blueLED, 255 - blue);  
  }
};

Notice:
  • The function is defined inside of the class. 
  • The function has access to the red, green and blue variables without using a dot. 
We can use the Class' member function like this: 

  //setColor(stop); 
  stop.display();
  delay(3000);
  
  //setColor(go);
  go.display();
  delay(3000);
  
  //setColor(slow);
  slow.display();
  delay(2000);

The use of setColor() is commented out. Below it is the equivalent use of display()

Mutators

Member functions can also be used to analyze or change the value of the class variables. Here are a couple of examples: 

  /* getIntensity()
   *  Get the total luminous intensity of the color. 
   *  @returns the sum of the color intensities. 
   */
  int getIntensity() {
    return red + green + blue; 
  }

  /* redShift()
   *  Make the color redder by decreasing the other color components by half. 
   */
  void redShift() {
    green = green / 2; 
    blue = blue / 2; 
  }

The member functions are able to operate on the colors. The redShift() function modifies them as it operates. 

Exercise: Build Your first Class 
In this exercise you will create your own color class and add the display function. 

Specifications

  • Start with the Color class above. 
  • Add the display function as sown above. 
  • In the loop() function of your program 
    • Create at least two colors
    • Show those colors on the LED
  • Save your exercise as first_color.ino
When you're done help those around you. You will submit this class with the next assignment. 

Special Member Functions

There are two special member functions: the constructor and the destructor. As you might have guessed these member functions are called when an instance of the class is created and when an instance of the class is destroyed. The constructor and destructor are identified because they are functions with the same name as the class. Also, they are the only functions in C/C++ that have no return type.

Here's the color class with a constructor and a destructor: 

class Color {

public: 

  Color() {
    cout << "Constructing a color." << endl;
  }

  ~Color() {
    cout << "Destroying a color." << endl;
  }

  int red; 
  int green; 
  int blue;

  void display() {
    analogWrite(redLED, 255 - red);
    analogWrite(greenLED, 255 - green); 
    analogWrite(blueLED, 255 - blue);  
  }
};

Remember:
  • The constructor is a function that has the same name as the class
  • The destructor has the same name as the class with a tilde (~) at the beginning. 
  • The constructor and destructor have no return type.  

Constructor Arguments

Constructors are especially useful because they can take arguments (destructors can never have arguments). The arguments to a constructor can help initialize the data in the class. Let's add a second constructor to our color class:

class Color {

public: 

  Color() {
    cout << "Constructing a color." << endl;
  }

  Color(int r, int g, int b) {
    cout << "Constructing the color r:" << r << " g:" << g << " b:" << b << endl;
    red = r; 
    green = g; 
    blue = b; 
  }
  ... 

Now we will be able to initialize a color as we create it. In our loop we can replace the lines that declared and initialize each color with these: 

  Color go(0, 255, 0);
  Color stop(255, 0, 0);
  Color slow(128, 128, 0);

Exercise: Improved Color 

In this exercise you will update your color class from the previous exercise. 

Specifications

  • Add a constructor that allows you to initialize the red, green and blue variables as shown above. 
  • Add a function called saturate() that does the following:
    • Finds the value of the faintest color (the minimum color value)
    • Subtracts the minimum value from all three colors.
  • The saturate function will have the effect of "purifying" the color. 
  • Test your saturate function on the following colors:
    • Color(64, 128, 256); 
    • Color(32, 125, 200);
  • Write a print member function that prints the values of each of the colors. 
    • Call your print function from inside of the display() function
  • Save your exercise as color_improved.ino
When you're done help those around you. You will submit this class with the next assignment. 

Wrap Up and Reminders

  • When class is over, please shut down your computer.
  • Complete unfinished exercises from today before the next class meeting
Comments