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//headers
2#ifndef TOOLS_hpp
3#define TOOLS_hpp
4#include<vector>
5#include<iostream>
6#include<fstream>
7#include<string>
8using std::ifstream;
9using std::cout;
10using std::cin;
11using std::endl;
12using std::cerr;
13using std::vector;
14using std::string;
15// functions prototypes
16inline vector<int> merge(const vector<int>&a,const vector<int>& b);//merge function prototype with Formal Parameters
17std::vector<int> sort(size_t start, size_t length, const std::vector<int>& vec);//sort function prototype with formal parameters
18#endif
19
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// 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