Schedule‎ > ‎

Quiz 4 Preparation

Quiz 4 Preparation   CS-11M  (See 12A slides or google for answers):

Be able to define these C++ terms in your own words:

1)      Encapsulation

2)      Object oriented programming

3)      Abstraction

4)      Access control:  public,  private and protected (focus on public & private, not protected)

5)      Constructor – what is it used for?  How do you figure out which method it is?

6)      What is an instance (aka object)?  How do you create an instance/object?

7)      What goes into a .h file vs. a cpp file vs. a .ino file?

8)      What is the difference between an interface and an implementation (aka definition)?

9)      Be able to write the interface and implementation for a simple class like Class Car

Sample code below:

ClassCar.h 

#ifndef CLASS_CAR_h

#define CLASS_CAR_h

 

#include "Arduino.h"

class Car  {

  public:

    Car(String make, String model, int year);

    Car(String make, String model);

    void setMake(String make);

    String getMake() const;

    void setModel(String model);

    String getModel() const;

    void setYear(int year);

    int getYear() const;

    void print() const;

    int  getCount() const;

    void printCount() const;

 

  private:

    String _make;

    String _model;

    int _year;

};

#endif

ClassCar.cpp

#include "Arduino.h"

#include "ClassCar.h"

#include <ArduinoSTL.h>

using namespace std;

 

Car::Car(String make, String model, int year)  {

  _make = make;

  _model = model;

  _year = year;

}

Car::Car(String make, String model) {

  _make = make;

  _model = model;

  _year = 0;

}

void Car::setMake(String make) {

  _make = make;

}

String Car::getMake() const {

  return _make;

}

void Car::setModel(String model)  {

  _model = model;

}

String Car::getModel() const  {

  return _model;

}

void Car::setYear(int year)  {

  _year = year;

}

int Car::getYear() const  {

  return _year;

}

 

void Car::print() const {

  cout << "Make:  ";

  Serial.print(getMake());

  cout << " Model:  ";

  Serial.print(getModel());

  cout << " Year:  ";

  Serial.println(getYear());

}

Comments