Schedule‎ > ‎

## Nested ifs (05A)

`void loop() {`
`  cout << "Enter a score: ";`
`  double score;`
`  cin >> score;`
`  String grade;`

`  if (score >= 90) {`
`    grade = "A";`
`  } else if (score >= 80) {`
`      grade = "B";`
`  } else if (score >= 70) {`
`      grade = "C";`
`  } else if (score >= 60) {`
`      grade = "D";`
`  } else {`
`    grade = "F";`
`  }`

`  cout << score << ", ";`
`  Serial.println(grade);    //must use Serial.println because grade is a String`
`}`

## Logical Operators (05A)

```void loop() {
bool op1 = false, op2 = false;  cout << "Enter true(1) or false(0) for two operands: " << endl;  cin >> op1;  cin >> op2;  bool answer1 = op1 && op2;  bool answer2 = op1 || op2;      cout << op1 << " and " << op2 << " = " << answer1 << endl;  cout << op1 << " or " << op2 << " = " << answer2  << endl;
}```

## Switch Statement (04A)

`void setup() {  Serial.begin(9600);  cout << "Enter a 1, 2 or 3: " << endl;}void loop() {  int  num;  if (Serial.available())  {    cin >> num;    while (Serial.available())  {  //put this after your cin statement      Serial.read();    }    switch (num) {  //can only switch on integer or char      case 1:        cout << "Apple" << endl;        break;      case 2:        cout << "Banana" << endl;        break;      case 3:        cout << "Orange" << endl;        break;      default:        cout << "Invalid Entry" << endl;    }  }}`

## Sensor Calibration (also max & min) (6B) and while loop

`#include <ArduinoSTL.h>`
`using namespace std;`
`#define SENSOR       0`

`void setup() {`
`  Serial.begin(9600);`
`  analogReference(DEFAULT);`
`}`
`void loop() {`
`  bool keypress = false;`
`  int reading;`
`  double doubleReading = 0.0;`
`  int max = 0, min = 1023;`
`  cout << "Calibrate the sensor, then enter any key..." << endl;`
`  while (!keypress) {`
`    if (Serial.available())  {`
`      Serial.read();`
`      keypress = true;`
`    }`
`    reading = analogRead(SENSOR);`
`    if (reading > max) {`
`      max = reading;`
`    }`
`    else  if (reading < min)  {`
`      min = reading;`
`    }`
`  }   `
`  cout << "min is " << min << " max is " << max << endl;`
`  keypress = false;`
`  do {`
`    if (Serial.available()) {`
`      Serial.read();`
`      keypress = true;`
`    }`
`    reading = analogRead(SENSOR);`
`    double scaledPercentage = 0.0;`
`    if (keypress == true)  {`
`      cout << "new reading is " << reading << endl;`
`      scaledPercentage = 100.0 * (reading - min) / (max - min);`
`      cout << "scaled reading as a percentage is  " << scaledPercentage << "%" << endl;`
`    }`
`  } while (! keypress);`
`  delay(1000);`
`}`

## do-while loop used to validate user input (7B)

`#include <ArduinoSTL.h>`
`using namespace std;`

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

`void loop() {  `
`    int a = readPosNum("Enter a positive number: ");`
`    int b = readPosNum("Enter a positive number: ");`
`    cout << a << " " << b << endl;`
`    swap(a, b);`
`    cout << "After calling function swap: " << a  << " " << b << endl;`
`}`

`void swap(int& var1, int& var2) {`
`    int temp = var1;`
`    var1 = var2;`
`    var2 = temp;`
`}`

`int readPosNum(String prompt) {`
`  int input = -1;`
`  do {`
`    Serial.print(prompt);`
`    cin >> input;`
`    while (Serial.available())  {  //put this after your cin statement`
`      Serial.read();`
`    }`
`    cout << input << endl;`
`    if (input <= 0) {`
`      cout << "Error: number must be > 0" << endl;`
`    }`
`  } while (input <= 0);`
`  return input;`
`}`

## Pass By Reference (7B) and getting input from user

