---
title: "Department class constructor and destructor"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:dept-class-destructor-order"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/dept-class-destructor-order
site: BCT Engineering
---

# Department class constructor and destructor

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2068 Chaitra, 5 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/dept-class-destructor-order
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

Write a program that can store Department ID and Department name with constructor. Also write destructor in the same class and show that objects are destroyed in reverse order of creation with suitable message.

## Short answer

The Department class constructor initializes ID and name, printing a creation message. The destructor prints a destruction message. In main(), three objects are created; upon going out of scope, destructors run in reverse order: d3, d2, d1 are destroyed, confirming LIFO order.

## Answer

A **destructor** has the class name preceded by `~`, takes no arguments and has no return type. It is called automatically when an object goes out of scope. For automatic (local) objects, destructors run in the **reverse order** of construction, since the stack unwinds last-in-first-out.

```cpp
#include <iostream>
#include <cstring>
using namespace std;

class Department
{
    int id;
    char name[30];
public:
    Department(int i, const char *n)       // constructor
    {
        id = i;
        strcpy(name, n);
        cout << "Constructor: " << name << " (ID " << id << ") created\n";
    }
    void display()
    {
        cout << "ID: " << id << ", Name: " << name << endl;
    }
    ~Department()                          // destructor
    {
        cout << "Destructor: " << name << " (ID " << id << ") destroyed\n";
    }
};

int main()
{
    Department d1(1, "Computer");
    Department d2(2, "Civil");
    Department d3(3, "Electronics");
    d1.display();
    d2.display();
    d3.display();
    return 0;       // destructors run here
}
```

#### Output

```text
Constructor: Computer (ID 1) created
Constructor: Civil (ID 2) created
Constructor: Electronics (ID 3) created
ID: 1, Name: Computer
ID: 2, Name: Civil
ID: 3, Name: Electronics
Destructor: Electronics (ID 3) destroyed
Destructor: Civil (ID 2) destroyed
Destructor: Computer (ID 1) destroyed
```

The objects `d1, d2, d3` are created in that order but destroyed as `d3, d2, d1`, showing reverse order of destruction.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which describes a destructor?
   A. Class name with ~, no arguments, no return type
   B. Class name with arguments
   C. void ~name()
   D. Same as constructor
   Answer: A. A destructor is ~ClassName(), taking no arguments and having no return type. (Key point: Destructor syntax)

2. For d1, d2, d3 created in that order in main, in what order are they destroyed?
   A. d1, d2, d3
   B. d3, d2, d1
   C. d2, d1, d3
   D. Random
   Answer: B. Local objects are destroyed in reverse order of construction (last in, first out). (Key point: Destruction order)

3. Why is destruction reversed for automatic objects?
   A. The stack unwinds last-in-first-out
   B. Alphabetical order
   C. Compiler optimisation only
   D. Constructors run backwards
   Answer: A. Local objects live on the stack, which unwinds LIFO. (Key point: Why reverse)

4. When is the destructor of a local object called?
   A. When the object goes out of scope
   B. Only on delete
   C. At program start
   D. When copied
   Answer: A. It is called automatically when the object goes out of scope. (Key point: When called)

## Related in Object Oriented Programming

- [Constructor/destructor order; parameterized base constructor](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Actor-dtor-order-single-multiple) — Object Oriented Programming, chapter 6, asked 2×
- [Constructor and destructor order, multilevel inheritance](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Actor-order-multilevel) — Object Oriented Programming, chapter 6, asked 2×
- [Department store billing system with files](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abilling-system-file) — Object Oriented Programming, chapter 8
- [Time class: add two objects](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-class-add-objects) — Object Oriented Programming, chapter 1
- [Patient information store and retrieve](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apatient-file-program) — Object Oriented Programming, chapter 8
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7

## Related in other subjects

- [Department store 36 × 15 m LED panel lighting](https://www.bctengineer.com/question.md?key=semester_6%2Findustrial_power_distribution_and_illumination%3Adepartment-store-36x15-led) — Industrial Power Distribution and Illumination, chapter 7, asked 3×
- [Department store 30 × 15 m twin 55 W LED](https://www.bctengineer.com/question.md?key=semester_6%2Findustrial_power_distribution_and_illumination%3Adepartment-store-30x15-twin-55w) — Industrial Power Distribution and Illumination, chapter 7, asked 3×
- [Department store 30 × 15 m twin 40 W fluorescent](https://www.bctengineer.com/question.md?key=semester_6%2Findustrial_power_distribution_and_illumination%3Adepartment-store-twin-40w-fluorescent) — Industrial Power Distribution and Illumination, chapter 7, asked 3×
- [Student course department class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Astudent-course-diagram-type) — Object Oriented Analysis and Design, chapter 2
