In: Computer Science
When you create a C++ class, you are given 4 default methods. What are they and why is it important that you remember that these exist?
class Sample {
// default constructor
Sample::Sample() {
}
// copy constructor
Sample::Sample(Sample const& rhs) {
}
// destructor
Sample::~Sample() {
}
// assignment operator
Sample& operator=(Sample const& rhs) { return *this; }
};
Default
constructor:
This is a parameterless constructor and has empty
body. If no user defined constructors are declared then by deafult
this is used.
Copy
Constructor:
copy an object to pass it as an argument to a method.
Constructor which creates a object by initializing it with an
object of same class.
Destructor:
It Provides a destructor equivalent to user defined
destructor with a empty body. It is Declared only if no user
defined destructor is declared. It is defined only if used.
Assignment
operator:
Provides the default Assignment operator (=), which
copies the class instance. It is Declared when no user defined
assignment operator is declared. It is defined only if used.