C++ Guide for EOS Development - Header files

in #eos6 years ago

Featured Image

This post is part of my C++ Guide for EOS developers

  1. Basics
  2. Call by value / reference & Pointers
  3. Classes and Structs
  4. Templates
  5. Iterators & Lambda Expressions
  6. Multi-index
  7. Header files

Header files

There are two main file types in C++. Source files (.cpp) and header files (.hpp or .h).
Header files are the files you include when writing #include <vector> or #include "./Game.hpp".

If you're coming from languages like Java, C#, etc., the concept of Header files might occur weird and unnecessary to you.
But let me try to motivate why C++ does it this way.

C++ separates declarations from definitions.
Declarations introduce an identifier and describe its type. It makes the identifiers known to the compiler.

Examples are:

// extern means exactly this, that the definition is in another file
extern int a;

double square(double d);

class Currency
{
    string name;
    double priceInUSD;

    public:
    Currency(const string &_name, const double price);

    void setName(const string &dogsName);
    void setPrice(double price);
    void print() const;
};

This class declaration actually also acts as a definition, but let's keep it simple to bring the point across.

Definitions instantiate the identifiers. It's what the linker needs after the compile step to assemble the binary.
Examples are initializing variables or implementing the functions:

int a;
int b = 5;
void Currency::setPrice(double price)
{
    priceInUSD = price;
}

void Currency::print() const
{
    cout << name << " is at a price of " << priceInUSD << "USD\n";
}

// ...

C++ has a one definition rule: While you can declare the same variables as often as you want, you can only have one definition of them.

In simple words: Declarations say that an identifier exists somewhere, the definition gives this identifier a face.

Usually, you put your declarations in header files and your definitions in source files.

The reason why C++ separates definitions from declarations, instead of recognizing symbols automatically from the source files like most modern languages do, is because it's 30 years old putting your declarations in header files brings a couple advantages:

  • It improves compile time because the compiler only needs the declarations in the header files. Unnecessary recompilations for implementation changes are therefore mitigated. (Yet, C++ is one of the slowest languages to compile.)
  • It structurally separates the interface of a class from the implementation.
  • You can build against code just by having the headers and don't need access to the source code for the definitions/implementations

The bigger your project becomes, the better it is to structure your code like this, otherwise, your code might become hard to follow.
For the beginning, a single header and a single source file (or even a single source file with both declarations and definitions) is enough.

Learn EOS Development Signup


Originally published at https://cmichel.io

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

@cmichel, go and place your daily vote for Steem on netcoins! http://contest.gonetcoins.com/

Congratulations @cmichel! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard:

SteemitBoard Ranking update - Resteem and Resteemed added

Support SteemitBoard's project! Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.033
BTC 62401.16
ETH 3012.10
USDT 1.00
SBD 3.55