diff --git a/docs/day-03/Basic_syntax.md b/docs/day-03/Basic_syntax.md new file mode 100644 index 000000000..639d6fcb8 --- /dev/null +++ b/docs/day-03/Basic_syntax.md @@ -0,0 +1,115 @@ +--- +sidebar_position: 1 +title: "Basic C++ Syntax Explanation and Examples" +sidebar_label: "Basic C++ Syntax" +slug: Basic C++ Syntax Explanation and Examples +--- + + +## 1. Comments: + +Comments are used to document code for better understanding. They are ignored by the compiler. + +### Examples: + +```cpp +// This is a single-line comment + +/* + This is a multi-line comment +*/ + +``` +## 2. Main Function: + +The `main()` function is the entry point of any C++ program. It's where the execution of the program begins. + +### Example: +```cpp +#include + +int main() { + std::cout << "Hello, World!"; + return 0; +} + +``` + +## 3. Data Types: + +C++ supports various data types for different kinds of values. + +### Example: + +```cpp +int age = 25; // Integer +float pi = 3.14; // Floating point number +char grade = 'A'; // Character +bool isTrue = true; // Boolean (true/false) + + +``` + +## 4. Variables: + +Variables are used to store data values. + +### Example: + +```cpp +int x = 5; // Declare and initialize an integer variable +float y = 3.14; // Declare and initialize a floating point variable +``` + +## 5. Control Structures: + +Control structures help in controlling the flow of the program. + +### Example: + +```cpp +if (x > 0) { + // Code block executed if condition is true +} else { + // Code block executed if condition is false +} + +while (x > 0) { + // Code block executed repeatedly while condition is true +} + +for (int i = 0; i < 5; i++) { + // Code block executed repeatedly with incrementing i from 0 to 4 +} + +``` +## 6. Functions: + +Functions are blocks of code that perform a specific task. + +### Example: + +```cpp +int add(int a, int b) { + return a + b; +} + +``` +## 7. Classes: + +Classes are user-defined data types that contain data members and member functions. + +### Example: + +```cpp +class Rectangle { +public: + int width; + int height; + + int area() { + return width * height; + } +}; + +``` \ No newline at end of file diff --git a/docs/day-03/Comments_in_C++.md b/docs/day-03/Comments_in_C++.md new file mode 100644 index 000000000..39794ed95 --- /dev/null +++ b/docs/day-03/Comments_in_C++.md @@ -0,0 +1,35 @@ +--- +sidebar_position: 3 +title: "Comments in C++" +sidebar_label: "Comments in C++" +slug: Comments in C++ +--- + +# Comments in C++ + +Comments in C++ are essential for documenting code and making it more understandable. There are two types of comments in C++: + +## Single-line comments + +These comments begin with `//` and continue until the end of the line. They are often used for short, inline explanations. + +```cpp +// This is a single-line comment +int x = 5; // This is also a single-line comment +``` +# Multi-line Comments in C++ + +Multi-line comments in C++ begin with `/*` and end with `*/`. They can span multiple lines and are often used for longer explanations or for temporarily disabling blocks of code. + +```cpp +/* This is a multi-line comment + It can span multiple lines */ +int y = 10; + +/* +This is another example of a multi-line comment +that spans multiple lines +*/ +``` + +Comments are ignored by the compiler and do not affect the execution of the program. They are purely for the benefit of developers to understand the code better. It's good practice to include comments to explain complex logic, algorithmic steps, or to provide context for future maintainers of the code. \ No newline at end of file diff --git a/docs/day-03/Structure_of_a_C++ Program.md b/docs/day-03/Structure_of_a_C++ Program.md new file mode 100644 index 000000000..8a7efc710 --- /dev/null +++ b/docs/day-03/Structure_of_a_C++ Program.md @@ -0,0 +1,59 @@ +--- +sidebar_position: 2 +title: "Structure of a C++ Program" +sidebar_label: "Structure of a C++ Program" +slug: Structure of a C++ Program +--- + + +# Structure of a C++ Program + +A typical C++ program consists of several parts: + +## Preprocessor Directives + +These are commands to the compiler and are preceded by a `#` symbol. Common preprocessor directives include `#include` for including header files and `#define` for defining constants. + +## Main Function + +Every C++ program must have a `main()` function, which serves as the entry point of the program. Execution of the program begins from here. + +## Function Definitions + +Additional functions may be defined to perform specific tasks within the program. These functions can be defined before or after the `main()` function. + +## Header Files + +C++ programs often use header files to declare functions, classes, and constants. These header files are included at the beginning of the program using the `#include` directive. + +## Namespace + +Namespace is used to avoid naming conflicts and to organize code into logical groups. Standard library elements are typically in the `std` namespace. + +## Comments + +Comments are used to document the code and improve its readability. They can be single-line (`//`) or multi-line (`/* */`). + +# Example C++ Program + +Here's an example of a simple C++ program that calculates the factorial of a number: + +```cpp +#include + +// Function to calculate factorial +int factorial(int n) { + if (n == 0 || n == 1) + return 1; + else + return n * factorial(n - 1); +} + +int main() { + int num; + std::cout << "Enter a number: "; + std::cin >> num; + std::cout << "Factorial of " << num << " is: " << factorial(num) << std::endl; + return 0; +} +``` \ No newline at end of file diff --git a/docs/day-03/_category_.json b/docs/day-03/_category_.json index 91740c50d..afc85edb1 100644 --- a/docs/day-03/_category_.json +++ b/docs/day-03/_category_.json @@ -1,7 +1,8 @@ { - "label": "Day 03", - "position": 4, - "link": { - "type": "generated-index" + "label": "Day 03", + "position": 4, + "link": { + "type": "generated-index" + } } -} + \ No newline at end of file diff --git a/docs/day-03/cpp-type-modifiers.md b/docs/day-03/cpp-type-modifiers.md deleted file mode 100644 index e73bae422..000000000 --- a/docs/day-03/cpp-type-modifiers.md +++ /dev/null @@ -1,95 +0,0 @@ ---- -sidebar_position: 3 -title: "Type Modifiers in C++" -description: "Type modifiers are used to change the meaning of the fundamental data types. In this tutorial, we will learn about type-modifiers and how to use them in C++ programming with the help of examples." -sidebar_label: "Type Modifiers" -slug: type-modifiers-in-cpp ---- - - -### 1. What is Type Modifiers in C++? -Type modifiers in C++ are keywords that you use to alter the meaning of the basic data types to fit specific needs of your program. They allow you to specify more precisely the properties of variables, such as their size and whether they can hold negative values. The primary type modifiers in C++ are `short`, `long`, `signed`, and `unsigned`. - - -### 2. Types of Type Modifiers in C++ with Proper Examples. -#### Types of Type Modifiers in C++: -- `short` -- `long` -- `signed` -- `unsigned` -- `long long` -- `short int` -- `long int` -- `long double` - -![Type Modifiers in CPP](../../static/img/day-03/type-modifiers-in-cpp.png) - -### 3. short type Modifier -The `short` modifier reduces the size of the `int` data type. Typically, a `short int` is at least 16 bits. -```cpp -short a; // Declares a short integer variable -short int b; // Equivalent to short a -``` -### 4. long type Modifier -The `long` modifier increases the size of the `int` data type. Typically, a `long int` is at least 32 bits. - -Example: - -```cpp -long a; // Declares a long integer variable -long int b; // Equivalent to long a -``` -### 5. signed type Modifier -The `signed` modifier allows a data type to store both positive and negative values. By default, integer types are `signed`. - -Example: - -```cpp - -signed int a; // Explicitly declares a signed integer variable -signed char b; // Declares a signed character variable -``` -### 6. unsigned type Modifier -- These modifiers can be used with `int` and `char` data types. -- `Signed` variables can hold both positive and negative integers, including zero. -- By default, integers are `signed`, so we can directly use `int`. -- `unsigned` variables can hold only non-negative integer values. -Example: -```cpp -signed int x = 23; // Positive-valued integer -signed int y = -13; // Negative-valued integer -unsigned int u = 2; // Positive-valued integer -``` -### 7. long long type Modifier -- The `long long` type modifier can be repeated twice to create the `long long` type. -- It is used for even larger numbers than `long`. -- This modifier can only be used with `int`. -Example: -```cpp -long long num = 12345678; // Very large integer -``` -### 8. short int type Modifier -- The `short int` type modifier is used to create a smaller integer variable that occupies less memory than a regular `int`. -- It restricts the range of values that can be stored in the variable. -- A `short int` typically uses two bytes of memory. -Example: -```cpp -short int smallNumber = 12345; -``` -### 9. long int type Modifier -- The `long int` type modifier is used to create an integer variable that can store larger values than a regular `int`. -- It extends the range of values that an int can hold. -- A `long int` typically uses four bytes of memory (32 bits). -- The range of a long int is -2,147,483,648 to 2,147,483,647. -- The format specifier for `long int` is `%ld`. -Example: -```cpp -long int largeNumber = 1234567890; -``` -### 10. long double type Modifier -The `long double` modifier is used for large floating-point numbers. -Example: -```cpp -long double c = 0.333333333333333333L; // Large floating-point number -``` - diff --git a/docs/day-03/datatypes-in-cpp.md b/docs/day-03/datatypes-in-cpp.md deleted file mode 100644 index f3438a94d..000000000 --- a/docs/day-03/datatypes-in-cpp.md +++ /dev/null @@ -1,132 +0,0 @@ ---- -sidebar_position: 2 -title: "Data Types in C++" -description: "In this tutorial, we will learn about basic data types such as int, float, char, etc. in C++ programming with the help of examples. A data type determines the type and size of an variable." -sidebar_label: "Datatypes" -slug: data-types-in-cpp ---- - -### 1. What is Data Types in C++? -All variables use data type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data they can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data type with which it is declared. Every data type requires a different amount of memory. -#### C++ supports the following data types: - -1. Primary or Built-in or Fundamental data type -2. Derived data types -3. User-defined data types - -![Type Modifiers in CPP](../../static/img/day-03/datatypes-in-cpp.png) - -### 2. C++ Fundamental Data Types - These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char, float, bool, etc. Fundamental data types available in C++ are: - -- Integer -- Character -- Boolean -- Floating Point -- Double Floating Point -- Valueless or Void -- Wide Character -### 3. Examples of Data Types in C++ -- Integer: The keyword used for integer data types is ``int``. Integers typically require 4 bytes of memory space and range from -2147483648 to 2147483647. -- Character: Character data type is used for storing characters. The keyword used for the character data type is ``char``. Characters typically require 1 byte of memory space and range from -128 to 127 or 0 to 255. -- Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean variable can store either `true` or `false`. The keyword used for the Boolean data type is ``bool``. -- Floating Point: Floating Point data type is used for storing single-precision floating-point values or decimal values. The keyword used for the floating-point data type is ``float``. Float variables typically require 4 bytes of memory space. -- Double Floating Point: Double Floating Point data type is used for storing double-precision floating-point values or decimal values. The keyword used for the double floating-point data type is ``double``. Double variables typically require 8 bytes of memory space. -- void: Void means without any value. void data type represents a valueless entity. A ``void`` data type is used for those function which does not return a value. -- Wide Character: Wide character data type is also a character data type but this data type has a size greater than the normal 8-bit data type. Represented by ``wchar_t``. It is generally 2 or 4 bytes long. -- sizeof() operator: ``sizeof()`` operator is used to find the number of bytes occupied by a variable/data type in computer memory. -### 4. C++ Modified Data Types List -1. Enumerations (enum): -Enumerations allow you to define a set of named integer constants. -```cpp -Example: - -enum Color { RED, GREEN, BLUE }; -``` - -2. Structures (struct): -Structures allow you to group related variables of different data types into a single unit. -```cpp -Example: - -struct Point { - int x; - int y; -}; -``` - -3. Unions (union): -Unions allow you to store different data types in the same memory location. -Only one member of the union can be accessed at a time. -```cpp -Example: - -union Data { - int i; - float f; - char c; -}; -``` - -4. Classes (class): -Classes are used for object-oriented programming. -They encapsulate data (attributes) and methods (functions) into a single unit. -```cpp -Example: - -class Circle { - double radius; -public: - double getArea() { return 3.14 * radius * radius; } -}; -``` - -5. Arrays: -Arrays allow you to store multiple elements of the same data type in a contiguous memory block. -```cpp -Example: - -int scores[5] = { 90, 85, 78, 92, 88 }; -``` -### 5. Derived Data Types in C++ - Derived data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely: - -1. Function: -A function is a block of code or program segment that performs a specific well-defined task. -Functions allow you to encapsulate a set of instructions and reuse them. -```cpp -Example: - -int max(int x, int y) { - return (x > y) ? x : y; -} -``` - -2. Array: -An array is a collection of elements of the same data type stored in contiguous memory locations. -It allows you to represent many instances of a value in a single variable. -```cpp -Example: - -int scores[5] = { 90, 85, 78, 92, 88 }; -``` - -3. Pointers: -Pointers are symbolic representations of memory addresses. -They allow programs to simulate call-by-reference and create dynamic data structures. -```cpp -Example: - -int num = 42; -int* ptr = # -``` - -4. References: -References provide an alias for an existing variable. -They allow you to work with the original variable directly. -```cpp -Example: - -int original = 100; -int& ref = original; -``` diff --git a/docs/day-03/variables-literals-constant-in-cpp.md b/docs/day-03/variables-literals-constant-in-cpp.md deleted file mode 100644 index 7f6c0a74a..000000000 --- a/docs/day-03/variables-literals-constant-in-cpp.md +++ /dev/null @@ -1,224 +0,0 @@ ---- -sidebar_position: 1 -title: "Variables, Literals, and Constants in C++" -description: "This is a simple program to demonstrate the use of variables, literals, and constants in C Plus Plus." -sidebar_label: "Variables, Literals, and Constants" -slug: variables-literals-constant-in-cpp ---- - - - -### 1. What is Variables, Literals, and Constants in C++? -#### Variables -- A variable is a container (storage area) used to hold data. -- Each variable is given a unique name (identifier) to indicate its storage area. -```cpp -For example: -int age = 14; // 'age' is a variable of the int data type -age = 17; // Now 'age' is 17 -``` -- Variables allow us to store and manipulate data during program execution. -#### Constants: -- A constant is a value that cannot be changed once it has been defined. -- Constants are treated like regular variables, but their values remain fixed. -- We use the const keyword to declare constants. -```cpp -Example: -const int LIGHT_SPEED = 299792458; // LIGHT_SPEED is a constant -// Attempting to change LIGHT_SPEED will result in an error -``` - -- Constants ensure that specific, unchanging values are used throughout the program. -#### Literals: -- Literals are fixed values used directly in the code. -- They represent specific data without any variables or calculations. -```cpp -Examples of literals: -Integer literals: 9, 42, -5 -Floating-point literals: 3.14, -0.005 -Character literals: 'A', 'b', '1' -String literals: "Hello, World!", "C++ is fun" -``` -- Literals cannot be assigned different values; they remain constant. -### 2. Why do we use Variables, Literals, and Constants in C++? -1. Variables: -- Allow us to store and manipulate data dynamically during program execution. -- Enable flexibility and adaptability in our code. -- Improve readability by giving meaningful names to data. -2. Constants: -- Ensure specific values remain fixed throughout the program. -- Enhance code clarity by using descriptive names for important values. -- Prevent accidental modification of critical data. -3. Literals: -- Provide fixed values directly in the code. -- Improve code readability by eliminating magic numbers. -- Represent specific data without the need for variables. -### 3. Write a simple program to demonstrate the use of Variables, Literals, and Constants in C++. -```c++ -#include -using namespace std; - -int main() { - // Variables - int age = 25; - double pi = 3.14; - char grade = 'A'; - - // Constants - const int LIGHT_SPEED = 299792458; - - // Literals - int num1 = 42; // Integer literal - double num2 = 2.71828; // Floating-point literal - char letter = 'X'; // Character literal - string message = "Hello, World!"; // String literal - - // Display values - cout << "Age: " << age << endl; - cout << "Pi: " << pi << endl; - cout << "Grade: " << grade << endl; - cout << "Speed of light: " << LIGHT_SPEED << " m/s" << endl; - cout << "Integer literal: " << num1 << endl; - cout << "Floating-point literal: " << num2 << endl; - cout << "Character literal: " << letter << endl; - cout << "String literal: " << message << endl; - - return 0; -} -``` -![variables in c++](../../static/img/day-03/variables-literals-constarts-in-cpp.png) - -### 4. Types of Variables in C++ -i. Local Variables: -- Local variables are declared within a function or a block of code. -- They have local scope, meaning they are accessible only within the function or block where they are defined. -- Local variables are created when the function or block is entered and destroyed when it exits. -```c++ -Example: -void myFunction() { - int localVar = 42; // Local variable - // ... -} -``` -ii. Global Variables: -- Global variables are declared outside any function or block. -- They have global scope, meaning they can be accessed from any part of the program. -- Global variables exist throughout the program’s execution. -```c++ -Example: -int globalVar = 100; // Global variable - -void myFunction() { - // Access globalVar here - // ... -} -``` -iii. Static Variables: -- Static variables are local variables with a special property. -- They retain their value between function calls. -- Static variables are initialized only once, even if the function is called multiple times. -```c++ -Example: -void myFunction() { - static int count = 0; // Static variable - count++; - // ... -} -``` - -iv. Register Variables: -- Register variables are used to request the compiler to store the variable in a CPU register for faster access. -- The register keyword hints the compiler to optimize access to this variable. -- Note that the compiler may ignore the request if it deems it unnecessary. -```c++ -Example: -register int speed = 100; // Register variable -``` -### 5. Types of Literals in C++ -Literals are the constant values that are assigned to the variables. Literals represent fixed values that cannot be modified. Literals contain memory but they do not have references as variables. Generally, both terms, constants, and literals are used interchangeably. -For example, “const int = 5;“, is a constant expression and the value 5 is referred to as a constant integer literal. -i. Integer Literals: -- Integer literals represent whole numbers without any fractional part. -- They can be positive or negative. -```c++ -Examples: -Decimal Integer: 42, -123 -Octal Integer: 012, 077 -Hexadecimal Integer: 0xff, 0x2a -Binary Integer (Java SE 7 and above): 0b1010101 -``` -ii. Floating Point Literals: -- Floating-point literals represent decimal numbers with a fractional part. -- They can be either single-precision (float) or double-precision (double). -```c++ -Examples: -3.14, -0.005, 2.71828 -``` -iii. Character Literals: --Character literals represent single characters enclosed in single quotes. -```c++ -Examples: -'A', 'b', '1' -``` -iv. String Literals: --String literals represent sequences of characters enclosed in double quotes. -```c++ -Example: -"Hello, World!" -``` -v. Boolean Literals: -- Boolean literals represent the truth values true or false. -```c++ -Example: -true, false -``` -vi. Pointer Literals: -Pointer literals are memory addresses represented as hexadecimal values. -```c++ -Example: -nullptr (represents a null pointer) -``` -### 6. Types of Constants in C++ -i. Integer Constants: -- Integer constants represent whole numbers without any fractional part. -- They can be positive or negative. -```c++ -Examples: -Decimal Integer: 42, -123 -Octal Integer: 012, 077 -Hexadecimal Integer: 0xff, 0x2a -Binary Integer (since C23): 0b1010101 -``` -ii. Floating Point Constants: -- Floating-point constants represent decimal numbers with a fractional part. -- They can be either single-precision (float) or double-precision (double). -```c++ -Examples: -3.14, -0.005, 2.71828 -``` -iii. Character Constants: -Character constants represent single characters enclosed in single quotes. -```c++ -Examples: -'A', 'b', '1' -``` -iv. String Constants: -- String constants represent sequences of characters enclosed in double quotes. -```c++ -Example: -"Hello, World!" -``` -v. Boolean Constants: -- Boolean constants represent the truth values true or false. -```c++ -Example: -true, false -``` -vi. Pointer Constants: -- Pointer constants are memory addresses represented as hexadecimal values. -```c++ -Example: -nullptr (represents a null pointer) -``` - -