`#include <ArduinoSTL.h>`
`using namespace std;`

`void setup() {`
`  Serial.begin(9600);`
`  cout << "Enter two numbers separated by a space: " << endl;`
`}`

`void loop() {`
`  if (Serial.available()) {`
`    int a = 0;`
`    int b = 0;`
`    cin >> a >> b;`
`    cout << a << " " << b << endl;`
`    swap(a, b);`
`    cout << "After calling function swap: " << a  << " " << b << endl;`
`  }`
`}`

`void swap(int& var1, int& var2) {`
`    int temp = var1;`
`    var1 = var2;`
`    var2 = temp;`
`}`

## Random Numbers (8A)

`#include <ArduinoSTL.h>`
`using namespace std;`

`void setup() {`
`  Serial.begin(9600);`
`  cout << "0-9   10-19" << endl;`
`  randomSeed(analogRead(0));`
`}`
`void loop() {`
`  long randNumber = random(10); // 0-9`
`  long randNumber2 = random(10, 20); // 10-19`
`  cout << randNumber << " and " << randNumber2 << endl;`
`  delay(1500);`
`}`

`#include <ArduinoSTL.h>`
`using namespace std;`

`void print(const int values[], int size) {`
`  for (int i = 0; i < size; i++) {`
`    cout << values[i] << " ";`
`  }`
`  cout << endl;`
`}`
`void print(const double values[], int size) {`
`  for (int i = 0; i < size; i++) {`
`    cout << values[i] << " ";`
`  }`
`  cout << endl;`
`}`

`void print(const char values[], int size) {`
`  for (int i = 0; i < size; i++) {`
`    cout << values[i] << " ";`
`  }`
`  cout << endl;`
`}`
`void setup() {`
`  Serial.begin(9600);`
`}`

`void loop() {`
`  const int NUM_SCORES = 10;`
`  int scores[NUM_SCORES] = {90, 91, 92, 93, 94, 95, 96, 97, 98, 99};`
`  const int NUM_TEMPS = 3;`
`  double temps[NUM_TEMPS] = {25.7, 30.3, 40.9};`
`  const int NUM_CHARS = 6;`
`  char myCharArray[NUM_CHARS] = {'a','e','i','o','u','\0'};`
`  cout << "Integer scores: ";`
`  print(scores, NUM_SCORES);`
`  cout << "Double Temperatures are: ";`
`  print(temps, NUM_TEMPS);`
`  cout << "Char Vowels are: ";`
`  print(myCharArray, NUM_CHARS);`
`  delay(5000);`
`}`

## Vectors (9A&9B)

`#include <ArduinoSTL.h>`
`using namespace std;`

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

`void printVector(vector<int> &myVect)  {`
`  cout << "in printVector" << endl;`
`  for (int i = 0; i < myVect.size(); i++) {`
`    cout << myVect[i] << " ";`
`  }`
`  cout << endl;`
`}`

`void loop() {`
`  /*    Experiments with no size on vector declaration    */`
`  vector<int> myIntegers;`
`  cout << "Printing myIntegers with no size on decl: size is  " << myIntegers.size() << endl;`
`  printVector(myIntegers);`
`  myIntegers.push_back(3);`
`  myIntegers.push_back(5);`
`  myIntegers.push_back(7);`
`  cout << "Printing myIntegers with no size on decl after 3 push_backs: size is  " << myIntegers.size() << endl;`
`  printVector(myIntegers);`

`  /***********************************************************/`
`  /*     Experiments with size on vector declaration         */`
`  `
`  vector<int> myNewVector(3);    //puts 0 in each of 3 elements`
`  cout << "printing myNewVector(3)after decl: size is  " << myNewVector.size() << endl;`
`  printVector(myNewVector);`
`  myNewVector.push_back(99);`
`  myNewVector.push_back(100);`
`  myNewVector.push_back(200);`
`  cout << "printing myNewVector(3)after 3 push_backs: size is  " << myNewVector.size() << endl;`
`  printVector(myNewVector);`
`  myNewVector[0] = 1;`
`  myNewVector[1] = 10;`
`  myNewVector[2] = 20;`
`  myNewVector[3] = 30;   //can I do this?  yes!`
`  myNewVector[8] = 50;   //can I do this?  No!  exceeds current size of vector CAN'T ALTER SIZE WITH ASSIGNMENT`
`  cout << "printing myNewVector(3) after 5 assignments: size is  " << myNewVector.size() << endl;`
`  printVector(myNewVector);`

