« Initialisation list in C++

Const properties and reference properties in a Class

When we enter the constructor the properties have already been assigned memory.

1class Student
2{
3public:
4 int rollNumber; // by default properties are non-static
5 int age;
6 char *name;
7 const int x;
8 int &y;
9
10 Student(){
11
12 }
13}

Const properties and reference properties needs to initialized at the time of the memory allocation step itself. For such cases initialization list comes to rescue

So whenever we have const and reference properties we need to define constructor and so that we can define initialization list.

whenever you have const and reference properties in your class then default copy assignment operator won't be available. We need to define our own.

1void operator=(const Student &other)
2{
3 this->age = other.age;
4 this->rollNumber = other.rollNumber;
5 this->name = other.name;
6}

For full github code use:

https://github.com/anishakd4/ds/tree/master/cn/oops