diff --git a/concepts/headers/about.md b/concepts/headers/about.md index 8d59dcba..5983cd3c 100644 --- a/concepts/headers/about.md +++ b/concepts/headers/about.md @@ -18,17 +18,24 @@ If you want to write a library called "quick_math" that offers a function "super ```cpp // A file named quick_math.h #pragma once + namespace quick_math { - double super_root(double x, int n); + +double super_root(double x, int n); + } ``` ```cpp // A file named quick_math.cpp -#include "quick_math.h" #include + +#include "quick_math.h" + double quick_math::super_root(double x, int n) { - while(n) { x = std::sqrt(x), --n;} + while (n) { + x = std::sqrt(x), --n; + } return x; } ``` @@ -51,31 +58,45 @@ One possible layout is to keep all the implementation details in the source file // A file named robot_flower.h #if !defined(ROBOT_FLOWER_H) #define ROBOT_FLOWER_H + #include + namespace robots { - class Flower { - private: - bool needs_water{}; - int size{}; - std::string name{}; - public: - Flower(std::string name, int size = 0); - void give_water(); - std::string get_name(); - int get_size(); - void start_next_day(); - }; -} + +class Flower { + private: + bool needs_water{}; + int size{}; + std::string name{}; + + public: + Flower(std::string name, int size = 0); + void give_water(); + std::string get_name(); + int get_size(); + void start_next_day(); +}; +} // namespace robots #endif ``` ```cpp // A file named robot_flower.cpp #include "robot_flower.h" -robots::Flower::Flower(std::string name, int size) {this->name = "Robotica " + name; this->size = size;} -void robots::Flower::start_next_day() {if (!needs_water) ++size; needs_water = true;} -std::string robots::Flower::get_name() {return name;} -int robots::Flower::get_size() {return size;} + +robots::Flower::Flower(std::string name, int size) { + this->name = "Robotica " + name; + this->size = size; +} + +void robots::Flower::start_next_day() { + if (!needs_water) ++size; + needs_water = true; +} + +std::string robots::Flower::get_name() { return name; } + +int robots::Flower::get_size() { return size; } ``` When 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 ```cpp // A file named robot_flower.h #pragma once + #include + namespace robots { - class Flower { - private: - bool needs_water{}; - int size{}; - std::string name{}; - public: - Flower(std::string name, int size = 0) {this->name = "Robotica " + name; this->size = size;} - void give_water() {needs_water = false;} - std::string get_name() {return name;} - int get_size() {return size;} - void start_next_day() {if (!needs_water) ++size; needs_water = true;} - }; -} + +class Flower { + private: + bool needs_water{}; + int size{}; + std::string name{}; + + public: + Flower(std::string name, int size = 0) { + this->name = "Robotica " + name; + this->size = size; + } + void give_water() { needs_water = false; } + std::string get_name() { return name; } + int get_size() { return size; } + void start_next_day() { + if (!needs_water) ++size; + needs_water = true; + } +}; +} // namespace robots ``` Projects 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) { } } -int myOtherFunction(int m) { - return myFunction(m / 2); -} +int myOtherFunction(int m) { return myFunction(m / 2); } ``` When `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 The next example shows how a forward declaration is used for functions. ```cpp -int myFunction(int n); // Forward declaration of myFunction -int myOtherFunction(int m); // Forward declaration of myOtherFunction +int myFunction(int n); // Forward declaration of myFunction +int myOtherFunction(int m); // Forward declaration of myOtherFunction // Definition of myFunction int myFunction(int n) { @@ -155,9 +184,7 @@ int myFunction(int n) { } // Definition of myOtherFunction -int myOtherFunction(int m) { - return myFunction(m / 2); -} +int myOtherFunction(int m) { return myFunction(m / 2); } ``` ## Include Guards via ifndef @@ -178,7 +205,9 @@ The syntax can be seen below with `MY_HEADER_FILE_H` as a variable. ```cpp #ifndef MY_HEADER_FILE_H /* any name uniquely mapped to file name */ #define MY_HEADER_FILE_H + // file content + #endif ``` diff --git a/concepts/headers/introduction.md b/concepts/headers/introduction.md index e7db48e1..978b94c6 100644 --- a/concepts/headers/introduction.md +++ b/concepts/headers/introduction.md @@ -18,17 +18,24 @@ If you want to write a library called "quick_math" that offers a function "super ```cpp // A file named quick_math.h #pragma once + namespace quick_math { - double super_root(double x, int n); -} + +double super_root(double x, int n); + +} // namespace quick_math ``` ```cpp // A file named quick_math.cpp -#include "quick_math.h" #include + +#include "quick_math.h" + double quick_math::super_root(double x, int n) { - while(n) { x = std::sqrt(x), --n;} + while (n) { + x = std::sqrt(x), --n; + } return x; } ``` @@ -51,31 +58,49 @@ One possible layout is to keep all the implementation details in the source file // A file named robot_flower.h #if !defined(ROBOT_FLOWER_H) #define ROBOT_FLOWER_H + #include + namespace robots { - class Flower { - private: - bool needs_water{}; - int size{}; - std::string name{}; - public: - Flower(std::string name, int size = 0); - void give_water(); - std::string get_name(); - int get_size(); - void start_next_day(); - }; -} + +class Flower { + private: + bool needs_water{}; + int size{}; + std::string name{}; + + public: + Flower(std::string name, int size = 0); + void give_water(); + std::string get_name(); + int get_size(); + void start_next_day(); +}; + +} // namespace robots + #endif ``` ```cpp // A file named robot_flower.cpp #include "robot_flower.h" -robots::Flower::Flower(std::string name, int size) {this->name = "Robotica " + name; this->size = size;} -void robots::Flower::start_next_day() {if (!needs_water) ++size; needs_water = true;} -std::string robots::Flower::get_name() {return name;} -int robots::Flower::get_size() {return size;} + +robots::Flower::Flower(std::string name, int size) { + this->name = "Robotica " + name; + this->size = size; +} + +void robots::Flower::start_next_day() { + if (!needs_water) { + ++size; + } + needs_water = true; +} + +std::string robots::Flower::get_name() { return name; } + +int robots::Flower::get_size() { return size; } ``` When the header is used as an API overview, that is where a person would look for information like default values. @@ -87,21 +112,34 @@ Another layout option is a _header only_ library, that does not have a `.cpp` fi ```cpp // A file named robot_flower.h #pragma once + #include + namespace robots { - class Flower { - private: - bool needs_water{}; - int size{}; - std::string name{}; - public: - Flower(std::string name, int size = 0) {this->name = "Robotica " + name; this->size = size;} - void give_water() {needs_water = false;} - std::string get_name() {return name;} - int get_size() {return size;} - void start_next_day() {if (!needs_water) ++size; needs_water = true;} - }; -} + +class Flower { + private: + bool needs_water{}; + int size{}; + std::string name{}; + + public: + Flower(std::string name, int size = 0) { + this->name = "Robotica " + name; + this->size = size; + } + void give_water() { needs_water = false; } + std::string get_name() { return name; } + int get_size() { return size; } + void start_next_day() { + if (!needs_water) { + ++size; + } + needs_water = true; + } +}; + +} // namespace robots ``` Projects 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.