Now open a new tab, and when asked for a filename, enter
telegraph.h
. Yes, we
will create a good old C header file. (To be precise, it will even be a C++
header file.)
TelegraphLibrary/telegraph.h
#ifndef __TELEGRAPH_H__
#define __TELEGRAPH_H__
class Telegraph {
public:
Telegraph(const int output_pin, const int dit_length);
void send_message(const char* message);
private:
void dit();
void dah();
void output_code(const char* code);
void output_symbol(const int length);
int _output_pin;
int _dit_length;
int _dah_length;
};
#endif
Ah, obviously object-oriented programming is not only for the big CPUs any-
more! This is an interface description of a
Telegraph
class that you could use
in your next enterprise project (provided that you need to transmit some
information as Morse code, that is).
We start with the classic double-include prevention mechanism; that is, the
body of the header file defines a preprocessor macro named
__TELEGRAPH_H__
.
We wrap the body (that contains this definition) in an
#ifndef
so that the body
is only compiled if the macro has not been defined. That way, you can include
the header as many times as you want, and the body will be compiled only
once.
The interface of the
Telegraph
class consists of a public part that users of the
class have access to and a private part that only members of the class can
use. In the public part, you find two things: a constructor that creates new
Telegraph
objects and a method named
send_message
that sends a message by
emitting it as Morse code. In your applications, you can use the class as fol-
lows:
Telegraph telegraph(13, 200);
telegraph.send_message("Hello, world!");
report erratum • discuss
Building a Morse Code Generator • 63
www.it-ebooks.info