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// 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// my_class.cpp
2#include "my_class.h" // header in local directory
3#include <iostream> // header in standard library
4
5using namespace N;
6using namespace std;
7
8void my_class::do_something()
9{
10 cout << "Doing something!" << endl;
11}
12
1// my_program.cpp
2#include "my_class.h"
3
4using namespace N;
5
6int main()
7{
8 my_class mc;
9 mc.do_something();
10 return 0;
11}
12
1// my_class.h
2namespace N
3{
4 class my_class
5 {
6 public:
7 void do_something();
8 };
9
10}
11