Explain the role of constructor and destructor in a class. Write the appropriate syntax for the copy constructor and parameterized constructors.
Q.) Explain the role of constructor and destructor in a class. Write the appropriate syntax for the copy constructor and parameterized constructors.
Subject: Object Oriented Programming and MethodologyConstructor:
- Definition: A constructor is a special member function of a class that is executed whenever we create new objects of that class.
- Role:
- Initializes the data members of the class.
- Allocates memory for the object.
- Performs other necessary operations for object creation.
Syntax:
class MyClass { public: MyClass(); // Default constructor MyClass(int x); // Parameterized constructor // Other members... };
Destructor:
- Definition: A destructor is a special member function of a class that is executed whenever an object of that class is destroyed.
- Role:
- Frees the memory allocated for the object.
- Performs other necessary operations for object destruction.
Syntax:
class MyClass { public: ~MyClass(); // Destructor // Other members... };
Copy Constructor:
- Definition: A copy constructor is a special member function that is used to create a new object of a class by copying values from an existing object of the same class.
- Role:
- Creates a new object that is a copy of an existing object.
- Initializes the data members of the new object with the values of the corresponding data members of the existing object.
Syntax:
class MyClass { public: MyClass(); // Default constructor MyClass(const MyClass& other); // Copy constructor // Other members... };
Parameterized Constructor:
- Definition: A parameterized constructor is a special member function that is used to create a new object of a class by passing values to its constructor.
- Role:
- Creates a new object and initializes its data members with the specified values.
Syntax:
class MyClass { public: MyClass(); // Default constructor MyClass(int x); // Parameterized constructor // Other members... };
Consider the following example:
class Person {
public:
Person() { // Default constructor
name = "John Doe";
age = 20;
}
Person(string name, int age) { // Parameterized constructor
this->name = name;
this->age = age;
}
~Person() { // Destructor
cout << "Person object destroyed" << endl;
}
// Other member functions...
private:
string name;
int age;
};
In this example, the Person
class has a default constructor, a parameterized constructor, and a destructor.
- The default constructor initializes the
name
andage
data members to default values. - The parameterized constructor takes two arguments,
name
andage
, and initializes the corresponding data members of the newly created object with these values. - The destructor prints a message to the console when an object of the
Person
class is destroyed.
When we create a Person
object using the default constructor, the following code will be executed:
Person person1; // Default constructor is called
This will create a Person
object with the name "John Doe" and age 20.
When we create a Person
object using the parameterized constructor, the following code will be executed:
Person person2("Jane Doe", 30); // Parameterized constructor is called
This will create a Person
object with the name "Jane Doe" and age 30.
When we destroy a Person
object, the following code will be executed:
person1.~Person(); // Destructor is called
This will print the message "Person object destroyed" to the console.