C++ Constructor and Destructor
- TYPES OF CONSTRUCTOR
- CHARACTERISTICS OF A CONSTRUCTOR
- WHAT IS COPY CONSTRUCTOR
- DESTRUCTOR
In short, when an object is created in C++, a
specific process called a constructor is automatically called. It is usually
used to create data members for new things. In C++, the Name of a class or
structure also functions as a construct name. When the object is ready, the
constructor is called. Because it creates values or provides data to an
object, it is called a constructor.
Its prototype or you can say syntax looks like this: -
<class name> (list of parameters)
Syntax of constructor is used to define a
constructor outside of the class
<class name> :: <class name> (list of
parameters) { // constructor definition)}
It is of two types
1) Default
constructor
2) Parameterized
constructor
Default constructor
A constructor with no arguments is called a default
constructor. It is called when the object is created.
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2;
return 0;
}
Output
Default Constructor Invoked
Default Constructor Invoked
Parametrized constructor
A constructor that takes parameters is called a parameter constructor. It is used to assign different values to separate objects.
#include <iostream>
using namespace std;
class Employee {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
Employee e2=Employee(102, "Nakul", 59000);
e1.display();
e2.display();
return 0;
}
Output:
101 Sonoo
890000
102 Nakul
59000
- · A constructor has the same name as the class it belongs to.
- · While this is possible, constructors are usually declared in the class and public sections. However, this is not necessary.
- · Because constructors do not return values, they have no return type.
- · When we create a class object, the constructor is called immediately.
- · Overloaded constructors are possible.
- · Declaring a constructor virtual is not allowed.
- · Constructor cannot be inherited.
- · Builder addresses are not referable.
- · When allocating memory, the constructor calls implicit new and delete operators.
What is a copy constructor?
A class function known as a copy constructor
initializes an object using another object of the same class - a comprehensive discussion
of Copy Constructors.
Whenever we define one or more non-default
constructors (with parameters) for a class, we must also include a default
constructor (without parameters), since the compiler does not provide one in
this situation. It is a best practice to always declare a default constructor,
even if it is not required.
The copy constructor requires a reference to an
object of the same class.
Sample
(Sample &t)
{
id=t.id;
}
Destructor
The special member function corresponding to a
constructor is a destructor. A constructor creates class objects that are
destroyed by a destructor. The word "destroyer and quot" followed by a
tilde () symbol is the same as the class name. You can only assign one
destroyer at a time. One way to destroy an object created by a constructor is
to use a destructor. Therefore, fighters cannot be overloaded. Destructors do
not accept arguments and do not give or return anything. As soon as the item
wears out, it is immediately called. Destructors free memory used by objects
created by the constructor. A destructor reverses the process of creating
things by destroying them.
It is defined as
~ <class-name>()
{
}
Let us try with an example
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Constructor Invoked"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2; //creating an object of Employee
return 0;
}
Output
Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked
No comments:
Post a Comment