`  /**************************************************************/`
`  /*   Experiments with initialization of vectors                */`
`  vector<int> myInitVector = {31, 32, 33, 34};  //only with C++11`
`  cout << "printing myInitVector after decl+init: size is " << myInitVector.size() << endl;`
`  printVector(myInitVector);`
`  vector<int> myInitVector2(5,9);  //puts 9 in each of 5 elements`
`  cout << "printing myInitVector2 after decl+init(5,9): size is " << myInitVector2.size() << endl;`
`  printVector(myInitVector2);`
`  cout << "END OF LOOP()" << endl << endl;`
`  `

`  delay(5000);`

## break and continue statements (10A)

`#include <ArduinoSTL.h>`
`using namespace std;`
`bool flag = true;`
`void setup() {`
`  // put your setup code here, to run once:`
`  Serial.begin(9600);`
`}`

`void loop() {`
`    float number, sum = 0.0;`

`    // flag is set by an external source`
`    while (flag)`
`    {`
`        cout << "Enter a number: " << endl;`
`        cin >> number;`
`        `
`        if (number != 0.0)`
`        {`
`            sum += number;`
`        }`
`        else`
`        {`
`            // terminates the while loop if number equals 0.0`
`            break;`
`        }`
`    }`
`    cout << "Sum = " << sum;`

`}`
`for ``(int i = 1; i <= 10; ++i)``{````        ```

`if ````( i == 6 || i == 9)````{``            `

`   ``continue``;````        ```

`}``        `

`cout ``<< i << "\t";``  //prints out 1  2  3  4  5  7  8  10`

`}`

## Insertion Sort (9B) using vector insert, push_back and light sensor

`#include <ArduinoSTL.h>`
`using namespace std;`

`static int SENSOR_PIN = 0;`
`static int NUM_READINGS = 16;`

`void setup() {`
`  Serial.begin(9600);`
`  analogReference(DEFAULT);`
`}`

`void insertionSort(vector<int> &data, int value) {`
`  int i = 0;`
`  bool done = false;`
`  while (done == false)  {`
`    if (value < data[i])  {`
`      data.insert(data.begin() + i, value);`
`      done = true;`
`    }  else {`
`      i++;`
`      if (i == data.size())  {`
`        done = true;`
`      }`
`    }  //end else block`
`  }  //end while loop`
`  if (i == data.size())  {  //got to the end without inserting`
`    data.push_back(value);`
`  }`
`}`

`void loop() {`
`  vector<int> readings;`
`  for (int i = 0; i < NUM_READINGS; i++) {`
`    insertionSort(readings, analogRead(SENSOR_PIN));`
`    delay(100);`
`  }`
`  cout << "Sorted readings: " << endl;`
`  for (int i = 0; i < NUM_READINGS; i++) {`
`    cout << readings[i] << " ";`
`  }`
`  cout << endl;`

`  delay(1000);`
`}`

## Bubble Sort (10A)

`#include <ArduinoSTL.h>`
`using namespace std;`

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

`void printList(const vector<int> &list) {`
`  cout << "{ " ;`
`  for (int i = 0; i < list.size(); i++) {`
`    cout << list[i] << " ";`
`  }`
`  cout << "}" << endl;`
`}`

`void bubbleSort(vector<int> &list) {`
`  int n = list.size();`
`  for (int i = 1; i < n; i++)  {`
`    for (int j = 1; j < n; j++)  {`
`      if (list[j - 1] > list[j])  {`
`        int temp = list[j];`
`        list[j] = list[j - 1];`
`        list[j - 1] = temp;`
`      }`
`    }`
`    printList(list);`
`  }`
`}`

