@@ -18,17 +18,24 @@ If you want to write a library called "quick_math" that offers a function "super
1818``` cpp
1919// A file named quick_math.h
2020#pragma once
21+
2122namespace quick_math {
22- double super_root(double x, int n);
23+
24+ double super_root(double x, int n);
25+
2326}
2427```
2528
2629```cpp
2730// A file named quick_math.cpp
28- #include "quick_math.h"
2931#include <cmath>
32+
33+ #include "quick_math.h"
34+
3035double quick_math::super_root(double x, int n) {
31- while(n) { x = std::sqrt(x), --n;}
36+ while (n) {
37+ x = std::sqrt(x), --n;
38+ }
3239 return x;
3340}
3441```
@@ -51,31 +58,45 @@ One possible layout is to keep all the implementation details in the source file
5158// A file named robot_flower.h
5259#if !defined(ROBOT_FLOWER_H)
5360#define ROBOT_FLOWER_H
61+
5462#include <string >
63+
5564namespace robots {
56- class Flower {
57- private:
58- bool needs_water{};
59- int size{};
60- std::string name{};
61- public:
62- Flower(std::string name, int size = 0);
63- void give_water();
64- std::string get_name();
65- int get_size();
66- void start_next_day();
67- };
68- }
65+
66+ class Flower {
67+ private:
68+ bool needs_water{};
69+ int size{};
70+ std::string name{};
71+
72+ public:
73+ Flower(std::string name, int size = 0);
74+ void give_water();
75+ std::string get_name();
76+ int get_size();
77+ void start_next_day();
78+ };
79+ } // namespace robots
6980#endif
7081```
7182
7283``` cpp
7384// A file named robot_flower.cpp
7485#include " robot_flower.h"
75- robots::Flower::Flower (std::string name, int size) {this->name = "Robotica " + name; this->size = size;}
76- void robots::Flower::start_next_day() {if (!needs_water) ++size; needs_water = true;}
77- std::string robots::Flower::get_name() {return name;}
78- int robots::Flower::get_size() {return size;}
86+
87+ robots::Flower::Flower (std::string name, int size) {
88+ this->name = "Robotica " + name;
89+ this->size = size;
90+ }
91+
92+ void robots::Flower::start_next_day() {
93+ if (!needs_water) ++size;
94+ needs_water = true;
95+ }
96+
97+ std::string robots::Flower::get_name() { return name; }
98+
99+ int robots::Flower::get_size() { return size; }
79100```
80101
81102When the header is used as an API overview, that is where a person would look for information like default values.
@@ -87,21 +108,31 @@ Another layout option is a _header only_ library, that does not have a `.cpp` fi
87108```cpp
88109// A file named robot_flower.h
89110#pragma once
111+
90112#include <string>
113+
91114namespace robots {
92- class Flower {
93- private:
94- bool needs_water{};
95- int size{};
96- std::string name{};
97- public:
98- Flower(std::string name, int size = 0) {this->name = "Robotica " + name; this->size = size;}
99- void give_water() {needs_water = false;}
100- std::string get_name() {return name;}
101- int get_size() {return size;}
102- void start_next_day() {if (!needs_water) ++size; needs_water = true;}
103- };
104- }
115+
116+ class Flower {
117+ private:
118+ bool needs_water{};
119+ int size{};
120+ std::string name{};
121+
122+ public:
123+ Flower(std::string name, int size = 0) {
124+ this->name = "Robotica " + name;
125+ this->size = size;
126+ }
127+ void give_water() { needs_water = false; }
128+ std::string get_name() { return name; }
129+ int get_size() { return size; }
130+ void start_next_day() {
131+ if (!needs_water) ++size;
132+ needs_water = true;
133+ }
134+ };
135+ } // namespace robots
105136```
106137
107138Projects might use combinations of these layouts and there is a lot of discussion as to what might be the best fit for each use case.
@@ -129,9 +160,7 @@ int myFunction(int n) {
129160 }
130161}
131162
132- int myOtherFunction(int m) {
133- return myFunction(m / 2);
134- }
163+ int myOtherFunction(int m) { return myFunction(m / 2); }
135164```
136165
137166When `myFunction` is defined, the compiler does not know about `myOtherFunction` yet.
@@ -142,8 +171,8 @@ The compiler assumes that the definition will follow at some later point after t
142171The next example shows how a forward declaration is used for functions.
143172
144173```cpp
145- int myFunction(int n); // Forward declaration of myFunction
146- int myOtherFunction(int m); // Forward declaration of myOtherFunction
174+ int myFunction(int n); // Forward declaration of myFunction
175+ int myOtherFunction(int m); // Forward declaration of myOtherFunction
147176
148177// Definition of myFunction
149178int myFunction(int n) {
@@ -155,9 +184,7 @@ int myFunction(int n) {
155184}
156185
157186// Definition of myOtherFunction
158- int myOtherFunction(int m) {
159- return myFunction(m / 2);
160- }
187+ int myOtherFunction(int m) { return myFunction(m / 2); }
161188```
162189
163190## Include Guards via ifndef
@@ -178,7 +205,9 @@ The syntax can be seen below with `MY_HEADER_FILE_H` as a variable.
178205```cpp
179206#ifndef MY_HEADER_FILE_H /* any name uniquely mapped to file name */
180207#define MY_HEADER_FILE_H
208+
181209// file content
210+
182211#endif
183212```
184213
0 commit comments