Copy Constructor

Prototype of copy constructor <classname>(const <classname>& ); Let say class name is foo. Then the copy constructor prototype will look like: foo(const foo& ); Scenarios:Below are the scenarios where copy constructor is called. 1) When an object is passed by value as function argument. 2) When function returns object by value. 3) When new object is created from an already existing/created object. Need:Compiler provides default copy constructor for each class. However, there are scenarios where we need to define the copy constructor explicitly. Consider a class below:#include <iostream>using namespace std;…

Initializer List

Let us understand the concept of initializer list in C++ with the help of an example. Consider a class viz. person, which has data members like age, name and employee dataExample #include <iostream>#include <string>using namespace std;class employeeData { unsigned int m_salary; public: employeeData() { cout << “employeeData default C’tor” << endl; m_salary = 0; }employeeData(unsigned int salary) { cout << “employeeData parameterized C’tor” << endl; m_salary = salary; }employeeData(const employeeData& data) { cout << “employeeData Copy C’tor” << endl; m_salary = data. m_salary; }employeeData& operator=(const employeeData& data) { cout <<…