A hierarchy is a pyramidal arrangement of items (objects, names, values, categories, etc.), in which the items are represented as being "above," "below," or "at the same level as" one another. Wikipedia
Hierarchy is just a technical way of describing the relationship between classes in c++. You would probably not hesitate if I said Both a Circle and a Triangle are a Shape but it might seem weird if I say that Both class Circle and class Triangle inherits from class Shape
Hierarchy is also a way of making a project modular.
If you have a set of classes that you can choose from, building a new class becomes much easier.
Say you'll want to make a new class called TemperatureSensor, this class will need to provide the user with:
void begin();
int read();
void debug(char* debugString);
#ifndef DEBUGGABLE_H #define DEBUGGABLE_H class Debuggable { public: inline void debug(char* debugString){ Serial.println(debugString); } }; #endif
#ifndef SENSOR_H #define SENSOR_H //abstract class Sensor class Sensor { public: //initialize the sensor inline virtual void begin(){ /*nothing*/ }; //read function must be implemented //this is called a pure virtual function virtual int read() = 0; }; #endif
Now, you can create a TemperatureSensor like this:
#ifndef TEMPERATURESENSOR_H #define TEMPERATURESENSOR_H #include <WProgram.h> //include classes Sensor and Debuggable #include "Sensor.h" #include "Debuggable.h" class TemperatureSensor : public Sensor, public Debuggable { public: inline TemperatureSensor( byte userPin ) { pin = userPin; } //read function must be implemented virtual int read(); private: byte pin; }; //avarage a temperature and return the avaraged sum int TemperatureSensor::read(){ //TODO - implement arithmetic so that we return a degree celcius/farenheit float sum = analogRead(pin); for (byte i=0; i<10; i++){ sum = analogRead(pin); } return (int)(.5 + sum / 10.); } #endif
And later, you could add a LightSensor like this:
#ifndef LIGHTSENSOR_H #define LIGHTSENSOR_H #include <WProgram.h> //include classes Sensor and Debuggable #include "Sensor.h" #include "Debuggable.h" class LightSensor : public Sensor, public Debuggable { public: inline LightSensor( byte userPin ) { pin = userPin; } //read function must be implemented virtual int read(); private: byte pin; }; int LightSensor::read(){ //TODO - map( analogRead , 0 , 1024 , min , max ); return analogRead(pin); } #endif
We use hierarchy in order to provide a way to make our code more divided.
This section will walk you through all the steps of making an inherited class from idea to result.
The first thing you will need to do is to define what you want to add to the base class. This could be done in a series of ways but I suggest starting with a list of desired functions and a short description of what they should do.
boolean begin(); return true if device has been initialized successfully initializes the target device boolean available(); return true if device is avalable, false if not. it should not try to poll device if no connection is already established
You will need to know what data and functions you'll inherit, and thus being able to use. It will be a timesaver to either memorize the functions, or to have the headerfile easily accessible.
'Code Analysis:'
First have a look at this: Basic Library Implementation
First have a look at this: Basic Library Implementation
I will extend the Button library and make a Switch library. The switch library will need both a press and a release in order to change state. It's task is to transform a momentary switch (button) to a on-off pushbutton using software logic.
add update() add isOn() add isOff()
Public datas and functions
//Under Construction
//Under Construction
Part of AlphaBeta Tutorials. | |
Last Modified: | January 29, 2017, at 03:08 PM |
By: | Bachar Wehbi |