`void loop() {`
`  cout << "Enter some integers separated by spaces (enter zero when you're done):" << endl;`
`  vector<int> list;`
`  int num;`
`  cin >> num;`
`  while (num != 0) {`
`    list.push_back(num);`
`    cin >> num;`
`  }`
`  cout << "Unsorted List: ";`
`  printList(list);`
`  bubbleSort(list);`
`  cout << "Sorted List: ";`
`  printList(list);`
`}`

## Selection Sort (Lab15)

`#include <ArduinoSTL.h>`
`using namespace std;`

`void swap(int &x, int &y)  //using reference variables`
`{`
`  int temp;`
`  temp = x;`
`  x = y;`
`  y = temp;`
`}`

`void print(int myArray[], int size)  {`
`  for (int i = 0; i < size; i++)`
`  {`
`    cout << myArray[i] << " ";`
`  }`
`  cout << endl;`
`}`

`void setup() {`
`  Serial.begin(9600);`
`  const int NUM_INTS = 10;`
`  int a[NUM_INTS] = {99, 22, 3, 0, 56, 101, 2, 23, 27, 22};`
`  // Print unsorted results`
`  cout << "Unsorted: ";`
`  print(a, NUM_INTS);`
`  int minIndex = 0;`
`  // Selection Sort`
`  for (int i = 0; i < (NUM_INTS - 1); i++)  {`
`    minIndex = i;`
`    // Find the index of the minimum element`
`    for (int j = i + 1; j < NUM_INTS; j++)  {`
`       if (a[j] < a[minIndex]) {`
`        minIndex = j;`
`      }`
`    }  //end inner for loop`
`    swap(a[i], a[minIndex]);`
`  }  //end outer for loop`
`  // Print sorted results`
`  cout << "Sorted:  ";`
`  print(a, NUM_INTS);`
`}   //end function setup()`

`void loop()`
`{`

`}`

## Pointers (10A)

`/* CS-11M `
`  Name: pointers`
`  Purpose: pointers practice`

`  @author Sharon Strelitz`
`  @version 1.0  4/2/2017`
`  output:`
`  The address of x is 0x8fa the value of x is 5`
`  The value of p is 0x8fa the value of *p is 5`
`  The address of y is 0x8f8 the value of y is 9`
`  The value of q is 0x8f8 the value of *q is 9`
`  The address of p (not its contents!) is 0x8f6`
`  The address of q (not its contents!) is 0x8f4`
`*/`

`#include <ArduinoSTL.h>`
`using namespace std;`

`void setup() {`
`  Serial.begin(9600);`
`  int x = 5;`
`  int y = 9;`
`  int* p = &x;`
`  int* q = &y;`
`  cout << "The address of x is " << &x <<  " the value of x is " << x << endl;`
`  cout << "The value of p is " << p <<  " the value of *p is " << *p << endl;`
`  cout << "The address of y is " << &y <<  " the value of y is " << y << endl;`
`  cout << "The value of q is " << q <<  " the value of *q is " << *q << endl;`
`  cout << "The address of p (not its contents!) is " << &p << endl;`
`  cout << "The address of q (not its contents!) is " << &q << endl;`
`}`

## Pointers (passing pointers to functions) (10A)

`void setup() {`
`  pinMode(LED_BUILTIN, OUTPUT);`
`}`

`void BlinkLED(int pin, int *whichState)  {`
`  digitalWrite(pin, *whichState);`
`  delay(500);`
`  *whichState = !(*whichState);`
`}`

`void loop() {`
`  static int state = HIGH;  //static means that state will retain its value from the previous loop`
`  BlinkLED(LED_BUILTIN, &state);`
`}`

## Class Student (12A)

Here is completed code:  ClassStudent.ino,  ClassStudent.h and ClassStudent.cpp

ClassStudent.h
`#ifndef CLASSSTUDENT_H`
`#define CLASSSTUDENT_H`

`#include "Arduino.h"`

