C++ Basics

Getting Started with C++

First Program

untitled
1
2
3
4
5
6
7
        #include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}
      
CPP UTF-8
Lines: 7

Basic Input/Output

untitled
1
2
3
4
5
6
7
8
9
10
        #include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;
    cout << "You entered: " << number << endl;
    return 0;
}
      
CPP UTF-8
Lines: 10

Variables and Data Types

Primitive Data Types

untitled
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        // Integer types
int a = 42;              // -2147483648 to 2147483647
short b = 100;           // -32768 to 32767
long c = 123456789L;     // At least 32 bits
long long d = 123456789LL; // At least 64 bits

// Floating-point types
float e = 3.14f;         // Single precision
double f = 3.14159;      // Double precision
long double g = 3.14159L; // Extended precision

// Character types
char h = 'A';            // Single character
wchar_t i = L'Ω';        // Wide character

// Boolean type
bool j = true;           // true or false

// Size and alignment
cout << "Size of int: " << sizeof(int) << " bytes\n";
cout << "Size of double: " << sizeof(double) << " bytes\n";
      
CPP UTF-8
Lines: 21

Type Modifiers

untitled
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        // Signed vs Unsigned
unsigned int positive = 4294967295;  // 0 to 4294967295
signed int withSign = -42;           // Can be negative

// Size modifiers
short int small = 32767;
long int big = 2147483647;
long long int bigger = 9223372036854775807LL;

// Const values
const double PI = 3.14159;
const char GRADE = 'A';

// Volatile for hardware access
volatile int sensorData;
      
CPP UTF-8
Lines: 15

Type Conversions

untitled
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
        // Implicit conversion
int x = 10;
double y = x;    // int to double

// Explicit conversion (C-style cast)
double pi = 3.14159;
int intPi = (int)pi;  // double to int

// Static cast
float f = 3.14f;
int i = static_cast<int>(f);

// Dynamic cast (for polymorphic types)
class Base { virtual void dummy() {} };
class Derived : public Base { };
Base* base = new Derived;
Derived* derived = dynamic_cast<Derived*>(base);

// Const cast
const int constant = 21;
int* modifier = const_cast<int*>(&constant);

// Reinterpret cast
int* p = new int(65);
char* ch = reinterpret_cast<char*>(p);
      
CPP UTF-8
Lines: 25

Operators

Arithmetic Operators

untitled
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        int a = 10, b = 3;

// Basic arithmetic
int sum = a + b;        // Addition
int diff = a - b;       // Subtraction
int prod = a * b;       // Multiplication
int quot = a / b;       // Division
int rem = a % b;        // Modulus

// Increment/Decrement
int c = ++a;    // Pre-increment
int d = b++;    // Post-increment
int e = --a;    // Pre-decrement
int f = b--;    // Post-decrement

// Compound assignment
int x = 5;
x += 3;         // x = x + 3
x -= 2;         // x = x - 2
x *= 4;         // x = x * 4
x /= 2;         // x = x / 2
x %= 3;         // x = x % 3
      
CPP UTF-8
Lines: 22

Logical and Comparison Operators

untitled
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        bool x = true, y = false;

// Logical operators
bool andResult = x && y;    // Logical AND
bool orResult = x || y;     // Logical OR
bool notResult = !x;        // Logical NOT

// Comparison operators
int a = 5, b = 10;
bool isEqual = a == b;      // Equal to
bool notEqual = a != b;     // Not equal to
bool lessThan = a < b;      // Less than
bool greaterThan = a > b;   // Greater than
bool lessEqual = a <= b;    // Less than or equal
bool greaterEqual = a >= b; // Greater than or equal

// Bitwise operators
int c = 5, d = 3;
int bitwiseAnd = c & d;     // Bitwise AND
int bitwiseOr = c | d;      // Bitwise OR
int bitwiseXor = c ^ d;     // Bitwise XOR
int leftShift = c << 1;     // Left shift
int rightShift = c >> 1;    // Right shift
int bitwiseNot = ~c;        // Bitwise NOT
      
CPP UTF-8
Lines: 24