1. Basic Syntax
#include <header_file>
.
#include <iostream>
for input/output (cin, cout)#include <cmath>
for mathematical functions (sqrt, pow, etc.)#include <string>
for string manipulationint main() { ... }
is the entry point of the program.// comment
/* comment */
2. Data Types
int
, long
, long long
: Integers (signed by default)unsigned int
, unsigned long
, unsigned long long
: Unsigned integersfloat
, double
: Floating-point numberschar
: Single character (e.g., ‘a’, ‘!’)bool
: Boolean (true or false)void
: Represents the absence of a type.&
.
int x = 5; int& ref_x = x;
ref_x
also affect x
.3. Variables and Constants
data_type variable_name;
(e.g., int age;
)data_type variable_name = value;
(e.g., double pi = 3.14159;
)const
keyword to declare a constant variable.
const int MAX_VALUE = 100;
4. Input/Output
cin >> variable1 >> variable2;
cout << "Hello, world!" << endl;
endl
inserts a newline character.cout << fixed << setprecision(2) << value;
for fixed-point output with 2 decimal places.5. Operators
Logical: && (AND), | (OR), ! (NOT) |
Bitwise: &, | , ^ (XOR), ~ (NOT), « (left shift), » (right shift) |
6. Control Flow
if (condition) { ... } else { ... }
if (condition) { ... } else if (condition) { ... } else { ... }
switch (expression) { case value1: ...; break; case value2: ...; break; default: ...; }
for (initialization; condition; increment) { ... }
while (condition) { ... }
do { ... } while (condition);
7. Functions
return_type function_name(parameter_list) { ... }
function_name(arguments);
8. Arrays
data_type array_name[size];
(e.g., int numbers[10];
)array_name[index]
(e.g., numbers[0]
)9. Pointers
data_type* pointer_name;
(e.g., int* ptr;
)*pointer_name
(accesses the value pointed to by the pointer)&variable_name
(gets the memory address of the variable)10. Classes and Objects
11. Strings
#include <string>
string myString = "Hello";
length()
, substr()
, find()
)12. Standard Template Library (STL)
vector
), lists (list
), maps (map
), sets (set
), etc.sort
), searching (find
), etc.13. Memory Management
new
operator: Allocates memory dynamically.
int* ptr = new int; *ptr = 5;
delete
operator: Deallocates memory.
delete ptr;
unique_ptr
, shared_ptr
(from <memory>
header)14. Input/Output Streams
ifstream
: Input file stream.ofstream
: Output file stream.fstream
: Both input and output.15. Namespaces
using namespace std;
(brings all entities from the std
namespace into scope)std::cout
, std::cin
(explicitly use namespace)16. Exception Handling
try
, catch
, throw
:
try { ... } catch (exception_type& e) { ... }
17. Object-Oriented Programming (OOP)
public
, private
, protected
.enum
18. Templates
template <typename T> T max(T a, T b) { ... }
**Key Differences from Java **
Logical Expression Evaluation in C++
&&
(AND): Returns true
only if both operands are true
.||
(OR): Returns true
if at least one operand is true
.!
(NOT): Negates the operand (converts true
to false
and vice versa).&&
(AND): If the left operand is false
, the right operand is not evaluated because the result of the entire expression will always be `false