LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 22 of 27

Smart Pointers

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Programs & Examples

Smart Pointers · Study material

A file-system tree owned with unique_ptr

Each node owns its children through unique_ptr, while the parent link is a plain pointer (an observer). Destroying the root frees the entire tree with no explicit delete.

#include <iostream>
#include <memory>
#include <string>
#include <vector>

// Children are owned with unique_ptr; the parent link is a plain observer pointer
struct Node {
    std::string name;
    Node* parent = nullptr;
    std::vector<std::unique_ptr<Node>> children;
    explicit Node(std::string n) : name(std::move(n)) {}
    Node* add(std::string n) {
        children.push_back(std::make_unique<Node>(std::move(n)));
        children.back()->parent = this;
        return children.back().get();
    }
    std::string path() const { return parent ? parent->path() + "/" + name : name; }
    int count() const { int c = 1; for (const auto& ch : children) c += ch->count(); return c; }
};

int main() {
    Node root("home");
    Node* asha = root.add("asha");
    Node* docs = asha->add("docs");
    docs->add("cv.pdf");
    asha->add("music");
    root.add("ravi")->add("notes.txt");
    std::cout << docs->children[0]->path() << '\n' << root.count() << " nodes\n";
    return 0;                       // destroying root frees the whole tree automatically
}

Output

home/asha/docs/cv.pdf
7 nodes

Breaking a team–employee cycle with weak_ptr

The cycle example shows how one weak link lets both objects be freed when the last outside owner goes away.

Keeping a session alive in a callback

The enable_shared_from_this example extends an object's lifetime until a pending callback has run.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links