OnlineCBT Logo OnlineCBT
C++ · LESSON 9

Constructors and Destructors

By Ratan Updated: 15 Oct 2026

Initialize objects with default, parameterized, copy constructors, and clean memory with destructors (~Class).

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Tie initialization and cleanup to object lifetime

A constructor establishes an object's initial valid state. A destructor runs when the object's lifetime ends and releases resources owned by that object. Local objects are normally destroyed in reverse order of completed construction.

Trace construction and destruction

#include 
#include 
#include 
class Lesson {
    std::string name_;
public:
    explicit Lesson(std::string name) : name_(std::move(name)) {
        std::cout << "Start " << name_ << '\n';
    }
    ~Lesson() {
        std::cout << "End " << name_ << '\n';
    }
};
int main() {
    Lesson first{"Basics"};
    { Lesson second{"Practice"}; }
    std::cout << "Back in main\n";
}

Expected sequence: Start Basics, Start Practice, End Practice, Back in main, End Basics. Braces introduce a scope. The inner object is destroyed when that scope ends, not at some later garbage-collection event.

Default, copy and move construction

A default constructor can be called without arguments. Copy construction creates a new object from an existing object; copy assignment updates an already-existing object. Move construction can transfer resources from an expiring or explicitly moved-from object. std::move itself is a cast enabling move-aware overload selection; it does not perform a transfer on its own.

Prefer the rule of zero

If members such as std::string, std::vector and smart pointers already manage all resources, your class often needs no custom destructor, copy or move operations. The compiler-generated operations follow the members' capabilities. Writing a destructor just because a class exists can unnecessarily affect generated move operations.

A raw owning pointer with a default shallow copy can cause two objects to attempt to free the same allocation. Prefer a container or unique_ptr, or explicitly design the full ownership behavior. A moved-from standard-library object remains valid, but its value may be unspecified unless the interface promises more.

Construction failure

If construction throws, already-constructed members and bases are destroyed. The incomplete object's own destructor does not run. This is one reason to store resources in RAII members rather than acquiring them into unmanaged raw pointers partway through the constructor.

Exercises

  1. Create three tracing objects in nested scopes and predict the exact destruction order.
  2. Remove the custom destructor from a class containing only a vector. Explain why the vector still releases its memory.
  3. Identify the difference between Lesson b = a; and b = a; after b already exists.
  4. Explain why a destructor should not allow exceptions to escape during stack unwinding.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
पता संपर्क करें

+91 7877547686

ई-मेल

onlinecbtportal@gmail.com

हेल्पलाइन नंबर

+91 7877547686


डाउनलोड करने के लिए क्लिक करें
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat