Basic C++ concepts
Syntax
Structure:
C++ programs typically have the following structure:
#include <header files>
using namespace std;
int main() {
// Code goes here
return 0;
}
Comments:
Single-line comments: //
Multi-line comments: /* */
Semicolons:
End every statement with a semicolon (;).
Data Types
Fundamental types:
int: Integers (whole numbers)
float: Floating-point numbers (decimals)
double: Double-precision floating-point numbers
char: Single characters
bool: Boolean values (true or false)
Modifier types:
signed: Represents both positive and negative values (default for int and char)
unsigned: Represents only non-negative values
Operators
Arithmetic:+, -, *, /, % (modulo)
Assignment:=
Comparison:==, !=, <, >, <=, >=
Logical:&& (and), || (or), ! (not)
Increment/Decrement:++, --
Control Flow
Conditional statements:
if: Executes code if a condition is true
else: Executes code if the condition in the if statement is false
else if: Provides additional conditional branches
Loops:
for: Repeats a block of code a specific number of times
while: Repeats a block of code as long as a condition is true
do-while: Repeats a block of code at least once, then checks a condition
Functions
Definition:
Blocks of code that perform specific tasks
Can be called multiple times from different parts of the program
return_type function_name(parameters) {
// Function body
}
Calling a function:
function_name(arguments);
Pointers
Variables that store memory addresses:
Declare using the * operator: int *ptr;
Accessing values:
*ptr dereferences the pointer (accesses the value at the memory address)
Dynamic memory allocation:
new operator: Allocates memory on the heap
delete operator: Deallocates memory when it's no longer needed
Remember that practice and experimentation are key to understanding these concepts fully.
Comments
Post a Comment