Constructor and Destructor in C++ are the special member functions of the class and they are essential components of Object Oriented Programming. Constructors are special functions within a class that initialize the object when it is created, providing default or custom values to its attributes. On the other hand, destructors are used to clean up resources allocated by the object during its lifetime, and they are automatically invoked when the object goes out of scope or is explicitly deleted.
What is a Constructor in C++?
A constructor in C++ is a special member function of a class that is automatically invoked when an object of that class is created. Its primary purpose is to initialize the object's attributes or perform any necessary setup. Constructors have the same name as the class and do not have a return type.
There are many types of constructors available in C++. When an object is instantiated, the appropriate constructor is automatically called, ensuring that the object is properly initialized before it is used. Constructors play a crucial role in object-oriented programming by establishing the initial state of objects.
How many types of constructors in C++?
There are basically four types of constructors present in C++ and these are:
- Default Constructor.
- Parameterized Constructor.
- Copy Constructor.
- Dynamic Constructor.
Default Constructor.
class ClassName { public: ClassName() { // Constructor code } };
// C++ code implementation for default constructor #include <iostream> using namespace std; class MyClass { private: int rollNo; string name; public: // Default Constructor MyClass() { std::cout << "Default Constructor Called" << std::endl; rollNo = 0; name = "NULL"; } void display(){ cout << "Student Name: " << name << endl; cout << "Roll No: " << rollNo << endl; } }; int main() { // Creating an object invokes the default constructor MyClass obj; obj.display(); return 0; }
Default Constructor Called
Student Name: NULL
Roll No: 0
Note: It's important to explicitly define a default constructor when custom initialization is required or when other constructors are defined in the class. (alert-passed)
Parameterized Constructor.
class ClassName { public: ClassName(type1 param1, type2 param2, ...) { // Constructor code } };
// C++ code implementation for Parameterized constructor #include <iostream> using namespace std; class Car { private: string brand; int year; public: // Parameterized Constructor Car(string b, int y) { cout << "Parameterized Constructor Called" << endl; brand = b; year = y; } void display() { cout << "Brand: " << brand << ", Year: " << year << endl; } }; int main() { // Creating an object using the parameterized constructor Car myCar("Toyota", 2022); myCar.display(); return 0; }
Parameterized Constructor Called Brand: Toyota, Year: 2022
Copy Constructor.
Note: If a copy constructor is not explicitly defined, the compiler generates a default copy constructor. (alert-success)
class ClassName { public: // Copy Constructor ClassName(const ClassName& obj) { // Constructor code } };
// C++ code implementation for Copy constructor #include <iostream> using namespace std; class Student { private: string name; int age; public: // Parameterized Constructor Student(string n, int a) : name(n), age(a) { cout << "Parameterized Constructor Called" << endl; } // Copy Constructor Student(const Student& obj) : name(obj.name), age(obj.age) { cout << "Copy Constructor Called" << endl; } void display() { cout << "Name: " << name << ", Age: " << age << endl; } }; int main() { // Creating an object using the parameterized constructor Student originalStudent("John", 20); originalStudent.display(); // Creating a new object using the copy constructor Student copiedStudent = originalStudent; copiedStudent.display(); return 0; }
Name: John, Age: 20
Copy Constructor Called
Name: John, Age: 20
Dynamic Constructor.
What is a Destructor in C++?
class ClassName { public: // Constructor(s) ClassName(); // Default constructor ClassName(parameters); // Parameterized constructor // Destructor ~ClassName(); };
- A destructor has the same name as the class but is prefixed with a tilde (~).
- Unlike constructors, which can be overloaded, a class can have only one destructor.
- The destructor is automatically invoked when an object goes out of scope or is explicitly deleted.
- It is used for releasing resources, closing files, releasing memory, or performing any cleanup necessary before the object is destroyed.
// C++ code implementation for Destructor #include <iostream> using namespace std; class MyClass { public: // Constructor MyClass() { std::cout << "Constructor Called" << std::endl; } // Destructor ~MyClass() { std::cout << "Destructor Called" << std::endl; } }; int main() { // Creating an object MyClass obj; // Object goes out of scope, destructor is called return 0; }
Constructor Called Destructor Called
Note: In the example, the destructor prints a message for demonstration purposes. In real-world scenarios, destructors are often used to deallocate memory, close files, or release other resources.(alert-passed)
(getButton) #text=(Access Specifiers in C++) #icon=(link) #color=(#2339bd)
No comments:
Post a Comment