Copy Constructor in C++

What is copy constructor? “A copy constructor is used to declare and initialize an object from another object.” “Constructor which accepts a reference to its own class as a parameter is called copy constructor.“ Look at below code: The process of initializing through a copy constructor is known as copy initialization. Here, t2=t1 will not invoke copy constructor. t1 and t2 are objects, assigns the values of t1 to t2. A copy constructor takes a reference to an object of the same class as itself as an argument. Example: The…

Read More

Constructor in C++

C++ provides a special member function called the constructor which enables an object to initialize itself when it is created. What is Constructor ? “A constructor is a special member function whose task is to initialize the objects of its class.” It is special because its name is the same name as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class. Syntax: A constructor is declared and defined…

Read More