Add content to C++ roadmap

This commit is contained in:
Kamran Ahmed committed 2023-06-01 03:42:42 +01:00
1 parent 36eed57ec2
commit 9f5d1aef74
92 files changed
+498 -461

No files matched your search

@@ -2,7 +2,7 @@
In C++, a namespace is a named scope or container that is used to organize and enclose a collection of code elements, such as variables, functions, classes, and other namespaces. They are mainly used to divide and manage the code base, giving developers control over name collisions and the specialization of code.
### Syntax
## Syntax
Here's the syntax for declaring a namespace:
@@ -12,11 +12,11 @@ namespace identifier {
}
```
### Using Namespaces
## Using Namespaces
To access elements within a namespace, you can use the scope resolution operator `::`. Here are some examples:
#### Declaring and accessing a namespace
### Declaring and accessing a namespace
```cpp
#include <iostream>
@@ -34,7 +34,7 @@ int main() {
}
```
#### Nesting namespaces
### Nesting namespaces
Namespaces can be nested within other namespaces:
@@ -57,11 +57,11 @@ int main() {
}
```
### `using` Keyword
##`using` Keyword
You can use the `using` keyword to import namespaced elements into the current scope. However, this might lead to name conflicts if multiple namespaces have elements with the same name.
#### Using a single element from a namespace
### Using a single element from a namespace
```cpp
#include <iostream>
@@ -80,7 +80,7 @@ int main() {
}
```
#### Using the entire namespace
### Using the entire namespace
```cpp
#include <iostream>
@@ -2,7 +2,7 @@
**Scope** refers to the visibility and accessibility of variables, functions, classes, and other identifiers in a C++ program. It determines the lifetime and extent of these identifiers. In C++, there are four types of scope:
1. **Global scope:** Identifiers declared outside any function or class have a global scope. They can be accessed from any part of the program (unless hidden by a local identifier with the same name). The lifetime of a global identifier is the entire duration of the program.
- **Global scope:** Identifiers declared outside any function or class have a global scope. They can be accessed from any part of the program (unless hidden by a local identifier with the same name). The lifetime of a global identifier is the entire duration of the program.
```cpp
#include <iostream>
@@ -14,7 +14,7 @@ int main() {
}
```
2. **Local scope:** Identifiers declared within a function or a block have a local scope. They can be accessed only within the function or the block they were declared in. Their lifetime is limited to the duration of the function/block execution.
- **Local scope:** Identifiers declared within a function or a block have a local scope. They can be accessed only within the function or the block they were declared in. Their lifetime is limited to the duration of the function/block execution.
```cpp
#include <iostream>
@@ -31,7 +31,7 @@ int main() {
}
```
3. **Namespace scope:** A namespace is a named scope that groups related identifiers together. Identifiers declared within a namespace have the namespace scope. They can be accessed using the namespace name and the scope resolution operator `::`.
- **Namespace scope:** A namespace is a named scope that groups related identifiers together. Identifiers declared within a namespace have the namespace scope. They can be accessed using the namespace name and the scope resolution operator `::`.
```cpp
#include <iostream>
@@ -45,7 +45,7 @@ int main() {
}
```
4. **Class scope:** Identifiers declared within a class have a class scope. They can be accessed using the class name and the scope resolution operator `::` or, for non-static members, an object of the class and the dot `.` or arrow `->` operator.
- **Class scope:** Identifiers declared within a class have a class scope. They can be accessed using the class name and the scope resolution operator `::` or, for non-static members, an object of the class and the dot `.` or arrow `->` operator.
```cpp
#include <iostream>
@@ -2,7 +2,7 @@
Forward declaration is a way of declaring a symbol (class, function, or variable) before defining it in the code. It helps the compiler understand the type, size, and existence of the symbol. This declaration is particularly useful when we have cyclic dependencies or to reduce compilation time by avoiding unnecessary header inclusions in the source file.
### Class Forward Declaration
## Class Forward Declaration
To use a class type before it is defined, you can declare the class without defining its members, like this:
@@ -23,7 +23,7 @@ public:
However, if you try to make an object of `ClassA` or call its member functions without defining the class, you will get a compilation error.
### Function Forward Declaration
## Function Forward Declaration
Functions must be declared before using them, and a forward declaration can be used to declare a function without defining it:
@@ -40,7 +40,7 @@ int add(int a, int b) {
}
```
### Enum and Typedef Forward Declaration
## Enum and Typedef Forward Declaration
For `enum` and `typedef`, it is not possible to forward declare because they don't have separate declaration and definition stages.
@@ -2,7 +2,7 @@
Code splitting refers to the process of breaking down a large code base into smaller, more manageable files or modules. This helps improve the organization, maintainability, and readability of the code. In C++, code splitting is generally achieved through the use of separate compilation, header files, and source files.
#### Header Files (.h or .hpp)
### Header Files (.h or .hpp)
Header files, usually with the `.h` or `.hpp` extension, are responsible for declaring classes, functions, and variables that are needed by multiple source files. They act as an interface between different parts of the code, making it easier to manage dependencies and reduce the chances of duplicated code.
@@ -21,7 +21,7 @@ public:
#endif
```
#### Source Files (.cpp)
### Source Files (.cpp)
Source files, with the `.cpp` extension, are responsible for implementing the actual functionality defined in the corresponding header files. They include the header files as needed and provide the function and class method definitions.
@@ -37,7 +37,7 @@ void Example::printMessage() {
}
```
#### Separate Compilation
### Separate Compilation
C++ allows for separate compilation, which means that each source file can be compiled independently into an object file. These object files can then be linked together to form the final executable. This provides faster build times when making changes to a single source file since only that file needs to be recompiled, and the other object files can be reused.