What is Constructor in C++ ?

  • In C++ constructor is a special type of member function of a class which initializes objects of a class.
  • Constructor is automatically called when object is created in C++.
  • It does not have any return type because it is special member function of the class.
  • In public section of class and it has same name as the class itself.
  • Copy and parameterized constructors have input arguments, by default constructors don’t have input argument however.
  • C++ compiler generates a default constructor for object, if we do not specify a constructor.
  • In constructor there are three types, they are default constructor, parameterized constructor, copy constructor.

Default Constructor

  • It is the constructor which has no parameters and doesn’t take any argument.

Sample Code

[pastacode lang=”cpp” manual=”using%20namespace%20std%3B%0A%0Aclass%20construct%0A%0A%7B%0A%0Apublic%3A%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0int%20a%2C%20b%3B%0A%0A%0A%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%20Default%20Constructor%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0construct()%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0a%20%3D%2030%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0b%20%3D%2040%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7D%0A%0A%7D%3B%0A%0A%0A%0A%0Aint%20main()%0A%0A%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%20Default%20constructor%20called%20automatically%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%20when%20the%20object%20is%20created%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0construct%20c%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0cout%20%3C%3C%20%22a%3A%20%22%20%3C%3C%20c.a%20%3C%3C%20endl%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%3C%3C%20%22b%3A%20%22%20%3C%3C%20c.b%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0return%201%3B%0A%0A%7D” message=”” highlight=”” provider=”manual”/]

Output

Parameterized Constructor

  • It is possible to pass arguments to constructors and these arguments help initialize an object when it is created typically.

Sample Code

[pastacode lang=”cpp” manual=”using%20namespace%20std%3B%0A%0Aclass%20Point%0A%0A%7B%0A%0Aprivate%3A%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0int%20x%2C%20y%3B%0A%0A%0A%0A%0Apublic%3A%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%20Parameterized%20Constructor%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0Point(int%20×1%2C%20int%20y1)%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0x%20%3D%20×1%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0y%20%3D%20y1%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7D%0A%0A%0A%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0int%20getX()%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0return%20x%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7D%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0int%20getY()%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0return%20y%3B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%7D%0A%0A%7D%3B%0A%0A%0A%0A%0Aint%20main()%0A%0A%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%20Constructor%20called%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0Point%20p1(10%2C%2015)%3B%0A%0A%0A%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%20Access%20values%20assigned%20by%20constructor%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0cout%20%3C%3C%20%22p1.x%20%3D%20%22%20%3C%3C%20p1.getX()%20%3C%3C%20%22%2C%20p1.y%20%3D%20%22%20%3C%3C%20p1.getY()%3B%0A%0A%0A%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0return%200%3B%0A%0A%7D” message=”” highlight=”” provider=”manual”/]

 Output

Copy Constructor

  • It is a member function which initializes an object using another object of the same class.

Sample Code

[pastacode lang=”cpp” manual=”using%20namespace%20std%3B%0A%0Aclass%20point%0A%0A%7B%0A%0Aprivate%3A%0A%0A%C2%A0%C2%A0double%20x%2C%20y%3B%0A%0A%0A%0A%0Apublic%3A%0A%0A%C2%A0%C2%A0%C2%A0%0A%0A%C2%A0%C2%A0%2F%2F%20Non-default%20Constructor%20%26%0A%0A%C2%A0%C2%A0%2F%2F%20default%20Constructor%0A%0A%C2%A0%C2%A0point%20(double%20px%2C%20double%20py)%0A%0A%C2%A0%C2%A0%7B%0A%0A%C2%A0%C2%A0%C2%A0%C2%A0x%20%3D%20px%2C%20y%20%3D%20py%3B%0A%0A%C2%A0%C2%A0%7D%0A%0A%7D%3B%0A%0A%0A%0A%0Aint%20main(void)%0A%0A%7B%0A%0A%0A%0A%0A%C2%A0%C2%A0%2F%2F%20Define%20an%20array%20of%20size%0A%0A%C2%A0%C2%A0%2F%2F%2010%20%26%20of%20type%20point%0A%0A%C2%A0%C2%A0%2F%2F%20This%20line%20will%20cause%20error%0A%0A%C2%A0%C2%A0point%20a%5B10%5D%3B%0A%0A%0A%0A%0A%C2%A0%C2%A0%2F%2F%20Remove%20above%20line%20and%20program%0A%0A%C2%A0%C2%A0%2F%2F%20will%20compile%20without%20error%0A%0A%C2%A0%C2%A0point%20b%20%3D%20point(5%2C%206)%3B%0A%0A%7D” message=”” highlight=”” provider=”manual”/]

Output

Leave a Reply

Your email address will not be published. Required fields are marked *