parent
36eed57ec2
commit
9f5d1aef74
92 changed files with 498 additions and 461 deletions
@ -1 +1,108 @@ |
|||||||
# Basic operations |
# Basic Operations in C++ |
||||||
|
|
||||||
|
Basic operations in C++ refer to the fundamental arithmetic, relational, and logical operations that can be performed using C++ programming language, which are essential for any kind of program or calculation in a real-world scenario. |
||||||
|
|
||||||
|
Here's a summary of the basic operations in C++ |
||||||
|
|
||||||
|
## Arithmetic Operations |
||||||
|
|
||||||
|
These operations are used for performing calculations in C++ and include the following: |
||||||
|
|
||||||
|
- **Addition (+)**: Adds two numbers. |
||||||
|
```cpp |
||||||
|
int a = 5; |
||||||
|
int b = 6; |
||||||
|
int sum = a + b; // sum is 11 |
||||||
|
``` |
||||||
|
|
||||||
|
- **Subtraction (-)**: Subtracts one number from the other. |
||||||
|
```cpp |
||||||
|
int a = 10; |
||||||
|
int b = 6; |
||||||
|
int diff = a - b; // diff is 4 |
||||||
|
``` |
||||||
|
|
||||||
|
- **Multiplication (*)**: Multiplies two numbers. |
||||||
|
```cpp |
||||||
|
int a = 3; |
||||||
|
int b = 4; |
||||||
|
int product = a * b; // product is 12 |
||||||
|
``` |
||||||
|
|
||||||
|
- **Division (/)**: Divides one number by another, yields quotient. |
||||||
|
```cpp |
||||||
|
int a = 12; |
||||||
|
int b = 4; |
||||||
|
int quotient = a / b; // quotient is 3 |
||||||
|
``` |
||||||
|
|
||||||
|
- **Modulus (%)**: Divides one number by another, yields remainder. |
||||||
|
```cpp |
||||||
|
int a = 15; |
||||||
|
int b = 4; |
||||||
|
int remainder = a % b; // remainder is 3 |
||||||
|
``` |
||||||
|
|
||||||
|
## Relational Operators |
||||||
|
|
||||||
|
These operations compare two values and return a boolean value (true/false) depending on the comparison. The relational operations are: |
||||||
|
|
||||||
|
- **Equal to (==)**: Returns true if both operands are equal. |
||||||
|
```cpp |
||||||
|
5 == 5 // true |
||||||
|
3 == 4 // false |
||||||
|
``` |
||||||
|
|
||||||
|
- **Not equal to (!=)**: Returns true if operands are not equal. |
||||||
|
```cpp |
||||||
|
5 != 2 // true |
||||||
|
1 != 1 // false |
||||||
|
``` |
||||||
|
|
||||||
|
- **Greater than (>)**: Returns true if the first operand is greater than the second. |
||||||
|
```cpp |
||||||
|
5 > 3 // true |
||||||
|
2 > 3 // false |
||||||
|
``` |
||||||
|
|
||||||
|
- **Less than (<)**: Returns true if the first operand is less than the second. |
||||||
|
```cpp |
||||||
|
3 < 5 // true |
||||||
|
6 < 5 // false |
||||||
|
``` |
||||||
|
|
||||||
|
- **Greater than or equal to (>=)**: Returns true if the first operand is greater than or equal to the second. |
||||||
|
```cpp |
||||||
|
5 >= 5 // true |
||||||
|
6 >= 2 // true |
||||||
|
3 >= 4 // false |
||||||
|
``` |
||||||
|
|
||||||
|
- **Less than or equal to (<=)**: Returns true if the first operand is less than or equal to the second. |
||||||
|
```cpp |
||||||
|
4 <= 4 // true |
||||||
|
2 <= 3 // true |
||||||
|
5 <= 4 // false |
||||||
|
``` |
||||||
|
|
||||||
|
## Logical Operators |
||||||
|
|
||||||
|
Logical operators are used for combining multiple conditions or boolean values. |
||||||
|
|
||||||
|
- **AND (&&)**: Returns true if both operands are true. |
||||||
|
```cpp |
||||||
|
true && true // true |
||||||
|
true && false // false |
||||||
|
``` |
||||||
|
|
||||||
|
- **OR (||)**: Returns true if any one of the operands is true. |
||||||
|
```cpp |
||||||
|
true || false // true |
||||||
|
false || false // false |
||||||
|
``` |
||||||
|
|
||||||
|
- **NOT (!)**: Returns true if the operand is false and vice versa. |
||||||
|
```cpp |
||||||
|
!true // false |
||||||
|
!false // true |
||||||
|
``` |
@ -1,86 +1,16 @@ |
|||||||
# Features of C++ Compilers |
# Features of C++ Compilers |
||||||
|
|
||||||
C++ compilers are responsible for transforming your human-readable C++ source code into machine-readable object code that can be executed by a computer. In this summary, we'll discuss different features of C++ compilers and, when applicable, provide code examples to illustrate them. |
Different C++ compilers have different features. Some of the most common features of C++ compilers are: |
||||||
|
|
||||||
1. **Standards compliance**: C++ compilers adhere to standard specifications set forth by ISO/IEC, ensuring that your code remains consistent across different platforms and operating systems. The most recent C++ standard is C++20. |
- **Optimization:** Compilers can optimize the code to improve the performance of the program. For example, they can remove redundant code, inline functions, and perform loop unrolling. |
||||||
|
- **Debugging:** Compilers can generate debugging information that can be used to debug the program. |
||||||
|
- **Warnings:** Compilers can generate warnings for suspicious code that may cause errors. |
||||||
|
|
||||||
Example: |
Some of the most popular C++ compilers are: |
||||||
```cpp |
|
||||||
// C++20 feature: concepts |
|
||||||
template<typename T> |
|
||||||
concept bool EqualityComparable = requires(T a, T b) { |
|
||||||
{ a == b } -> bool; |
|
||||||
{ a != b } -> bool; |
|
||||||
}; |
|
||||||
``` |
|
||||||
|
|
||||||
2. **Cross-compilation**: C++ compilers allow you to create executable files for various target platforms, even if your development environment is different. |
- **GNU Compiler Collection (GCC):** GCC is a free and open-source compiler that supports many programming languages, including C++. |
||||||
|
- **Clang:** Clang is a C++ compiler that is part of the LLVM project. It is designed to be compatible with GCC. |
||||||
|
- **Microsoft Visual C++:** Microsoft Visual C++ is a C++ compiler that is part of the Microsoft Visual Studio IDE. |
||||||
|
- **Intel C++ Compiler:** Intel C++ Compiler is a C++ compiler that is part of the Intel Parallel Studio XE suite. |
||||||
|
|
||||||
3. **Optimization**: C++ compilers can perform various optimization techniques to improve the performance or reduce the size of your compiled executable. |
You should go through the documentation of your compiler to learn more about its features. |
||||||
|
|
||||||
Example: |
|
||||||
```cpp |
|
||||||
// Compiler will likely optimize this loop |
|
||||||
int sum = 0; |
|
||||||
for (int i = 0; i < 10; ++i) |
|
||||||
sum += i; |
|
||||||
``` |
|
||||||
|
|
||||||
4. **Header files and libraries**: C++ compilers include standard libraries and support inclusion of custom header files or third-party libraries, which provide additional utilities and functions. |
|
||||||
|
|
||||||
Example: |
|
||||||
```cpp |
|
||||||
#include <iostream> // Standard header for input/output operations |
|
||||||
#include "custom_header.h" // Custom header |
|
||||||
``` |
|
||||||
|
|
||||||
5. **Diagnostics**: C++ compilers produce diagnostic messages about errors or warnings in your source code to help you identify and fix issues. |
|
||||||
|
|
||||||
Example: |
|
||||||
```cpp |
|
||||||
int main() { |
|
||||||
// Compiler will produce a diagnostic message |
|
||||||
// about the missing semicolon |
|
||||||
int a = 5 |
|
||||||
int b = 10; |
|
||||||
return 0; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
6. **Debug and release configurations**: C++ compilers allow you to configure build settings for either debug or release mode, which helps you identify and fix bugs during development, or optimize your code for performance in the final version. |
|
||||||
|
|
||||||
7. **Templates**: C++ compilers support a robust template system that allows you to write generic code which works with different data types, without function or class duplication. |
|
||||||
|
|
||||||
Example: |
|
||||||
```cpp |
|
||||||
template<typename T> |
|
||||||
T add(T a, T b) { |
|
||||||
return a + b; |
|
||||||
} |
|
||||||
|
|
||||||
int main() { |
|
||||||
int result_int = add(1, 2); |
|
||||||
double result_double = add(1.0, 2.0); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
``` |
|
||||||
|
|
||||||
8. **Language features**: C++ compilers support various language features, such as namespaces, classes, inheritance, polymorphism, exceptions, and more, making it possible to write clean, maintainable, and well-structured code. |
|
||||||
|
|
||||||
Example: |
|
||||||
```cpp |
|
||||||
// C++ class and inheritance example |
|
||||||
class Animal { |
|
||||||
public: |
|
||||||
virtual void talk() const = 0; |
|
||||||
}; |
|
||||||
|
|
||||||
class Dog : public Animal { |
|
||||||
public: |
|
||||||
void talk() const override { |
|
||||||
std::cout << "Woof!" << std::endl; |
|
||||||
} |
|
||||||
}; |
|
||||||
``` |
|
||||||
|
|
||||||
These are some of the key features you'll find in C++ compilers, which are essential for developing high-quality C++ applications efficiently. |
|
Loading…
Reference in new issue