HackIndia Logo
HackathonsNewsSkillsGrowth
HackIndia LogoContact: +91 9910125804

General

  • Companies
  • Colleges
  • Board of Advisors
  • Brand Ambassadors
  • FAQ
  • Contact
  • Report a Bug

Hackathons

  • Upcoming Hackathons
  • Judges
  • Mentors
  • Partners
  • Participating Colleges

Resources

  • About Us
  • Branding
  • Latest News
  • Growth
  • Skills
  • Ecosystem
  • Forum
  • Ideas
  • Prompt Optimizer
  • How to earn XP

© 2026 HackIndia. All rights reserved.

Privacy PolicyTerms of ServiceCode of ConductSitemap

Build 2.139 · 09 Nov 2026, 10:59 UTC

    HackIndia
    HackathonsNewsInternshipsSkillsGrowth
    C++Working towardsC++
    0%

    0 of 10 steps done · +100 XP · +50 SHARP when it is yours

    1. 1Write C++ that does not break5 min · +10 XP · +5 SHARP
    2. 2Use references and pointers safely5 min · +10 XP · +5 SHARP
    3. 3Solve faster with STL4 min · +10 XP · +5 SHARP
    4. 4Handle strings and parsing5 min · +10 XP · +5 SHARP
    5. 5Design classes with RAII5 min · +10 XP · +5 SHARP
    6. 6Understand move semantics plainly5 min · +10 XP · +5 SHARP
    7. 7Compile, debug, and test locally4 min · +10 XP · +5 SHARP
    8. 8Ship your C++ repo5 min · +10 XP · +5 SHARP
    9. The check10 questions, 7 to pass · +10 SHARP
    10. Hand in the taskA project that uses the skill
    11. Skill and certificate+100 XP · +50 SHARP
    Lesson 5 of 8 5 min

    C++

    Design classes with RAII

    • +10 XP for this lesson
    • +100 XP when the skill is proved
    • 4 learning it right now
    In 30 seconds
    • Design a C++ class that owns a resource and frees it automatically.
    • Use constructors, destructors, and deleted copy to prevent leaks and double frees.
    Add one RAII-style class to your project and explain it in your README.
    Key point

    One ownership rule

    RAII means Resource Acquisition Is Initialization. Put the resource inside an object, acquire it in the constructor, and release it in the destructor.

    The resource can be a file, socket, database handle, mutex lock, heap buffer, or any thing that must be closed, unlocked, or freed.

    Why it matters

    In hackathon code, crashes often come from cleanup being skipped after an early return or error. RAII makes cleanup happen when the object goes out of scope.

    You already use RAII when you use std::string, std::vector, std::ifstream, and std::lock_guard. They clean themselves up without you calling free or close in many places.

    Example

    Small file wrapper

    This class owns a C FILE pointer. It opens the file in the constructor and closes it in the destructor.

    Copying is disabled because two objects closing the same file would be unsafe. Move semantics comes next lesson, so keep this simple for now.

    #include <cstdio>
    #include <stdexcept>
    #include <string>
    
    class FileReader {
        FILE* file;
    
    public:
        explicit FileReader(const std::string& path) : file(std::fopen(path.c_str(), "r")) {
            if (!file) {
                throw std::runtime_error("Could not open file: " + path);
            }
        }
    
        ~FileReader() {
            if (file) {
                std::fclose(file);
            }
        }
    
        FileReader(const FileReader&) = delete;
        FileReader& operator=(const FileReader&) = delete;
    
        int getChar() {
            return std::fgetc(file);
        }
    };
    
    int main() {
        FileReader input("sample.txt");
        int ch = input.getChar();
        return 0;
    }
    Your to-do

    Design the class

    Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.

    0 of 5 done

    Common mistake

    Common bad pattern

    Do not make users remember to call cleanup manually. Someone will return early during a contest fix and forget it.

    If your class has open(), close(), and no destructor doing cleanup, it is not protecting the rest of your code.

    void process() {
        FILE* f = std::fopen("data.txt", "r");
        if (!f) return;
    
        // Bug risk: any early return before fclose leaks the file handle.
        if (someCondition()) return;
    
        std::fclose(f);
    }
    Tip

    Prefer existing RAII

    Before writing your own RAII class, check if the standard library already has one. Use std::ifstream for files, std::vector for dynamic arrays, std::unique_ptr for single ownership, and std::lock_guard for locks.

    Write your own wrapper only when your project uses a C library, system handle, or custom resource that the STL does not already manage.

    README proof

    For your HackIndia repo, explain one class that manages ownership. Mention what it owns, where it acquires the resource, where it releases it, and why copying is disabled or safe.

    This is useful for internships because it shows you can write C++ that survives errors, not just pass sample test cases.

    Your to-do

    Do this now

    Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.

    0 of 5 done

    Quick check

    0 of 2 right
    1.Your class owns a C file handle. What should happen in its destructor?
    2.Why delete copy operations for a simple file-owning wrapper?

    Answer the quick check to finish this lesson.

    ← Lesson 4Skip to lesson 6 →
    C++
    Working towards
    C++ +100 XP

    0 of 8 lessons done · proved by a project that uses the skill

    See the track