cpp

syntax examples

02_syntax_examples.cpp⚙️
/**
 * ============================================================
 * C++ SYNTAX AND STRUCTURE EXAMPLES
 * ============================================================
 * 
 * This file demonstrates the fundamental syntax elements of C++.
 * 
 * Compile: g++ -std=c++17 -Wall 02_syntax_examples.cpp -o syntax_demo
 * Run: ./syntax_demo
 * 
 * ============================================================
 */

#include <iostream>
#include <string>

// Using declarations (selective - recommended approach)
using std::cout;
using std::cin;
using std::endl;
using std::string;

// ============================================================
// NAMESPACES - Organizing Code
// ============================================================

// Custom namespace for math utilities
namespace MathUtils {
    const double PI = 3.14159265359;
    const double E = 2.71828182845;
    
    double circleArea(double radius) {
        return PI * radius * radius;
    }
    
    double circleCircumference(double radius) {
        return 2 * PI * radius;
    }
}

// Nested namespace
namespace Game {
    namespace Physics {
        const double GRAVITY = 9.81;
        
        double fallDistance(double time) {
            return 0.5 * GRAVITY * time * time;
        }
    }
    
    namespace Graphics {
        void render() {
            cout << "Rendering frame..." << endl;
        }
    }
}

// C++17 nested namespace syntax
namespace Company::Department::Team {
    void doWork() {
        cout << "Team working!" << endl;
    }
}

// ============================================================
// FUNCTION PROTOTYPES (Forward Declarations)
// ============================================================

// Declare functions before main(), define after
void demonstrateNamespaces();
void demonstrateBlocks();
void demonstrateSemicolons();
int processArguments(int count, char* values[]);

// ============================================================
// MAIN FUNCTION
// ============================================================

int main(int argc, char* argv[]) {
    cout << "========================================" << endl;
    cout << "   C++ SYNTAX AND STRUCTURE DEMO" << endl;
    cout << "========================================" << endl << endl;
    
    // Process any command-line arguments
    if (argc > 1) {
        processArguments(argc, argv);
    }
    
    // Run demonstrations
    demonstrateNamespaces();
    demonstrateBlocks();
    demonstrateSemicolons();
    
    cout << endl << "Program completed successfully!" << endl;
    return 0;  // EXIT_SUCCESS
}

// ============================================================
// FUNCTION DEFINITIONS
// ============================================================

void demonstrateNamespaces() {
    cout << "--- NAMESPACE DEMO ---" << endl;
    
    // Accessing namespace members with ::
    cout << "Pi = " << MathUtils::PI << endl;
    cout << "Circle area (r=5): " << MathUtils::circleArea(5) << endl;
    
    // Nested namespace access
    cout << "Gravity = " << Game::Physics::GRAVITY << endl;
    cout << "Fall distance (2s): " << Game::Physics::fallDistance(2) << endl;
    
    // Using a namespace locally
    {
        using namespace MathUtils;  // Only in this block
        cout << "E = " << E << endl;
        cout << "Circumference (r=3): " << circleCircumference(3) << endl;
    }
    
    // C++17 nested namespace
    Company::Department::Team::doWork();
    
    cout << endl;
}

void demonstrateBlocks() {
    cout << "--- CODE BLOCKS AND SCOPE DEMO ---" << endl;
    
    int outer = 100;
    cout << "Outer variable: " << outer << endl;
    
    {   // Inner block 1
        int inner1 = 200;
        cout << "Inner block 1 - outer: " << outer << ", inner1: " << inner1 << endl;
        
        {   // Inner block 2
            int inner2 = 300;
            cout << "Inner block 2 - outer: " << outer << ", inner1: " << inner1 
                 << ", inner2: " << inner2 << endl;
        }
        // inner2 is now out of scope
    }
    // inner1 is now out of scope
    
    // Variable shadowing (same name in different scopes)
    int value = 10;
    cout << "Value in outer scope: " << value << endl;
    
    {
        int value = 20;  // Shadows outer 'value'
        cout << "Value in inner scope: " << value << endl;
    }
    
    cout << "Value back in outer scope: " << value << endl;
    
    cout << endl;
}

void demonstrateSemicolons() {
    cout << "--- SEMICOLONS AND STATEMENTS DEMO ---" << endl;
    
    // Declaration statements
    int x;
    int y, z;  // Multiple declarations
    
    // Assignment statements
    x = 10;
    y = 20;
    z = 30;
    
    // Combined declaration and initialization
    int sum = x + y + z;
    
    // Expression statements
    cout << "Sum: " << sum << endl;
    
    // Multiple statements on one line (avoid in real code)
    int a = 1; int b = 2; int c = 3;
    cout << "a=" << a << ", b=" << b << ", c=" << c << endl;
    
    // One statement across multiple lines
    int longCalculation = 1 + 2 + 3 + 4 + 5 +
                          6 + 7 + 8 + 9 + 10 +
                          11 + 12 + 13 + 14 + 15;
    cout << "Long calculation result: " << longCalculation << endl;
    
    // Empty statement (just a semicolon - valid but rarely useful)
    ;
    
    // Compound statement (block)
    {
        int temp = 100;
        temp += 50;
        cout << "Temp in block: " << temp << endl;
    }
    
    cout << endl;
}

int processArguments(int count, char* values[]) {
    cout << "--- COMMAND LINE ARGUMENTS ---" << endl;
    cout << "Program name: " << values[0] << endl;
    cout << "Number of arguments: " << count - 1 << endl;
    
    for (int i = 1; i < count; i++) {
        cout << "  Arg " << i << ": " << values[i] << endl;
    }
    
    cout << endl;
    return count - 1;
}

// ============================================================
// OUTPUT EXAMPLE:
// ============================================================
/*
========================================
   C++ SYNTAX AND STRUCTURE DEMO
========================================

--- NAMESPACE DEMO ---
Pi = 3.14159
Circle area (r=5): 78.5398
Gravity = 9.81
Fall distance (2s): 19.62
E = 2.71828
Circumference (r=3): 18.8496
Team working!

--- CODE BLOCKS AND SCOPE DEMO ---
Outer variable: 100
Inner block 1 - outer: 100, inner1: 200
Inner block 2 - outer: 100, inner1: 200, inner2: 300
Value in outer scope: 10
Value in inner scope: 20
Value back in outer scope: 10

--- SEMICOLONS AND STATEMENTS DEMO ---
Sum: 60
a=1, b=2, c=3
Long calculation result: 120
Temp in block: 150

Program completed successfully!
*/

// ============================================================
// EXERCISES:
// ============================================================
/*
 * 1. Create your own namespace called "Utilities" with at least 
 *    two functions
 * 
 * 2. Create nested namespaces for a game with Physics, Audio, 
 *    and Graphics sub-namespaces
 * 
 * 3. Write a program that demonstrates variable shadowing with
 *    3 levels of nested blocks
 * 
 * 4. Run this program with command-line arguments:
 *    ./syntax_demo arg1 arg2 arg3
 */
Syntax Examples - C++ Tutorial | DeepML