`class Student  {`
`public:`
`  Student(String name, int ID, String hobby);`
`  Student(String name, int ID);`
`  Student();`
`  void setName(String name);`
`  String getName() const;`
`  void setHobby(String hobby);`
`  String getHobby() const;`
`  void setID(int ID);`
`  int getID() const;`
`  void printStudent() const;`

`private:`
`  String _name;`
`  int _ID;`
`  String _hobby;`
`};`
`#endif`

ClassStudent.cpp
`#include "Arduino.h"`
`#include "ClassStudent.h"`
`#include <ArduinoSTL.h>`
`using namespace std;`

`Student::Student(String name, int ID, String hobby)  {`
`  _name = name;`
`  _ID = ID;`
`  _hobby = hobby;`
`}`

`Student::Student(String name, int ID)  {`
`  _name = name;`
`  _ID = ID;`
`  _hobby = "";`
`}`

`Student::Student()  {`
`  _name = "";`
`  _ID = 0;`
`  _hobby = "";`
`}`
`String Student::getHobby() const  {`
`  return _hobby;`
`}`

`void Student::setHobby(String hobby)  {`
`  _hobby = hobby;`
`}`

`String Student::getName() const  {`
`  return _name;`
`}`

`void Student::setName(String name)  {`
`  _name = name;`
`}`

`int Student::getID() const  {`
`  return _ID;`
`}`

`void Student::setID(int ID)  {`
`  _ID = ID;`
`}`

`void Student::printStudent() const {`
`  Serial.print(getName());`
`  cout << " ID " << getID() << " Hobby: ";`
`  Serial.println(getHobby());`
`}`

ClassStudent.ino
`#include <ArduinoSTL.h>`
`#include "ClassStudent.h"`
`using namespace std;`

`void setup() {`
`  Serial.begin(9600);`
`  Student student1("JoseG", 2345, "Studying");  //max value for ID is 32768`
`  Student student2("HansL", 7891, "Programming");`
`  Student student3("Sharon", 2121, "Teaching C++");`
`  student1.printStudent();`
`  student2.printStudent();`
`  student3.printStudent();`
`  student1.setName("Juan");`
`  student2.setHobby("Java");`
`  student1.printStudent();`
`  student2.printStudent();`
`  student3.printStudent();`
`}`

`void loop() {`
`  // put your main code here, to run repeatedly:`

`}`

## Class Elevator (Lab 19)

Elevator.h

`#ifndef ELEVATOR_H`
`#define ELEVATOR_H`
`#include "Arduino.h"`
`#include <Servo.h>`
`#include <ArduinoSTL.h>`
`using namespace std;`

`class Elevator  {`
`  public:`
`    Elevator(int position = 0, Servo *servo = 0);`
`    void moveElevator(int position);`
`    void moveElevatorMultiple(vector<int> &data, bool up);`
`    bool Elevator::checkFloors(vector<int> &dataup, vector<int> &datadown);`

`  private:`
`    int _position;`
`    Servo* _myServo;`
`};`
`#endif`

Elevator.cpp

`#include "Arduino.h"`
`#include "elevator.h"`
`#include <ArduinoSTL.h>`
`#include <Servo.h>`
`using namespace std;`

`Elevator::Elevator(int position = 0, Servo *servo = 0)  {`
`  _position = position;`
`  _myServo = servo;`
`}`

`void Elevator::moveElevator(int position) {`
`  cout << "writing position " << position << endl;`
`  _myServo->write(position);`
`  _position = position;`
`}`

`void Elevator::moveElevatorMultiple(vector<int> &data, bool up)  {`
`  for (int i = 0; i < data.size(); i++)`
`  {`
`    if (up == false)  {  //going down`
`      data[i] = data[i] * (-1);  //get rid of negative numbers`
`    }`
`    int scaler = (data[i] - 1) * 20; //convert to scale of 0 to 180`
`    //cout << "scaler is " << scaler << endl;`
`    moveElevator(scaler);`
`    delay(1000);`
`  }`
`}`

