https://github.com/rishibakshii/codes
https://github.com/rishibakshii/codes
Last synced: 25 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/rishibakshii/codes
- Owner: RishiBakshii
- Created: 2023-12-30T18:29:21.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-02T19:25:15.000Z (almost 2 years ago)
- Last Synced: 2025-03-21T10:32:49.237Z (7 months ago)
- Language: C++
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

```cpp
// Documentation Section
/* This is a C++ program to find the
factorial of a number
The basic requirement for writing this
program is to have knowledge of loops
To find the factorial of a number
iterate over the range from number to 1
*/// Linking Section
#include
using namespace std;// Definition Section
#define msg "FACTORIAL\n"
typedef int INTEGER;// Global Declaration Section
INTEGER num = 0, fact = 1, storeFactorial = 0;// Function Section
INTEGER factorial(INTEGER num)
{
// Iterate over the loop from
// num to one
for (INTEGER i = 1; i <= num; i++) {
fact *= i;
}// Return the factorial
return fact;
}// Main Function
INTEGER main()
{
// Given number Num
INTEGER Num = 5;// Function Call
storeFactorial = factorial(Num);
cout << msg;// Print the factorial
cout << Num << "! = "
<< storeFactorial << endl;return 0;
}```
### Pointers
## What is a pointer variable?
A pointer variable is a variable that stores the memory address of another variable.
It acts as a reference or "pointer" to the location where data is stored in memory.
It's declared using the asterisk (*) operator before the variable name (e.g., int *ptr;).
Applications of pointer variables:## Dynamic memory allocation: Allocate memory at runtime for data structures like arrays, linked lists, trees.
Passing arguments by reference: Modify function arguments directly, enabling functions to change caller's variables.
Accessing array elements: Efficiently work with arrays, as array names are implicitly pointers to their first elements.
Implementing data structures: Build complex structures like linked lists, trees, and graphs, which heavily rely on pointers.
Handling strings: Manipulate strings, as C-style strings are essentially character arrays accessed using pointers.
Advantages:## Efficient memory management: Dynamically allocate and deallocate memory as needed, avoiding pre-allocation of fixed-size arrays.
Direct memory access: Interact with memory directly for low-level operations and hardware interaction.
Flexibility: Create complex data structures and algorithms that are not possible with regular variables alone.
Disadvantages:## Complexity: Can be challenging to understand and use correctly, increasing the risk of errors like segmentation faults.
Security vulnerabilities: Improper pointer usage can lead to memory corruption, buffer overflows, and security exploits.
Operations on pointer variables:## Assignment: Assign the address of another variable to a pointer (e.g., ptr = &x;).
Dereferencing: Access the value stored at the address pointed to by the pointer (e.g., *ptr = 10;).
Arithmetic: Perform pointer arithmetic (e.g., ptr++, ptr += 2) to move through memory locations.
Comparison: Compare pointers for equality or inequality (e.g., ptr1 == ptr2).
Data types that can be expressed as pointers:## Basic data types: int, float, char, double, etc.
Derived data types: Arrays, structures, unions, classes (in C++).
Function pointers: Store addresses of functions for dynamic function calls.
Key points:Use pointers with care, as incorrect usage can lead to serious errors.
Understand memory management concepts like allocation and deallocation.
Utilize debugging tools to track pointer-related issues.
Consider safer alternatives like smart pointers in C++ when appropriate.**Here's a comprehensive explanation of how object-oriented programming (OOP) offers advantages over structured programming (SP), along with key OOP features:**
**Key Advantages of OOP over SP:**
1. **Encapsulation:**
- OOP encapsulates data (attributes) and the operations that act on that data (methods) within objects.
- This promotes modularity, reusability, and data protection by hiding implementation details and controlling access.
- SP often separates data and functions, leading to less cohesive code and potential data integrity issues.2. **Inheritance:**
- OOP allows new classes (subclasses) to inherit properties and behaviors from existing classes (base classes).
- This facilitates code reuse, extensibility, and the creation of hierarchical relationships between objects.
- SP lacks inheritance, requiring code duplication for similar functionalities.3. **Polymorphism:**
- OOP objects can respond to the same method call in different ways, depending on their class or type.
- This enables flexible and adaptable code that can handle diverse objects without explicitly checking their types.
- SP often relies on conditional statements for different object types, making code less adaptable.4. **Real-world Modeling:**
- OOP aligns well with real-world entities and processes, making it intuitive for modeling complex systems.
- Objects represent real-world entities with attributes and behaviors, mirroring natural relationships.
- SP can model real-world scenarios, but its separation of data and functions can make it less intuitive.**Key Features of OOP:**
- **Objects:** Encapsulated units of data (attributes) and behavior (methods).
- **Classes:** Blueprints for creating objects, defining their attributes and methods.
- **Inheritance:** Establishing hierarchical relationships between classes for code reuse and extensibility.
- **Polymorphism:** Objects of different classes responding to the same method calls in different ways.
- **Abstraction:** Focusing on essential features while hiding implementation details.
- **Encapsulation:** Bundling data and methods together within objects for protection and modularity.**In essence, OOP promotes:**
- Better code organization and modularity
- Enhanced code reusability and maintainability
- Easier modeling of real-world concepts
- Potential for more adaptable and flexible software systems
-
**Here's an explanation of inline functions in C++, highlighting their usefulness:****Inline Functions:**
- **Definition:** Inline functions are functions whose code is substituted directly into the calling code at compile time, rather than generating a separate function call.
- **Declaration:** Defined using the `inline` keyword before the function declaration:```c++
inline int square(int x) {
return x * x;
}
```**Benefits:**
1. **Performance:**
- Eliminate function call overhead, improving performance for small, frequently called functions.
- Avoid context switching and function call setup/cleanup.2. **Code Size:**
- Can reduce code size if used for small functions that are called multiple times.
- Avoid function call instructions and potential code duplication.3. **Readability:**
- Improve code readability by keeping code together instead of jumping to separate function definitions.4. **Encapsulation:**
- Enable encapsulation of small, helper functions within classes without incurring function call overhead.5. **Debugging:**
- Can simplify debugging by having code inline within a single context.**Cautions:**
- **Overuse:** Excessive use can lead to larger code size, potentially slowing compilation and increasing memory usage.
- **Complex Functions:** Inlining large or complex functions might not yield significant performance gains.
- **Compiler Decisions:** The compiler ultimately decides whether to inline a function, based on factors like code size and optimization settings.**Best Practices:**
- Use inline functions for:
- **Small, frequently called functions:** Arithmetic operations, getters/setters, simple calculations.
- **Functions within class definitions:** Encapsulate implementation details without performance overhead.
- Avoid inlining:
- **Large or complex functions:** Unlikely to improve performance and could negatively impact code size.
- **Recursive functions:** Inlining can lead to code explosion.
- Prefer `constexpr` functions for compile-time computations whenever possible.