Explain the constructors and destructors. Support your answer with examples.
Question
Explain the constructors and destructors. Support your answer with examples.
Solution 1
In object-oriented programming, constructors and destructors are special member functions of a class that are used to initialize and destroy objects respectively.
- Constructors: A constructor is a special member function of a class that is executed whenever we create new objects of that class. It has the same name as the class itself, has no return type, and is used to initialize the values of data members of the class.
Example in C++:
class Example {
public:
int a;
// Constructor
Example() {
a = 0;
}
};
int main() {
// Creating object of the class, this will call the constructor
Example obj;
cout << obj.a; // Outputs: 0
return 0;
}
In this example, Example() is a constructor which initializes the value of a to 0. When an object of the class Example is created, the constructor is called automatically.
- Destructors: A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the
deleteexpression is applied to a pointer to the object of that class. A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.
Example in C++:
class Example {
public:
// Constructor
Example() {
cout << "Constructor Called!" << endl;
}
// Destructor
~Example() {
cout << "Destructor Called!" << endl;
}
};
int main() {
Example obj;
return 0;
}
In this example, ~Example() is a destructor. It is called automatically when the object goes out of scope. The output of the above code will be:
Constructor Called!
Destructor Called!
This shows that the constructor is called when the object is created and the destructor is called when the object goes out of scope.
Solution 2
In object-oriented programming, constructors and destructors are special member functions of a class that are used to initialize and destroy objects respectively.
- Constructors: A constructor is a special member function of a class that is executed whenever we create new objects of that class. It helps to set initial values for the object attributes.
For example, consider a class 'Car'.
class Car {
public:
Car() { // This is the constructor
cout << "Car created" << endl;
}
};
int main() {
Car car1; // When we create an object of the class, the constructor is called
return 0;
}
In this example, when we create an object 'car1' of the class 'Car', the constructor is automatically called and the message "Car created" is printed.
- Destructors: A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class. A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters.
For example, consider the same class 'Car'.
class Car {
public:
Car() { // This is the constructor
cout << "Car created" << endl;
}
~Car() { // This is the destructor
cout << "Car destroyed" << endl;
}
};
int main() {
Car car1; // When we create an object of the class, the constructor is called
return 0; // When the object goes out of scope, the destructor is called
}
In this example, when the object 'car1' goes out of scope at the end of the main function, the destructor is automatically called and the message "Car destroyed" is printed.
Similar Questions
How is Constructor different from Destructor? List down any three differences
How many destructors can a class have?MultipleNoneOneIt depends on the number of constructors
Which of the following is the correct definition of a destructor for the class Person?Select one:a.~Person();b.delete Person();c.destroy Person();d.~Person(int x, int y);
Select the INCORRECT statement about 'destructors' in C#?A class can have one destructor onlyDestructors cannot be inherited or overloadedDestructors can have modifiers or parametersAll of above mentioned
2. Which of the following gets called when an object is being created?A. ConstructorB. Virtual FunctionC. DestructorsD. Main
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.