Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 70 additions & 41 deletions concepts/headers/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cmath>

#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;
}
```
Expand All @@ -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 <string>

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.
Expand All @@ -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 <string>

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.
Expand Down Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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
```

Expand Down
106 changes: 72 additions & 34 deletions concepts/headers/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cmath>

#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;
}
```
Expand All @@ -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 <string>

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.
Expand All @@ -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 <string>

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.
Expand Down