constructors in c++

C++ constructors

In this article, you will learn the complete concept of the constructors in c++ and constructors type in cpp.

what is a constructor?

The constructor is the member function of the class which makes the objects of the class. The constructor called automatically when the object is created.
Types of constructor

There are three types of constructors in the c++ language.

default constructor
parameterized constructor
copy constructor
constructors-in-cpp

How to create constructors?


For the creation of the constructor make the member function of the class name without return data type and write in the function what you initialize during the object creation. The privacy of the constructor usually public in the class  Following is the syntax of the constructor.


What is the syntax of the constructor



class myhouse{
public:
myhouse(){
cout<<" hello constructor";
}

};

The member function my house without return data type is the constructor of the class.

Default constructor

The constructor does not take any argument or has no parameters is called default constructor

Example


#include<iostream>
using namespace std;

class myhouse{
public:
myhouse(){
cout<<" welcome to house";        //display something to check calling of constructor
}

};


int main()
{
myhouse obj; //create an object of the class mmyhouse
return 0;
}


Parameterize constructors:


Constructors may be parameterized like parameterize functions. Parameterize constructors are used for initializing the values for the attributes of the class.

How to use parameterize constructor

Example:

#include<iostream>
using namespace std;

class myhouse{
int numberofroom;
int numberoffloor;

public:
myhouse(int room, int floor){
numberofroom=room; //first attribute
numberoffloor=floor; //2nd attribute
cout<<" constructor called"; //display something for checking constructor
}

};


int main()
{
myhouse obj(5,10); //create first object
myhouse obj2(20,14); //create second object
}

Here,myhouse class has two attributes number of floors and numberofroom. During the creation of the object, two int variables pass as parameters to the object. In both objects, two int variables pass through the object.

Copy constructor

A copy constructor initializes the object by another object of the same class.

Example

#include<iostream>
using namespace std;

class myhouse{                        //create class
int numberofroom;            //private attribute

int numberoffloor;                
public:                             //define constructor       
myhouse(int room, int floor){
numberofroom=room;
numberoffloor=floor;
cout<<" constructor called"<<endl;
}
myhouse(myhouse &obj)
{
numberoffloor=obj.numberoffloor;
numberofroom=obj.numberofroom;
cout<<" copy constructor"<<endl ;
}
void getdata(){
cout<<numberoffloor<<endl;
cout<<numberofroom<<endl;
}

};


int main()
{
myhouse obj(5,10); //create first object
obj.getdata();
myhouse obj2=obj; //create the second object
obj2.getdata();
return 0;
}

Can constructor define outside the class?


Yes, constructors define outside the class. For this purpose first, declare constructor inside the class then define the constructor outside the class. The scope resolution operator(::) is used with the class name for defining the constructor outside the class.

How to define constructor outside the class?

Example


#include<iostream>
using namespace std;

class myhouse{
int numberofroom;
int numberoffloor;
public:
myhouse(int room, int floor);

};
myhouse::myhouse(int room, int floor){
numberofroom=room;
numberoffloor=floor;
cout<<" constructor called";
}


int main()
{
myhouse obj(5,10); //create object
}

In this example, the constructor of the class myhouse outside the class. The constructor of myhouse is parameterized to accept the two int variables for initializing the attributes. The class name myhouse and scope resolution operator(::) is used before defining the constructor.

Please write the comment,if you find anything incorrect or you want to share more information about the above topic.

Comments

Popular posts from this blog

how to cout in cpp

Flutter layout

Diferrence between flutter and react native