|
| 1 | +Constructors and Destructors in C++ |
| 2 | + |
| 3 | +In C++, constructors and destructors are special member functions of a class that help manage the lifecycle of objects. They automatically handle initialization and cleanup without the programmer having to call them explicitly. |
| 4 | + |
| 5 | +1. Constructor |
| 6 | + |
| 7 | +Purpose: |
| 8 | +A constructor is used to initialize an object when it is created. It sets up the initial state of the object, allocates resources if necessary, and can take arguments to customize the initialization. |
| 9 | + |
| 10 | +Key Points: |
| 11 | + |
| 12 | +Name of the constructor matches the class name. |
| 13 | + |
| 14 | +No return type (not even void). |
| 15 | + |
| 16 | +Automatically called when an object is created. |
| 17 | + |
| 18 | +Can be overloaded (multiple constructors with different parameters). |
| 19 | + |
| 20 | +Example Use Case: |
| 21 | +Imagine you have a BankAccount class. When a new account is created, you might want to initialize the balance and account holder’s name. |
| 22 | + |
| 23 | +#include <iostream> |
| 24 | +using namespace std; |
| 25 | + |
| 26 | +class BankAccount { |
| 27 | + string name; |
| 28 | + double balance; |
| 29 | +public: |
| 30 | + // Constructor |
| 31 | + BankAccount(string n, double b) { |
| 32 | + name = n; |
| 33 | + balance = b; |
| 34 | + cout << "Account for " << name << " created with balance $" << balance << endl; |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +int main() { |
| 39 | + BankAccount acc1("Alice", 1000.0); // Constructor is called here |
| 40 | + return 0; |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +Output: |
| 45 | + |
| 46 | +Account for Alice created with balance $1000 |
| 47 | + |
| 48 | +2. Destructor |
| 49 | + |
| 50 | +Purpose: |
| 51 | +A destructor is used to clean up when an object is destroyed. This includes: |
| 52 | + |
| 53 | +Releasing dynamically allocated memory (new / delete) |
| 54 | + |
| 55 | +Closing files |
| 56 | + |
| 57 | +Releasing other system resources |
| 58 | + |
| 59 | +Key Points: |
| 60 | + |
| 61 | +Name of destructor is class name prefixed with ~ (tilde symbol). |
| 62 | + |
| 63 | +No return type, takes no parameters. |
| 64 | + |
| 65 | +Automatically called when an object goes out of scope or is explicitly deleted. |
| 66 | + |
| 67 | +Only one destructor per class (cannot be overloaded). |
| 68 | + |
| 69 | +Example Use Case: |
| 70 | +If a class allocates dynamic memory for some reason, failing to release it will cause memory leaks. A destructor ensures cleanup. |
| 71 | + |
| 72 | +#include <iostream> |
| 73 | +using namespace std; |
| 74 | + |
| 75 | +class Demo { |
| 76 | + int* data; |
| 77 | +public: |
| 78 | + // Constructor |
| 79 | + Demo(int size) { |
| 80 | + data = new int[size]; // allocate memory |
| 81 | + cout << "Memory allocated for " << size << " integers." << endl; |
| 82 | + } |
| 83 | + |
| 84 | + // Destructor |
| 85 | + ~Demo() { |
| 86 | + delete[] data; // free memory |
| 87 | + cout << "Memory freed." << endl; |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +int main() { |
| 92 | + Demo obj(5); // Constructor called |
| 93 | + // do some operations |
| 94 | +} // Destructor called automatically here |
| 95 | + |
| 96 | + |
| 97 | +Output: |
| 98 | + |
| 99 | +Memory allocated for 5 integers. |
| 100 | +Memory freed. |
| 101 | + |
| 102 | +3. Differences Between Constructor and Destructor |
| 103 | +Feature Constructor Destructor |
| 104 | +Purpose Initialize object Clean up object |
| 105 | +Name Same as class name Same as class name with ~ |
| 106 | +Return Type None None |
| 107 | +Parameters Can have parameters (overloaded) Cannot have parameters |
| 108 | +Called When object is created When object goes out of scope or is deleted |
| 109 | +Number per class Can have multiple (overloaded) Only one |
| 110 | +4. Example Scenario Where Destructor is Essential |
| 111 | + |
| 112 | +Suppose a class opens a file for writing. You must close the file when the object is destroyed. If you forget, you might lose data or lock the file. |
| 113 | + |
| 114 | +#include <iostream> |
| 115 | +#include <fstream> |
| 116 | +using namespace std; |
| 117 | + |
| 118 | +class FileWriter { |
| 119 | + ofstream file; |
| 120 | +public: |
| 121 | + FileWriter(string filename) { |
| 122 | + file.open(filename); |
| 123 | + cout << "File opened for writing.\n"; |
| 124 | + } |
| 125 | + |
| 126 | + void writeData(string data) { |
| 127 | + file << data << endl; |
| 128 | + } |
| 129 | + |
| 130 | + ~FileWriter() { |
| 131 | + file.close(); // ensures the file is closed when object goes out of scope |
| 132 | + cout << "File closed.\n"; |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +int main() { |
| 137 | + { |
| 138 | + FileWriter fw("output.txt"); |
| 139 | + fw.writeData("Hello, World!"); |
| 140 | + } // Destructor called here, file is safely closed |
| 141 | +} |
| 142 | + |
| 143 | +✅ Summary |
| 144 | + |
| 145 | +Constructor: Sets up the object → called when object is created. |
| 146 | + |
| 147 | +Destructor: Cleans up resources → called when object is destroyed. |
| 148 | + |
| 149 | +Destructors are essential whenever a class manages dynamic memory, files, or other resources, ensuring no leaks or dangling handles. |
0 commit comments