`bool Elevator::checkFloors(vector<int> &dataup, vector<int> &datadown) {`
`  int maximumFloorUp = dataup[dataup.size() - 1];`
`  int maximumFloorDown = datadown[0] * (-1);  // get rid of negative number`
`  bool okay = true;`
`  if (maximumFloorUp > 10 || maximumFloorDown > 10)  {`
`    cout << "This building has only 10 floors.  Please re-enter"  << endl;`
`    okay = false;`
`    return okay;`
`  }`
`  if (maximumFloorDown >= maximumFloorUp)  {`
`    cout << "maximumFloorUp = " << maximumFloorUp << " maximumFloorDown is " << maximumFloorDown << endl;`
`    cout << "Your biggest negative floor must be less than your biggest positive floor,  please re-enter"  << endl;`
`    okay = false;`
`  }`
`  return okay;`
`}`

Elevator.ino

`#include <ArduinoSTL.h>`
`#include <Servo.h>`
`#include "elevator.h"`
`using namespace std;`
`Servo myServo;`
`const int SERVO_PIN = 9;`

`Elevator myElevator(0, &myServo);`

`void setup() {`
`  Serial.begin(9600);`
`  myServo.attach(SERVO_PIN);`
`  delay(1000);`
`}`

`void sortedInsert(vector<int> &data, int value) { //uses iterators, not required in this course`
`  vector<int>::iterator it = data.begin();`
`  while ( it != data.end() && *it < value ) {`
`    it++;`
`  }`
`  data.insert(it, value);`
`}`
`void printVector(vector<int> &data)  {`
`  for (int i = 0; i < data.size(); i++)  {`
`    cout << data[i] << " ";`
`  }`
`  cout << endl;`
`}`

`void loop() {`
`  int pos = 0;  //always start out at floor 1`
`  myElevator.moveElevator(pos);`
`  cout << "Enter floor numbers (zero when you're done):" << endl;`
`  vector<int> todo;`
`  int floor;`
`  bool done = false;`
`  while (!done) {`
`    cin >> floor;`
`    if (floor != 0)  {`
`      todo.push_back(floor);`
`    }  else  {`
`      done = true;`
`    }`
`  }`
`  cout << "you entered: ";`
`  printVector(todo);`
`  //first sort into two lists:  up and down`
`  vector<int> up;`
`  vector<int> down;`
`  for (int i = 0; i < todo.size(); i++)  {`
`    if (todo[i] > 0)  {`
`      sortedInsert(up, todo[i]);`
`    }  else  {`
`      sortedInsert(down, todo[i]);`
`    }`
`  }`
`  bool okay = myElevator.checkFloors(up, down);`
`  if (okay == false)  {`
`    return;`
`  }`

`  cout << "up is ";`
`  printVector(up);`
`  cout << "down is ";`
`  printVector(down);`
`  cout << "going up" << endl;`

`  myElevator.moveElevatorMultiple(up, true);`
`  // cout << "going down" << endl;`
`  myElevator.moveElevatorMultiple(down, false);`

`}`

## Recursive Washing Dishes (13A) - no value returned from recursion

`#include <ArduinoSTL.h>`
`using namespace std;`

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

`void loop() {`
`  int count = 0;`
`  cout << "\nEnter the number of dishes to wash: ";`
`  cin >> count;`
`  cout << count << endl;`
`  wash(count);`
`  delay(1000);`
`}`

`void wash(int counter) {`
`  if (counter == 0) { // base case`
`    cout << "Done washing dishes" << endl;`
`    return;`
`  } else {`
`    cout << "Washing one dish; number remaining: ";`
`    counter = counter - 1; // solve part of problem`
`    cout << counter << endl;`
`    wash(counter); // recursive call`
`    cout << "Washed " << (counter + 1) << " dishes so far." << endl;`
`  }`
`}`

## Recursive Exponentiation (13A) - calculated value returned from recursion

`void loop() {`
`  int x = 0;`
`  int y = 0;`
`  cout << "\nCalculating power(x, y)" << endl;`
`  cout << "Enter x: ";`
`  cin >> x;`
`  cout << x;`
`  cout << " Enter y: ";`
`  cin >> y;`
`  cout << y << endl;`
`  long p = power(x, y);`
`  cout << "power(" << x << ", " << y << ") = " << p << endl;`
`}`

