1. Constructor - idea and syntax #

Created Wednesday 08 January 2020

Idea of a constructor #

Properties of a constructor #

  1. It is a function
  2. Name is the same as that of the class.
  3. Has no return type, not even void.
  4. Called only once in the life-time of an object.

Types of constructors #

  1. Default constructor - No arguments. It is named default coz it takes no argument.
  2. Parameterized constructor - takes some argument.
  3. Copy constructor - Takes only one object of the same class as argument.

Notes:

// compiler provided constructor
class_name()
{

}

http://www.cplusplus.com/doc/tutorial/classes2/#default_constructor

int main()
{
	Car c;
	string name = "Batman";
	c.ride(Passenger(name)); // explicit call without declaration
}