-2

I am new to modern c++, so I have difficulties to separate header and cpp file with below code (AES encryption and decryption). For example, how would I separate the code?

https://gist.github.com/edwardstock/3c992fb71320391d3639696328a61115

  • Why would you want to? The reason for separating the code would determine how you do it. Myself, I can't see any reason why you would want to separate that particular code. – john Nov 09 '22 at 06:36
  • Because I want to convert it to common function, and use in other function to encrypt & decrypt with AES. The code is not my gist, I want to reuse it. – Conkel Conkel Nov 09 '22 at 06:41
  • Then the answer below seems reasonable. The API is the encrypt and decrypt functions. The crucial thing is to understand the difference between a declaration and a definition. You will have to add function declarations for encrypt and decrypt to your header file. – john Nov 09 '22 at 06:44

1 Answers1

4

Your API goes to the header file. That includes:

  • Any classes your users might want to use
  • Any function/variable declarations your user might want to use

The implementation of this API goes to the CPP file:

  • Most function definitions
  • Most variable definitions
  • Implementation details (classes, functions your user will never need, etc.)

There are exceptions, like inline function definitions might still go to the header, and templates might have to be in header files as entirely, but this is the general overview.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93