what are constructors in C++?
Hey Guys, I wanted know what are constructors in C++? Well, I have heard about this but I have no idea of its working. So, If anyone having any information or knowledge about this term than please let me know as soon as possible. I very keen to know this term as I want to know and execute a program on this term. Well, can anyone tell me what is the connection of object initialized and the constructor? If anybody who knows athe solution for this query then please let me know as soon as possible.
Re: what are constructors in C++?
When a object is created, the variable of the objects must be initialized. Initialization of variable of object is done automatically when the object is created .The object is initialized at the time of creation of it by means of a special function. This special function is known as constructor. It is called as constructor because it constructs the value of data member of class. You must know that constructor is created when an object is initialized.
Re: what are constructors in C++?
Characteristics of constructor
• The name of the constructor should be always on the name of class.
• Constructor will be called automatically whenever the object is created.
• They do not have any return value.
• They should be always declared in public section.
• They will be called only once for each object
• They cannot be inherited.
This are characteristics of a constructor where it follows all of the above points. When you define a class, you should know that an object should be initialized and it should be created. So, it is necessary to follow this points. You should also be aware that a constructor does not returns the value. This is what many of them are still not aware of it. One more important thing is that it should be always be created in public section not in private or protected section.
Re: what are constructors in C++?
Code:
For example
class Box
{
int l,w,h;
public:
Box() // constructor
{
l=10;
h=20;
w=30;
}
}
main()
{
Box b1;
}
This is an simple example of constructor which is shown as above. In this an object is initialized and how it is created is shown. You will find that the box named constructor is created in the program. The name of the constructor and the class should be similar which is also shown. After initialization the value of objects are also assigned in the program. This is the most simplest and easiest program of constructor in C++.
Re: what are constructors in C++?
When a object b1 of class Box is created the control will automatically transfer to the constructor and execute the statements written in side the constructor, which initializes the variable l, h, w of object b1;
The constructor mentioned above does not have arguments so such type of constructor is know as constructor without argument. If a class does not define any constructor then c++ creates a constructor when the object of class is created and initializes all variable of object to value 0.such kind of constructor is known as default constructor.