---
title: "Join two strings with dynamic constructor"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:join-strings-dynamic-constructor"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/join-strings-dynamic-constructor
site: BCT Engineering
---

# Join two strings with dynamic constructor

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2070 Chaitra, 7 marks
- Video: [Dynamic Constructor in C++ | Syntax & Dynamic Constructors in C++ Programming with Example](https://www.youtube.com/watch?v=fBoioONMHw8) — WsCube Tech
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/join-strings-dynamic-constructor
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

Write a C++ program to join two strings using dynamic constructor concept.

## Answer

A **dynamic constructor** is a constructor that allocates memory at run time with the `new` operator. It is useful when the size of the data is not known beforehand, such as the length of a string. The memory is released in the destructor with `delete`.

To join two strings, we use a constructor that takes two `String` objects, allocates `len1 + len2 + 1` bytes, and copies both strings into it.

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

class String {
    char *str;
    int len;
public:
    String() { len = 0; str = new char[1]; str[0] = '\0'; }

    // dynamic constructor: allocates memory for the given text
    String(const char *s) {
        len = strlen(s);
        str = new char[len + 1];
        strcpy(str, s);
    }

    // dynamic constructor that joins two strings
    String(String &a, String &b) {
        len = a.len + b.len;
        str = new char[len + 1];      // memory sized at run time
        strcpy(str, a.str);
        strcat(str, b.str);
    }

    void show() { cout << str << "  (length " << len << ")" << endl; }

    ~String() { delete[] str; }       // free memory
};

int main() {
    String s1("Object ");
    String s2("Oriented");
    String s3(s1, s2);                // join s1 and s2

    s1.show();
    s2.show();
    s3.show();
    return 0;
}
```

**Output:**

```text
Object   (length 7)
Oriented  (length 8)
Object Oriented  (length 15)
```

**Working:** `new char[len + 1]` reserves exactly enough space (one extra byte for `'\0'`). `strcpy` copies the first string and `strcat` appends the second. The destructor frees each object's memory when `main()` ends.

Note: if objects are copied with the default copy constructor, two objects would share one pointer. For real use, add a user-defined copy constructor and assignment operator.

## Check yourself

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

1. What makes a constructor dynamic?
   A. It allocates memory with new at run time
   B. It is static
   C. It uses malloc only
   D. It has no arguments
   Answer: A. It uses new to allocate memory whose size is known only at run time. (Key point: Dynamic constructor)

2. How many bytes are allocated to join two strings?
   A. len1 + len2
   B. len1 + len2 + 1
   C. len1 * len2
   D. 2 * (len1 + len2)
   Answer: B. One extra byte holds the '\0' terminator. (Key point: Allocation size)

3. How are the strings joined?
   A. strcpy first string then strcat second
   B. strcmp both
   C. Add pointers
   D. strlen only
   Answer: A. strcpy copies the first, strcat appends the second. (Key point: Joining)

4. What does ~String() do?
   A. delete[] str frees the memory
   B. Prints the string
   C. Copies string
   D. Nothing
   Answer: A. The destructor releases the dynamically allocated memory. (Key point: Destructor role)

## Related in Object Oriented Programming

- [Simple program illustrating OOP concept](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-simple-program) — Object Oriented Programming, chapter 1
- [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 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
- [Dynamic memory allocation with new and delete](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adynamic-memory-allocation) — Object Oriented Programming, chapter 3, asked 2×
- [Add objects of two different classes](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-add-two-class-objects) — Object Oriented Programming, chapter 4, asked 2×

## Related in other subjects

- [Compare two strings with function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-compare-function) — Computer Programming, chapter 7
- [Stored program concept and processing cycle](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Astored-program-processing-cycle) — Microprocessor, chapter 1, asked 6×
- [C program: sum of two 8-bit BCD numbers](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3A8051-bcd-sum-c) — Embedded System, chapter 9
- [Dynamic balancing with two correction planes](https://www.bctengineer.com/question.md?key=semester_7%2Ftheory_of_mechanism_and_machine_ii%3Abalancing-masses-two-planes-numerical) — Theory of Mechanism and Machine II, chapter 5
