---
title: "Constructors and destructors with syntax"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:constructor-destructor-define"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/constructor-destructor-define
site: BCT Engineering
---

# Constructors and destructors with syntax

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2069 Chaitra, 2+2 marks
- Video: [Constructor Basics | C++ Tutorial](https://www.youtube.com/watch?v=bnyveJ17lao) — Portfolio Courses
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/constructor-destructor-define
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

Define constructor and destructor. Write down different types of constructors with syntax.

## Short answer

A constructor is a special member function that initializes object data members; it has the same name as the class and no return type. A destructor (~ClassName) automatically releases resources. Types: default (no parameters), parameterized (with arguments), copy (from existing object via reference), and dynamic (allocates memory with new).

## Answer

A **constructor** is a special member function that has the same name as its class, has no return type, and is called automatically when an object is created. It initialises the data members of the object. A **destructor** is a special member function with the class name preceded by a tilde (`~`). It is called automatically when an object goes out of scope or is deleted, and it releases resources (such as memory) the object holds. A destructor takes no arguments and cannot be overloaded.

#### Types of constructors

**1. Default constructor** - takes no arguments (or all arguments have default values).

```cpp
class Box {
    int l, b;
public:
    Box() { l = 0; b = 0; }          // default constructor
};
Box b1;                              // calls Box()
```

**2. Parameterized constructor** - takes arguments so each object can be initialised with different values.

```cpp
class Box {
    int l, b;
public:
    Box(int x, int y) { l = x; b = y; }   // parameterized
};
Box b2(4, 5);        // implicit call
Box b3 = Box(6, 7);  // explicit call
```

**3. Copy constructor** - creates a new object as a copy of an existing object of the same class. The argument must be passed by reference.

```cpp
class Box {
    int l, b;
public:
    Box(const Box &o) { l = o.l; b = o.b; }   // copy constructor
};
Box b4(b2);      // or Box b4 = b2;
```

**4. Constructor with default arguments** (variant of the above): `Box(int x = 1, int y = 1);`

**5. Dynamic constructor** - allocates memory with `new` inside the constructor.

Syntax of the destructor:

```cpp
~Box() { /* clean-up code, e.g. delete[] p; */ }
```

General syntax: `ClassName(parameter_list) { body }` for constructors and `~ClassName() { body }` for the destructor.

## Check yourself

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

1. Which is true of a destructor?
   A. Takes arguments and can be overloaded
   B. Has ~ before class name, takes no arguments, cannot be overloaded
   C. Has return type void
   D. Called manually only
   Answer: B. A destructor is ~ClassName(), no arguments, so it can't be overloaded. (Key point: Destructor)

2. What is a default constructor?
   A. Takes no arguments (or all defaulted)
   B. Takes two objects
   C. Copies an object
   D. Allocates with malloc
   Answer: A. It takes no arguments or has defaults for all. (Key point: Default constructor)

3. How must the argument of a copy constructor be passed?
   A. By value
   B. By reference
   C. By pointer only
   D. Not at all
   Answer: B. Copy constructors take the argument by reference. (Key point: Copy constructor argument)

4. What is a dynamic constructor?
   A. One that allocates memory with new inside the constructor
   B. One that is virtual
   C. One without a body
   D. One that is static
   Answer: A. A dynamic constructor allocates memory at run time with new. (Key point: Dynamic constructor)

## Related in Object Oriented Programming

- [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×
- [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×
- [Department class constructor and destructor](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adept-class-destructor-order) — Object Oriented Programming, chapter 4
- [Constructors: need and types](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aconstructor-types) — Object Oriented Programming, chapter 4, asked 2×
- [Syntax of operator overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoperator-overloading-syntax) — Object Oriented Programming, chapter 5, asked 4×
- [Class and function template with syntax](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-function-template-syntax) — Object Oriented Programming, chapter 9, asked 4×
