In this page, we will learn C++ constructor with their following concept,
constructor
in simple language, the constructor is a function member which is used to initialize a class-variable.
The constructor function is similar to the class name, which automatically calls itself when the class object is declared.
Rule for declaring a constructor in C++
- The constructor is declared in the public section of the class whereas definition can define both inside or outside class (using the resolution operator like other normal members).
- The constructor member name is the same as the class name. Which is a member of this.
- As soon as the object of that class is created, whose member it is, it automatically gets called.
- They do not return any value. Although it is not a return type, it is also not a void type function.
- Constructor member cannot be inherited in C++, although they can be called from the derive class.
- Because the constructor is a function, so it can be overloaded.
before starting more let’s try to understand why we use constructor in C++?
why we use constructor in C++?
Class is a user defined data type but its behaviour like a Built int data type. meaning,
Built in data type.
int x,y,z;
User-defined data type
class-name x,y,z;
as we know, int is the keyword whereas class-name is an identifier. But in both we can declare variables where in int, declare x, y, z in normal variables and class, declare x, y, z class-variables or objects. both are called identifier.
let’s initialization them,
As we see above, int is a data type while class-name is an identifier. In both we can do variable declaration.
Their initialization will be as follows –
int x = 5; int y = 2; int z =4;
class variable
class-name x,y,z;
x.member(5);
y.member(2);
z.member(4);
will be like this.
you can see the difference between the two wherein the built in data type, the value (5,2,4) is assigned while the variable is declared. meaning no separate statement is written, whereas in class it does not.
In class, class-variable (x, y, z) is declared first, then the value is assigned using the class member as seen above.
in class, first the class-variable (x, y, z) is declared, then the value is assigned through the class member, then the class-object is initialized
You can understand this from the example below-
class constructor
{
int a,b;
public:
void constructor(int x, int y)
{
a = x;
b = y;
}
};
constructor obj(3,4); // direct initialization
in constructor, if no value is assigned, the data-members of the class will be initialized automatically, whose value can be zero or garbage, just like built-in-data type.
Type of constructor in C++
- Default constructor
- Parameterized constructor
- Copy constructor
Default constructor in c++
A constructor which does not pass any arguments, which means without arguments, is called the default constructor.
The syntax of a default constructor is given below, where construct() is a default constructor in the program –
syntax
class construct
{
public:
construct()
{
body of constructor member;
}
};
The program of Default constructor is given below-
In the program below construct() is a default constructor. The object of the class construct will be automatically called in execution as soon as it is created.
#include<iostream>
using namespace std;
class construct
{
public:
construct(void); // constructor declaration in public mode
};
construct::construct(void) // constructor definition outside class
{
cout<<"constructor called:\n";
}
void main()
{
clrscr();
construct x; // object x declare
getch();
}
OUTPUT
constructor called:
Every time a new object of a class is created, the same time the constructor function will automatically call it, means object will initialize automatically.
For Example-
construct x, y, z;
Then,
constructor called
constructor called
constructor called
a default constructor makes a class visible in the program because as soon as the class object is created, it is automatically called, no separate function member (default constructor) has to be called,
whereas after creating an object in a class without a default constructor, we also have to write the calling statement of its members.
The compiler automatically provides a constructor if no default constructor member has been declared in the program. It is reported in the parameterized constructor.
Parameterized Constructor in C++
The name itself implies that the constructor, which passes the parameters in the function, is called the parameterized constructor.
Here is the syntax below,
function-name(parameter-list);
remember here, function-name is same as class-name and parameter-list represent the data-type which may be int , float, char type.
Example
#include<iostream>
using namespace std;
class construct
{
int a,b;
public:
// construct(){ } default constructor invoked by compiler
construct(int,int); //peramitrized constructor
};
construct::construct(int x,int y) // constructor defination outside class
{
a = x;
b = y;
cout<<"Total : "<<a+b<<endl;
}
int main()
{
construct x(2,3);
return 0;
}
OUTPUT
Total: 5
where the default constructor is used for the same values, the parameterized constructor is used to initialize different types of values.
use of constructor
- constructor used to initialize a class object.
- Because a constructor is automatically called as soon as a function class object is created, if a task is to be performed at the starting point of execution, it can be defined in the constructor body file handling exercise.
- However, class variables can be initialized even by a normal function, but where a normal member has to call each object, which can be complicated when the number of object increases, while the newly created object is automatically initialized in the constructor will go.
- a good example of this is to establish a connection to the server in a website, (defining the connection in the constructor body) where the constructor is automatically called when loading each web page.
Previous – class and object in C++
Next – destructor in C++