- Please note: This method will change in the future
- In c++20, modules were introduced.
- These will eventually take the place of the techniques we are learning.
- But this is a long way down the road.
- And there is a HUGE amount of legacy code that uses this technique.
- And our compilers do not yet fully support modules.
- Please note: I am typeing these notes as I develop my code, there may be syntax errors in the code in these notes, please refer to the final code for correct examples.
- We will place code in multiple files by
- Putting function prototypes, constants and other definitions in a header file.
- Putting executable code (ie function implementations) in an implementation file.
- Header files.
- These get included in the other code files.
- So they should NOT CONTAIN executable code.
- In practice
- Header files have the .h extension.
- We don't want to pollute the namespace with a
using
command.
- We need to add guards.
- Lets build a Trinary.h file
- Guards
- These are in the preprocessor language
- They keep the code from being compiled multiple times, even if it is included multiple times.
- They can be done two ways.
- The ALWAYS follow the same format.
- Pragma
- This is not standard
- But almost every compiler supports this.
- At the top of the file put
#pragma once
- IFDEF, DEFINE, ENDIF
- So the start of our header file could be
-
#ifndef TRINARY_ROUTINES
#define TRINARY_ROUTINES
#endif
- We will be using the string library so include that.
-
#ifndef TRINARY_ROUTINES
#define TRINARY_ROUTINES
#include <string>
#endif
- I said before we don't want to pollute the name space, so no
using
statement.
- But we need to reference string in the
std::
namespace.
- So we add our functions as follows
-
#ifndef TRINARY_ROUTINES
#define TRINARY_ROUTINES
#include <string>
std::string IntToTrinary(int number);
int TrinaryToInt(std::string number);
#endif
- I am going to use a trick when I write the code
- If I place the available digits in a string, I can calculate their values easily.
- So I want to declare a constant TRINARY_DIGITS{"012"}
- I just add this to the header file.
-
#ifndef TRINARY_ROUTINES
#define TRINARY_ROUTINES
#include <string>
const std::string TRINARY_DIGITS{"012"};
std::string IntToTrinary(int number);
int TrinaryToInt(std::string number);
#endif
- Finally I want a pair of utility routines
- One that converts an integer digit to a trinary digit
-
char IntDigitToTrinary(int d);
- And one that goes the other way.
-
int TrinaryDigitToInt(char d);
- I need to decide if I want to expose these routines to other users,.
- If I do, I am committing to make sure that they are correct and always maintain them.
- Then I would put them in the header.
- If not, I could just put the prototypes in the implementation file.
- For now let's put them into the header file.
- Our final header file becomes
-
#ifndef TRINARY_ROUTINES
#define TRINARY_ROUTINES
#include <string>
const std::string TRINARY_DIGITS{"012"};
std::string IntToTrinary(int number);
int TrinaryToInt(std::string number);
char IntDigitToTrinary(int d);
int TrinaryDigitToInt(char d);
#endif
- Note, this could also be done as :
-
#pragma once
#include <string>
const std::string TRINARY_DIGITS{"012"};
std::string IntToTrinary(int number);
int TrinaryToInt(std::string number);
char IntDigitToTrinary(int d);
int TrinaryDigitToInt(char d);