Constructor in C++ is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. The constructor in C++ has the same name as the class or structure. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object which is why it is known as constructors. Show
Constructor does not have a return value, hence they do not have a return type. The prototype of Constructors is as follows: <class-name> (list-of-parameters); Constructors can be defined inside or outside the class declaration:- The syntax for defining the constructor within the class: <class-name> (list-of-parameters) { // constructor definition } The syntax for defining the constructor outside the class: <class-name>: :<class-name> (list-of-parameters){ // constructor definition} Example C++
Output Enter the RollNo:Enter the Name:Enter the Fee: 0 6.95303e-310 Example C++
Output: Enter the RollNo: 30 Enter the Name: ram Enter the Fee: 20000 30 ram 20000 How constructors are different from a normal member function? C++
A constructor is different from normal functions in following ways:
Let us understand the types of constructors in C++ by taking a real-world example. Suppose you went to a shop to buy a marker. When you want to buy a marker, what are the options. The first one you go to a shop and say give me a marker. So just saying give me a marker mean that you did not set which brand name and which color, you didn’t mention anything just say you want a marker. So when we said just I want a marker so whatever the frequently sold marker is there in the market or in his shop he will simply hand over that. And this is what a default constructor is! The second method is you go to a shop and say I want a marker a red in color and XYZ brand. So you are mentioning this and he will give you that marker. So in this case you have given the parameters. And this is what a parameterized constructor is! Then the third one you go to a shop and say I want a marker like this(a physical marker on your hand). So the shopkeeper will see that marker. Okay, and he will give a new marker for you. So copy of that marker. And that’s what a copy constructor is! Characteristics of the constructor:
Types of Constructors1. Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters. It is also called a zero-argument constructor.
CPP
C++
2. Parameterized Constructors: It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.
CPP
Output p1.x = 10, p1.y = 15 C++
When an object is declared in a parameterized constructor, the initial values have to be passed as arguments to the constructor function. The normal way of object declaration may not work. The constructors can be called explicitly or implicitly. Example e = Example(0, 50); // Explicit call Example e(0, 50); // Implicit call
3. Copy Constructor:A copy constructor is a member function that initializes an object using another object of the same class. A detailed article on Copy Constructor. Whenever we define one or more non-default constructors( with parameters ) for a class, a default constructor( without parameters ) should also be explicitly defined as the compiler will not provide a default constructor in this case. However, it is not necessary but it’s considered to be the best practice to always define a default constructor. Copy constructor takes a reference to an object of the same class as an argument. Sample(Sample &t) { id=t.id; } CPP
Output: Error: point (double px, double py): expects 2 arguments, 0 provided C++
C++
C++
C++
Destructor:A destructor is also a special member function as a constructor. Destructor destroys the class objects created by the constructor. Destructor has the same name as their class name preceded by a tilde (~) symbol. It is not possible to define more than one destructor. The destructor is only one way to destroy the object created by the constructor. Hence destructor can-not be overloaded. Destructor neither requires any argument nor returns any value. It is automatically called when the object goes out of scope. Destructors release memory space occupied by the objects created by the constructor. In destructor, objects are destroyed in the reverse of object creation. The syntax for defining the destructor within the class ~ <class-name>() { } The syntax for defining the destructor outside the class <class-name>: : ~ <class-name>(){} C++
Output Constructor executed Destructor executed C++
Output Constructor executed Constructor executed Constructor executed Constructor executed Destructor executed Destructor executed Destructor executed Destructor executed C++
Output No. of Object created: 1 No. of Object created: 2 No. of Object created: 3 No. of Object created: 4 No. of Object destroyed: 4 No. of Object destroyed: 3 No. of Object destroyed: 2 No. of Object destroyed: 1 Characteristics of a destructor:-1. Destructor is invoked automatically by the compiler when its corresponding constructor goes out of scope and releases the memory space that is no longer required by the program.
Related Articles :
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above What is the purpose of a default constructor?A default constructor in Java is created by the compiler itself when the programmer doesn't create any constructor. The purpose of the default constructor is to initialize the attributes of the object with their default values.
What does a default constructor do when does a class have a default constructor?The default constructor in Java initializes the data members of the class to their default values such as 0 for int, 0.0 for double etc. This constructor is implemented by default by the Java compiler if there is no explicit constructor implemented by the user for the class.
What is the default constructor in a C++ class used for?Default Constructors in C++
They are primarily useful for providing initial values for variables of the class. The two main types of constructors are default constructors and parameterized constructors. Default constructors do not take any parameters.
What is the meaning of default constructor in Java?What is a default constructor? A default constructor is a constructor created by the compiler if we do not define any constructor(s) for a class. Here is an example: public class Student { String firstName; String lastName; int age; public static void main(String args[]) { Student myStudent = new Student(); myStudent.
|