- What is a non-default (or “alternative”) constructor?
- If we define a non-default constructor, will C++ generate an implicitly defined default constructor?
- What is
thisand when do we use thethiskeyword? - Trace the output of the following code and explain which constructors are called at each step and why?
#include <iostream>
class Student {
public:
Student() {
std::cout << "Default constructor called" << std::endl;
}
Student(int id) {
std::cout << "Non-default constructor called for ID " << id << std::endl;
}
};
int main() {
Student s1(10);
Student* s2 = new Student;
Student* s3 = new Student(20);
delete s2;
delete s3;
return 0;
}
5. What is a destructor?
6. A destructor will automatically release memories that are allocated in the constructor- true or false?