`long power(long x, long y) {`
`  if (y == 0) {`
`    return 1;`
`  } else {`
`    y = y - 1;`
`    long result = power(x, y);`
`    result = result * x;`
`    return result;`
`  }`
`}`

## ProductAppNewer (14A) cygwin code: passing & returning objects, member and nonmember functions

`#include <iostream>`
`using namespace std;`

`class Product {`
`public:`
`    Product();`
`    Product(string newName, double newPrice);`
`    string getName() const;`
`    double getPrice() const;`
`    void setName(string newName);`
`    void setPrice(double newPrice);`
`    void print() const;`
`private:`
`    string name;`
`    double price;`
`};`

`Product::Product() {`
`    name = "none";`
`    price = 0.0;`
`}`

`Product::Product(string newName, double newPrice) {`
`    setName(newName);`
`    setPrice(newPrice);`
`}`

`string Product::getName() const {`
`    return name;`
`}`

`double Product::getPrice() const {`
`    return price;`
`}`

`void Product::setName(string newName) {`
`    name = newName;`
`}`

`void Product::setPrice(double newPrice) {`
`    price = newPrice;`
`}`

`void Product::print() const {`
`    cout <<  name << " @ " << price << endl;`
`}`

`// Function with Product parameters`
`bool isHigherPrice(Product& prod1, Product& prod2);`

`// Function that returns an object`
`Product makeProduct();`
`Product mysteryProduct(Product &myProduct);`

`// For testing class Product`
`int main() {`
`    cout << "Enter the first product:\n";`
`    Product prod1 = makeProduct();`
`    cout << "Enter the second product:\n";`
`    Product prod2 = makeProduct();`
`    if (isHigherPrice(prod1, prod2)) {`
`        cout << prod1.getName() << " costs more\n";`
`    } else {`
`        cout << prod2.getName() << " costs more\n";`
`    }`
`    Product newProd = mysteryProduct(prod2);`
`    newProd.print();`
`    return 0;`
`}`

`bool isHigherPrice(Product& prod1, Product& prod2) {`
`    if (prod1.getPrice() > prod2.getPrice()) {`
`        return true;`
`    }`
`    return false;`
`}`

`Product makeProduct() {`
`    string name;`
`    cout << "Product name: ";`
`    cin >> name;`
`    double price;`
`    cout << "Price for a "`
`         << name << ": ";`
`    cin >> price;`
`    Product newProd(name, price);`
`    return newProd;`
`}`

`Product mysteryProduct(Product &myProduct) {`
`    Product newProduct("mystery", myProduct.getPrice());`
`    return newProduct;`
`}`

## Practice Final Solution:  Clock

finalclock.ino

`/**`
`  CS-11M`
`  Name: finalclock.ino`
`  Purpose: Creates, sets and then displays the clock ticks using the Clock Class`

`  @author Sharon Strelitz`
`  @version 1.0  5/3/2017`
`*/`
`#include <ArduinoSTL.h>`
`#include "Clock.h"`
`using namespace std;`

`Clock clk;`

`void setup() {`
`  Serial.begin(9600);`
`  clk = setClock();`
`  `
`  vector<int> myVector = clk.getTimeVector();`
`  cout << "myVector.size() is " << myVector.size() << endl;`
`  cout << "time vector is: ";`
`  for (int i = 0; i < myVector.size(); i++)  {`
`    cout << myVector[i] << " ";`
`  }`
`  cout << endl;`
`  `
`  cout << "Enter string you want reversed:  ";`
`  String myString;`
`  while (!Serial.available()); //wait till string available`
`  myString = Serial.readString();`
`  Serial.println(myString);`
`  showBackwards(myString);`
`  cout << endl;`
`  `
`}`
`void loop() {`
`  clk.tick();`
`  Serial.println(clk.toString());`
`  delay(1000);`
`}`
`/**`
`  setClock:  sets the time of the clock from user input`
`  @param no parameters`
`  @return Clock object`
`*/`
`Clock setClock() {`
`  Serial.println("Enter hours, minutes and seconds separated by spaces");`
`  int hours = 0, min = 0, sec = 0;`
`  cin >> hours >> min >> sec;`
`  while (Serial.available())  {  //put this after your cin statement`
`    Serial.read();`
`  }`
`  Clock clk(hours, min, sec);`
`  return clk;`
`}`
`/**`
`    Displays the string backwards.`

