RAII ties a resource's lifetime to an object's scope.

RAII Scope

raii_scope.cpp
#include <iostream>
#include <string>

class ScopeLog {
private:
    std::string name;

public:
    ScopeLog(std::string label) {
        name = label;
        std::cout << "enter=" << name << std::endl;
    }

    ~ScopeLog() {
        std::cout << "leave=" << name << std::endl;
    }
};

int runJob(std::string label, int workUnits) {
    ScopeLog scope(label);
    int total = 0;
    for (int i = 1; i <= workUnits; i++) {
        total += i;
    }
    return total;
}

int main() {
    std::string label = ;
    int workUnits = ;

    int total = runJob(label, workUnits);

    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <string>

class ScopeLog {
private:
    std::string name;

public:
    ScopeLog(std::string label) {
        name = label;
        std::cout << "enter=" << name << std::endl;
    }

    ~ScopeLog() {
        std::cout << "leave=" << name << std::endl;
    }
};

int runJob(std::string label, int workUnits) {
    ScopeLog scope(label);
    int total = 0;
    for (int i = 1; i <= workUnits; i++) {
        total += i;
    }
    return total;
}

int main() {
    std::string label = ;
    int workUnits = ;

    int total = runJob(label, workUnits);

    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <string>

class ScopeLog {
private:
    std::string name;

public:
    ScopeLog(std::string label) {
        name = label;
        std::cout << "enter=" << name << std::endl;
    }

    ~ScopeLog() {
        std::cout << "leave=" << name << std::endl;
    }
};

int runJob(std::string label, int workUnits) {
    ScopeLog scope(label);
    int total = 0;
    for (int i = 1; i <= workUnits; i++) {
        total += i;
    }
    return total;
}

int main() {
    std::string label = ;
    int workUnits = ;

    int total = runJob(label, workUnits);

    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <string>

class ScopeLog {
private:
    std::string name;

public:
    ScopeLog(std::string label) {
        name = label;
        std::cout << "enter=" << name << std::endl;
    }

    ~ScopeLog() {
        std::cout << "leave=" << name << std::endl;
    }
};

int runJob(std::string label, int workUnits) {
    ScopeLog scope(label);
    int total = 0;
    for (int i = 1; i <= workUnits; i++) {
        total += i;
    }
    return total;
}

int main() {
    std::string label = ;
    int workUnits = ;

    int total = runJob(label, workUnits);

    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <string>

class ScopeLog {
private:
    std::string name;

public:
    ScopeLog(std::string label) {
        name = label;
        std::cout << "enter=" << name << std::endl;
    }

    ~ScopeLog() {
        std::cout << "leave=" << name << std::endl;
    }
};

int runJob(std::string label, int workUnits) {
    ScopeLog scope(label);
    int total = 0;
    for (int i = 1; i <= workUnits; i++) {
        total += i;
    }
    return total;
}

int main() {
    std::string label = ;
    int workUnits = ;

    int total = runJob(label, workUnits);

    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <string>

class ScopeLog {
private:
    std::string name;

public:
    ScopeLog(std::string label) {
        name = label;
        std::cout << "enter=" << name << std::endl;
    }

    ~ScopeLog() {
        std::cout << "leave=" << name << std::endl;
    }
};

int runJob(std::string label, int workUnits) {
    ScopeLog scope(label);
    int total = 0;
    for (int i = 1; i <= workUnits; i++) {
        total += i;
    }
    return total;
}

int main() {
    std::string label = ;
    int workUnits = ;

    int total = runJob(label, workUnits);

    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <string>

class ScopeLog {
private:
    std::string name;

public:
    ScopeLog(std::string label) {
        name = label;
        std::cout << "enter=" << name << std::endl;
    }

    ~ScopeLog() {
        std::cout << "leave=" << name << std::endl;
    }
};

int runJob(std::string label, int workUnits) {
    ScopeLog scope(label);
    int total = 0;
    for (int i = 1; i <= workUnits; i++) {
        total += i;
    }
    return total;
}

int main() {
    std::string label = ;
    int workUnits = ;

    int total = runJob(label, workUnits);

    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <string>

class ScopeLog {
private:
    std::string name;

public:
    ScopeLog(std::string label) {
        name = label;
        std::cout << "enter=" << name << std::endl;
    }

    ~ScopeLog() {
        std::cout << "leave=" << name << std::endl;
    }
};

int runJob(std::string label, int workUnits) {
    ScopeLog scope(label);
    int total = 0;
    for (int i = 1; i <= workUnits; i++) {
        total += i;
    }
    return total;
}

int main() {
    std::string label = ;
    int workUnits = ;

    int total = runJob(label, workUnits);

    std::cout << "total=" << total << std::endl;
    return 0;
}
#include <iostream>
#include <string>

class ScopeLog {
private:
    std::string name;

public:
    ScopeLog(std::string label) {
        name = label;
        std::cout << "enter=" << name << std::endl;
    }

    ~ScopeLog() {
        std::cout << "leave=" << name << std::endl;
    }
};

int runJob(std::string label, int workUnits) {
    ScopeLog scope(label);
    int total = 0;
    for (int i = 1; i <= workUnits; i++) {
        total += i;
    }
    return total;
}

int main() {
    std::string label = ;
    int workUnits = ;

    int total = runJob(label, workUnits);

    std::cout << "total=" << total << std::endl;
    return 0;
}
RAII RAII means resource acquisition is initialization: acquire in the constructor and release in the destructor.