In: Computer Science
1) What is the member initializer list used for? Give a code example of using one
2) Give two examples of when the copy constructor is called by the “compiler”.
3) What actions should be done in the destructor function? Give an example function header for the destructor for the Jedi class. When is the destructor called?
Thank you!
1). Member initializer list: Every class has its member variables. Most case the member variable are initialized in the constructor. Member initializer list help initialize member variables using the none defaulte methods. They are used for initializing non-static const data members, reference members and member objects.
Example in none static constantan data initialization
#include<iostream>
using namespace std;
class MemberInitList{
const int num;
public:
MemberInitList(int num): num(num){//Initializer list used
}
/**
* Other codes follows here
*/
}
2).
i. When an object is initialize from another object of the same type
ii. When passing an object by value as an argument to a function parameter.
3).
The fuctions of destructor function is to destroy an object , this is done by freeing the memory and other rersources that an object might have been using.
Example function header:
/**
* Header function
~Jedi(){
//Some code to be executed whe destructor is called
}
When destructor is called:
When an object goes out of scope.
Explicit dealocation of an object using the delete function.
End of the lifetime of a Temporary object.
_______________________________
Comment Down For Any Queries.
Please Give a Thumbs Up If You are satisfied With The Answer.