`    @param str The string to display in reverse.`
`    @return void`
`*/`
`void showBackwards(String str)  {`
`  if (str.length() == 0)  {`
`    cout << "done" << endl;`
`  }  else  {`
`    String last = str.substring(0, 1);`
`    String newString = str.substring(1, str.length());`
`    showBackwards(newString);`
`    Serial.print(last);`
`  }`
`}`
Clock.h
`/**`
`  CS-11M `
`  Name: Clock.h`
`  Purpose: Provides the interface for the Clock Class`

`  @author Sharon Strelitz`
`  @version 1.0  5/3/2017`
`*/`
`#ifndef CLOCK_H`
`#define CLOCK_H`

`#include "Arduino.h"`
`#include <ArduinoSTL.h>`
`using namespace std;`

`class Clock {`
`  public:`
`    Clock(); // starts at midnight`
`    Clock(int hours, int minutes, int seconds);`
`    // Set seconds, minutes, hours as elements 0, 1, 2 respectively.`
`    void setTime(int time[]);`
`    int getHours() const;`
`    int getMinutes() const;`
`    int getSeconds() const;`
`    // Return current hours, minutes, seconds as`
`    // elements 0, 1, 2 respectively.`
`    vector<int> getTimeVector() const;`
`    String toString() const;`
`    void tick();`

`  private:`
`    int _hours;`
`    int _minutes;`
`    int _seconds;`
`};`
`#endif`
Clock.cpp
`/**`
`  CS-11M`
`  Name: Clock.cpp`
`  Purpose: Provides the implementation of the Clock Class`

`  @author Sharon Strelitz`
`  @version 1.0   5/3/2017`
`*/`
`#include "Clock.h"`

`Clock::Clock() {`
`  _hours = 0;`
`  _minutes = 0;`
`  _seconds = 0;`
`}`
`Clock::Clock(int hours, int minutes, int seconds) {`
`  int t[] = { seconds, minutes, hours };`
`  setTime(t);`
`}`
`void Clock::setTime(int time[]) {`
`  _hours = time[2];`
`  _minutes = time[1];`
`  _seconds = time[0] - 1;`
`  tick();`
`}`
`int Clock::getHours() const {`
`  return _hours;`
`}`
`int Clock::getMinutes() const {`
`  return _minutes;`
`}`
`int Clock::getSeconds() const {`
`  return _seconds;`
`}`
`vector<int> Clock::getTimeVector() const {`
`  vector<int> tv;`
`  tv.push_back(_seconds);`
`  tv.push_back(_minutes);`
`  tv.push_back(_hours);`
`  //vector<int> tv(3);`
`  //tv[0] = _seconds;`
`  //tv[1] = _minutes;`
`  //tv[2] = _hours;`
`  return tv;`
`}`
`String Clock::toString() const {`
`  String timeStr = String("") + getHours() + ":";`
`  int min = getMinutes();`
`  if (min < 10) timeStr += "0";`
`  timeStr += min;`
`  timeStr += ":";`
`  int sec = getSeconds();`
`  if (sec < 10) timeStr += "0";`
`  timeStr += sec;`
`  return timeStr;`
`}`
`void Clock::tick() {`
`  _seconds++;`
`  if (_seconds >= 60) {`
`    _seconds = 0;`
`    _minutes++;`
`  }`
`  if (_minutes >= 60) {`
`    _minutes = 0;`
`    _hours++;`
`  }`
`  if (_hours >= 24) {`
`    _hours = 0;`
`  }`
`}`