mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-09-02 13:52:46 +02:00
Join strings and newline char (#8394)
This commit is contained in:
@@ -34,7 +34,7 @@ int main() {
|
|||||||
class HelloWorld {
|
class HelloWorld {
|
||||||
public:
|
public:
|
||||||
void printHello() {
|
void printHello() {
|
||||||
std::cout << "Hello, World!" << '\n';
|
std::cout << "Hello, World!\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -22,7 +22,7 @@ Let's say you have a simple C++ program saved in a file called `hello.cpp`:
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello, World!" << '\n';
|
std::cout << "Hello, World!\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -64,7 +64,7 @@ int main() {
|
|||||||
m["one"] = 1;
|
m["one"] = 1;
|
||||||
m["two"] = 2;
|
m["two"] = 2;
|
||||||
|
|
||||||
std::cout << "Map contains:" << '\n';
|
std::cout << "Map contains:\n";
|
||||||
for (const auto &pair : m) {
|
for (const auto &pair : m) {
|
||||||
std::cout << pair.first << ": " << pair.second << '\n';
|
std::cout << pair.first << ": " << pair.second << '\n';
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@ int main() {
|
|||||||
um["one"] = 1;
|
um["one"] = 1;
|
||||||
um["two"] = 2;
|
um["two"] = 2;
|
||||||
|
|
||||||
std::cout << "Unordered map contains:" << '\n';
|
std::cout << "Unordered map contains:\n";
|
||||||
for (const auto &pair : um) {
|
for (const auto &pair : um) {
|
||||||
std::cout << pair.first << ": " << pair.second << '\n';
|
std::cout << pair.first << ": " << pair.second << '\n';
|
||||||
}
|
}
|
||||||
|
@@ -14,7 +14,7 @@ public:
|
|||||||
|
|
||||||
// Use the same shared data for copying.
|
// Use the same shared data for copying.
|
||||||
MyString(const MyString &other) : data(other.data) {
|
MyString(const MyString &other) : data(other.data) {
|
||||||
std::cout << "Copied using the Copy-Write idiom." << '\n';
|
std::cout << "Copied using the Copy-Write idiom.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make a copy only if we want to modify the data.
|
// Make a copy only if we want to modify the data.
|
||||||
@@ -22,7 +22,7 @@ public:
|
|||||||
// Check if there's more than one reference.
|
// Check if there's more than one reference.
|
||||||
if (data.use_count() > 1) {
|
if (data.use_count() > 1) {
|
||||||
data = std::make_shared<std::string>(*data);
|
data = std::make_shared<std::string>(*data);
|
||||||
std::cout << "Copy is actually made for writing." << '\n';
|
std::cout << "Copy is actually made for writing.\n";
|
||||||
}
|
}
|
||||||
*data = str;
|
*data = str;
|
||||||
}
|
}
|
||||||
|
@@ -17,14 +17,14 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void implementation() {
|
void implementation() {
|
||||||
std::cout << "Default implementation in Base" << '\n';
|
std::cout << "Default implementation in Base\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Derived1 : public Base<Derived1> {
|
class Derived1 : public Base<Derived1> {
|
||||||
public:
|
public:
|
||||||
void implementation() {
|
void implementation() {
|
||||||
std::cout << "Custom implementation in Derived1" << '\n';
|
std::cout << "Custom implementation in Derived1\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -12,28 +12,28 @@ To resolve this ambiguity, you can use virtual inheritance. A virtual base class
|
|||||||
class Base {
|
class Base {
|
||||||
public:
|
public:
|
||||||
void print() {
|
void print() {
|
||||||
std::cout << "Base class" << '\n';
|
std::cout << "Base class\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Derived1 : virtual public Base {
|
class Derived1 : virtual public Base {
|
||||||
public:
|
public:
|
||||||
void derived1Print() {
|
void derived1Print() {
|
||||||
std::cout << "Derived1 class" << '\n';
|
std::cout << "Derived1 class\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Derived2 : virtual public Base {
|
class Derived2 : virtual public Base {
|
||||||
public:
|
public:
|
||||||
void derived2Print() {
|
void derived2Print() {
|
||||||
std::cout << "Derived2 class" << '\n';
|
std::cout << "Derived2 class\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Derived3 : public Derived1, public Derived2 {
|
class Derived3 : public Derived1, public Derived2 {
|
||||||
public:
|
public:
|
||||||
void derived3Print() {
|
void derived3Print() {
|
||||||
std::cout << "Derived3 class" << '\n';
|
std::cout << "Derived3 class\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -15,7 +15,7 @@ Here's an example in C++ demonstrating dynamic polymorphism.
|
|||||||
class Shape {
|
class Shape {
|
||||||
public:
|
public:
|
||||||
virtual void draw() {
|
virtual void draw() {
|
||||||
std::cout << "Drawing a shape" << '\n';
|
std::cout << "Drawing a shape\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@ public:
|
|||||||
class Circle : public Shape {
|
class Circle : public Shape {
|
||||||
public:
|
public:
|
||||||
void draw() override {
|
void draw() override {
|
||||||
std::cout << "Drawing a circle" << '\n';
|
std::cout << "Drawing a circle\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@ public:
|
|||||||
class Rectangle : public Shape {
|
class Rectangle : public Shape {
|
||||||
public:
|
public:
|
||||||
void draw() override {
|
void draw() override {
|
||||||
std::cout << "Drawing a rectangle" << '\n';
|
std::cout << "Drawing a rectangle\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -10,14 +10,14 @@ Here is a basic example of how `dynamic_cast` can be used:
|
|||||||
class BaseClass {
|
class BaseClass {
|
||||||
public:
|
public:
|
||||||
virtual void display() {
|
virtual void display() {
|
||||||
std::cout << "BaseClass" << '\n';
|
std::cout << "BaseClass\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class DerivedClass : public BaseClass {
|
class DerivedClass : public BaseClass {
|
||||||
public:
|
public:
|
||||||
void display() {
|
void display() {
|
||||||
std::cout << "DerivedClass" << '\n';
|
std::cout << "DerivedClass\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -15,14 +15,14 @@ int main() {
|
|||||||
// Some code here...
|
// Some code here...
|
||||||
|
|
||||||
if (/*some error condition*/) {
|
if (/*some error condition*/) {
|
||||||
std::cout << "An error occurred." << '\n';
|
std::cout << "An error occurred.\n";
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// More code here...
|
// More code here...
|
||||||
|
|
||||||
if (/*another error condition*/) {
|
if (/*another error condition*/) {
|
||||||
std::cout << "Another error occurred." << '\n';
|
std::cout << "Another error occurred.\n";
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ void some_function() {
|
|||||||
// Some code here...
|
// Some code here...
|
||||||
|
|
||||||
if (/*some error condition*/) {
|
if (/*some error condition*/) {
|
||||||
std::cout << "An error occurred." << '\n';
|
std::cout << "An error occurred.\n";
|
||||||
std::exit(1);
|
std::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -19,7 +19,7 @@ template <typename T>
|
|||||||
class MyContainer {
|
class MyContainer {
|
||||||
public:
|
public:
|
||||||
void print() {
|
void print() {
|
||||||
std::cout << "Generic container." << '\n';
|
std::cout << "Generic container.\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ template <>
|
|||||||
class MyContainer<int> {
|
class MyContainer<int> {
|
||||||
public:
|
public:
|
||||||
void print() {
|
void print() {
|
||||||
std::cout << "Container for integers." << '\n';
|
std::cout << "Container for integers.\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -33,7 +33,7 @@ Example of a source file:
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
void Example::printMessage() {
|
void Example::printMessage() {
|
||||||
std::cout << "Hello, code splitting!" << '\n';
|
std::cout << "Hello, code splitting!\n";
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -35,8 +35,8 @@ int main() {
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cerr << "An error occurred." << '\n';
|
std::cerr << "An error occurred.\n";
|
||||||
std::clog << "Logging information." << '\n';
|
std::clog << "Logging information.\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -25,7 +25,7 @@ Here are a few examples to demonstrate the use of lambda functions in C++:
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
auto printHello = []() {
|
auto printHello = []() {
|
||||||
std::cout << "Hello, World!" << '\n';
|
std::cout << "Hello, World!\n";
|
||||||
};
|
};
|
||||||
printHello(); // Output: Hello, World!
|
printHello(); // Output: Hello, World!
|
||||||
```
|
```
|
||||||
|
@@ -13,7 +13,7 @@ C++ provides the following logical operators:
|
|||||||
```cpp
|
```cpp
|
||||||
int a = 5, b = 10;
|
int a = 5, b = 10;
|
||||||
if (a > 0 && b > 0) {
|
if (a > 0 && b > 0) {
|
||||||
std::cout << "Both values are positive." << '\n';
|
std::cout << "Both values are positive.\n";
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
- **OR Operator (||)**
|
- **OR Operator (||)**
|
||||||
@@ -25,7 +25,7 @@ C++ provides the following logical operators:
|
|||||||
```cpp
|
```cpp
|
||||||
int a = 5, b = -10;
|
int a = 5, b = -10;
|
||||||
if (a > 0 || b > 0) {
|
if (a > 0 || b > 0) {
|
||||||
std::cout << "At least one value is positive." << '\n';
|
std::cout << "At least one value is positive.\n";
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ C++ provides the following logical operators:
|
|||||||
```cpp
|
```cpp
|
||||||
int a = 5;
|
int a = 5;
|
||||||
if (!(a < 0)) {
|
if (!(a < 0)) {
|
||||||
std::cout << "The value is not negative." << '\n';
|
std::cout << "The value is not negative.\n";
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ Using these operators, you can create more complex logical expressions, for exam
|
|||||||
int a = 5, b = -10, c = 15;
|
int a = 5, b = -10, c = 15;
|
||||||
|
|
||||||
if (a > 0 && (b > 0 || c > 0)) {
|
if (a > 0 && (b > 0 || c > 0)) {
|
||||||
std::cout << "At least two values are positive." << '\n';
|
std::cout << "At least two values are positive.\n";
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -30,7 +30,7 @@ class Animal
|
|||||||
public:
|
public:
|
||||||
void eat()
|
void eat()
|
||||||
{
|
{
|
||||||
std::cout << "I can eat!" << '\n';
|
std::cout << "I can eat!\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ class Mammal
|
|||||||
public:
|
public:
|
||||||
void breath()
|
void breath()
|
||||||
{
|
{
|
||||||
std::cout << "I can breathe!" << '\n';
|
std::cout << "I can breathe!\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ class Dog : public Animal, public Mammal
|
|||||||
public:
|
public:
|
||||||
void bark()
|
void bark()
|
||||||
{
|
{
|
||||||
std::cout << "I can bark! Woof woof!" << '\n';
|
std::cout << "I can bark! Woof woof!\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -13,7 +13,7 @@ To create a new thread, include the `<thread>` header file and create an instanc
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
void my_function() {
|
void my_function() {
|
||||||
std::cout << "This function is executing in a separate thread" << '\n';
|
std::cout << "This function is executing in a separate thread\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
@@ -13,7 +13,7 @@ public:
|
|||||||
int age;
|
int age;
|
||||||
|
|
||||||
void bark() {
|
void bark() {
|
||||||
std::cout << name << " barks!" << '\n';
|
std::cout << name << " barks!\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
@@ -47,7 +47,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void bark() {
|
void bark() {
|
||||||
std::cout << name << " barks!" << '\n';
|
std::cout << name << " barks!\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
@@ -62,14 +62,14 @@ Inheritance is the concept of deriving new classes from existing ones, which ena
|
|||||||
class Animal {
|
class Animal {
|
||||||
public:
|
public:
|
||||||
void breathe() {
|
void breathe() {
|
||||||
std::cout << "I can breathe" << '\n';
|
std::cout << "I can breathe\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Dog : public Animal {
|
class Dog : public Animal {
|
||||||
public:
|
public:
|
||||||
void bark() {
|
void bark() {
|
||||||
std::cout << "Dog barks!" << '\n';
|
std::cout << "Dog barks!\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
@@ -90,21 +90,21 @@ Polymorphism allows you to use a single interface to represent different types.
|
|||||||
class Animal {
|
class Animal {
|
||||||
public:
|
public:
|
||||||
virtual void makeSound() {
|
virtual void makeSound() {
|
||||||
std::cout << "The Animal makes a sound" << '\n';
|
std::cout << "The Animal makes a sound\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Dog : public Animal {
|
class Dog : public Animal {
|
||||||
public:
|
public:
|
||||||
void makeSound() override {
|
void makeSound() override {
|
||||||
std::cout << "Dog barks!" << '\n';
|
std::cout << "Dog barks!\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Cat : public Animal {
|
class Cat : public Animal {
|
||||||
public:
|
public:
|
||||||
void makeSound() override {
|
void makeSound() override {
|
||||||
std::cout << "Cat meows!" << '\n';
|
std::cout << "Cat meows!\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
@@ -32,7 +32,7 @@ class MyClass_Impl // the actual implementation
|
|||||||
public:
|
public:
|
||||||
void some_method()
|
void some_method()
|
||||||
{
|
{
|
||||||
std::cout << "Implementation method called!" << '\n';
|
std::cout << "Implementation method called!\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -10,7 +10,7 @@ The first program that most people learn to write in any programming language is
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello, World!" << '\n';
|
std::cout << "Hello, World!\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -40,7 +40,7 @@ int main() {
|
|||||||
To output text to the console, we use the `std::cout` object and the insertion operator `<<`. In the "Hello, World!" example, we used the following line to print "Hello, World!" to the console:
|
To output text to the console, we use the `std::cout` object and the insertion operator `<<`. In the "Hello, World!" example, we used the following line to print "Hello, World!" to the console:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
std::cout << "Hello, World!" << '\n';
|
std::cout << "Hello, World!\n";
|
||||||
```
|
```
|
||||||
- `std`: This is the namespace where C++ standard library entities (classes and functions) reside. It stands for "standard"
|
- `std`: This is the namespace where C++ standard library entities (classes and functions) reside. It stands for "standard"
|
||||||
- `std::cout`: The standard "character output" stream that writes to the console
|
- `std::cout`: The standard "character output" stream that writes to the console
|
||||||
|
@@ -38,7 +38,7 @@ Create a new file called `main.cpp` within your project and include this code:
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
std::cout << "Hello, World!" << '\n';
|
std::cout << "Hello, World!\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@@ -15,14 +15,14 @@ Here's an example that demonstrates SFINAE in action:
|
|||||||
template <typename T, typename = void>
|
template <typename T, typename = void>
|
||||||
struct foo_impl {
|
struct foo_impl {
|
||||||
void operator()(T t) {
|
void operator()(T t) {
|
||||||
std::cout << "Called when T is not arithmetic" << '\n';
|
std::cout << "Called when T is not arithmetic\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct foo_impl<T, std::enable_if_t<std::is_arithmetic<T>::value>> {
|
struct foo_impl<T, std::enable_if_t<std::is_arithmetic<T>::value>> {
|
||||||
void operator()(T t) {
|
void operator()(T t) {
|
||||||
std::cout << "Called when T is arithmetic" << '\n';
|
std::cout << "Called when T is arithmetic\n";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user