1// my_class.h
2#ifndef MY_CLASS_H // include guard
3#define MY_CLASS_H
4
5namespace N
6{
7 class my_class
8 {
9 public:
10 void do_something();
11 };
12}
13
14#endif /* MY_CLASS_H */
15
1// You should use header files when you wan't to split your
2// program across multiple files. Use it like this:
3
4// vec2.hpp
5class vec2 {
6 public:
7 void printVec(); // Decleration
8 float x, y;
9}
10// vec2.cpp
11#include "vec2.hpp"
12void vec2::printVec() { // Implementation
13 std::cout << x << y << std::endl;
14}
1// sample.h
2#pragma once
3#include <vector> // #include directive
4#include <string>
5
6namespace N // namespace declaration
7{
8 inline namespace P
9 {
10 //...
11 }
12
13 enum class colors : short { red, blue, purple, azure };
14
15 const double PI = 3.14; // const and constexpr definitions
16 constexpr int MeaningOfLife{ 42 };
17 constexpr int get_meaning()
18 {
19 static_assert(MeaningOfLife == 42, "unexpected!"); // static_assert
20 return MeaningOfLife;
21 }
22 using vstr = std::vector<int>; // type alias
23 extern double d; // extern variable
24
25#define LOG // macro definition
26
27#ifdef LOG // conditional compilation directive
28 void print_to_log();
29#endif
30
31 class my_class // regular class definition,
32 { // but no non-inline function definitions
33
34 friend class other_class;
35 public:
36 void do_something(); // definition in my_class.cpp
37 inline void put_value(int i) { vals.push_back(i); } // inline OK
38
39 private:
40 vstr vals;
41 int i;
42 };
43
44 struct RGB
45 {
46 short r{ 0 }; // member initialization
47 short g{ 0 };
48 short b{ 0 };
49 };
50
51 template <typename T> // template definition
52 class value_store
53 {
54 public:
55 value_store<T>() = default;
56 void write_value(T val)
57 {
58 //... function definition OK in template
59 }
60 private:
61 std::vector<T> vals;
62 };
63
64 template <typename T> // template declaration
65 class value_widget;
66}
67
1#include "enter_name_here.cpp"
2
3//Make sure the files are in the same directory.
4