Skip to content

Commit

Permalink
Introduction to C++ Fundamentals: Syntax, Structure, and Basic Compon…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
nihasinghania22 committed Jul 14, 2024
1 parent 3d63b83 commit 35f29ab
Showing 1 changed file with 216 additions and 1 deletion.
217 changes: 216 additions & 1 deletion docs/day-02/first-cpp-program.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,222 @@ Examples:
- `cout << "Hello, World!" << endl;` - Prints "Hello, World!" to the console.
- `if (age >= 18) { ... }` - Conditional statement to check if age is greater than or equal to 18 and execute code within the block if true.
- `for (int i = 0; i < 3; i++) { ... }` - Loop that iterates three times.
6. Comments (Optional):

Additional Syntax Elements:


5. Semicolons (;):

In C++, semicolons are used to terminate most statements.
They act as a separator between different instructions.
Example:
cppCopyint x = 5;
cout << "Hello";

Forgetting semicolons is a common mistake for beginners and can lead to compilation errors.

6. Curly Braces ({}):

Used to group multiple statements into a single block.
Essential for defining the scope of functions, loops, and conditional statements.
Also used to define the body of classes and namespaces.
Example:
cppCopyif (condition) {
// Multiple statements can be placed here
statement1;
statement2;
}


7. Parentheses ():

Used in function declarations and calls to enclose parameters.
Required in conditional statements and loops to enclose conditions.
Example:
cppCopyint add(int a, int b) {
return a + b;
}

if (x > 0) {
// Do something
}



Data Types:

C++ provides several built-in data types:

Integer types:

int: Whole numbers (e.g., -5, 0, 42)
short: Smaller range integer
long: Larger range integer
unsigned variations of the above


Floating-point types:

float: Single-precision decimal numbers
double: Double-precision decimal numbers


Character types:

char: Single character (e.g., 'A', '7', '$')


Boolean type:

bool: True or false values


Derived types:

arrays: Collections of elements of the same type
pointers: Store memory addresses
references: Aliases for existing variables



Example:
cppCopyint age = 25;
float pi = 3.14159f;
char grade = 'A';
bool isStudent = true;
int numbers[5] = {1, 2, 3, 4, 5}; // Array
int* ptr = &age; // Pointer
int& ageRef = age; // Reference

8. Operators:

C++ provides various types of operators:
a. Arithmetic operators:

Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%)
Example: int result = 10 + 5 * 2; // result is 20

b. Comparison operators:

Equal to (==), Not equal to (!=), Greater than (>), Less than (<),
Greater than or equal to (>=), Less than or equal to (<=)
Example: if (age >= 18) { cout << "Adult"; }

c. Logical operators:

AND (&&), OR (||), NOT (!)
Example: if (age > 18 && hasLicense) { cout << "Can drive"; }

d. Assignment operators:

Simple assignment (=)
Compound assignments (+=, -=, *=, /=, %=)
Example:
cppCopyint x = 5;
x += 3; // Equivalent to x = x + 3;



9. Control Structures:

Control structures direct the flow of program execution:
a. if-else statements:
cppCopyif (condition) {
// Code executed if condition is true
} else if (another_condition) {
// Code executed if another_condition is true
} else {
// Code executed if all conditions are false
}
b. switch statements:
cppCopyswitch (variable) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if no case matches
}
c. Loops:

for loop: Used when number of iterations is known
cppCopyfor (int i = 0; i < 5; i++) {
cout << i << " ";
}

while loop: Executes while a condition is true
cppCopywhile (condition) {
// Code to repeat
}

do-while loop: Executes at least once, then repeats while condition is true
cppCopydo {
// Code to repeat
} while (condition);



10. Functions:

Functions are reusable blocks of code:

Function declaration: Tells compiler about function name, return type, and parameters
Function definition: Provides the actual body of the function
Example:
cppCopy// Declaration
int add(int a, int b);

// Definition
int add(int a, int b) {
return a + b;
}

// Function call
int result = add(5, 3);


Function overloading: Multiple functions can have the same name if they differ in parameter types or number.

11. Basic Input/Output:

C++ uses streams for input/output operations:

cout for output:
cppCopycout << "Hello, " << name << "!";

cin for input:
cppCopyint age;
cin >> age;

Formatting output:
cppCopy#include <iomanip>
cout << fixed << setprecision(2) << 3.14159; // Outputs 3.14



12. Header Files:

Header files contain declarations of functions and variables:

Standard library headers: <iostream>, <string>, <vector>, etc.
User-defined headers: Created for custom libraries
Usage:
cppCopy#include <iostream> // Angular brackets for standard headers
#include "myheader.h" // Quotes for user-defined headers



13. Compilation Process:

C++ programs go through several stages before execution:

Preprocessing: Handles directives like #include and #define
Compilation: Converts C++ code to object code
Linking: Combines object code with libraries to create an executable

14. Comments (Optional):

- Lines starting with `//` (single line comment) or `/* ... */` (multi-line comment).
- Provide explanations within the code for better understanding.
Expand Down

0 comments on commit 35f29ab

Please sign in to comment.