Initialization List examples code in C++ in Linux

/*1) For initialization of non-static const data members:
const data members must be initialized using Initializer List. In the following example, “t” is a const data member of Test class and is initialized using Initializer List.
*/
#include<iostream>
using namespace std;

class Test {
    const int t;
public:
    Test(int t):t(t){} //error: assignment of read-only member ‘Test::t’ if assignment is used
    //Test(int t){this->t=t;} //error: assignment of read-only member ‘Test::t’ if assignment is used
    int getT() { return t; }
};

int main() {
    Test t1(10);
    cout<<t1.getT()<<endl;;
    return 0;
}

/* OUTPUT:
   10
*/
/*2) For initialization of reference members:
Reference members must be initialized using Initializer List. In the following example, “t” is a reference member of Test class and is initialized using Initializer List.
// Initialization of reference data members*/
#include<iostream>
using namespace std;

class Test {
    int& t;
public:
    Test(int &t):t(t) {cout<<"constructor\n";}
    //Test(int &t) {this->t=t;}  //error: uninitialized reference member ‘Test::t’
    int getT() { return t; }
};

int main() {
    int x = 20;
    Test t1(x);
    cout<<t1.getT()<<endl;
    x = 30;
    cout<<t1.getT()<<endl;
int y=100;
    cout<<t1.getT()<<endl;

//    Test t(400);//error: no matching function for call to ‘Test::Test(int)’
    return 0;
}
/* OUTPUT:
    20
    30
    30
 */
//3.//Initialer list is used for initializing contained class object which does not have a default copy constructor.
#include <iostream>
using namespace std;

class A {
    int i;
public:
    A(int );
};

A::A(int arg) {
    i = arg;
    cout << "A's Constructor called: Value of i: " << i << endl;
}

// Class B contains object of A
class B {
    A a;
public:
    B(int );
};

B::B(int x):a(x) {  //Initializer list must be used
    cout << "B's Constructor called";
}

int main() {
    B obj(10);
    return 0;
}
/* OUTPUT:
    A's Constructor called: Value of i: 10
    B's Constructor called
*/
/*4) For initialization of base class members : Like point 3, parameterized constructor of base class can only be called using Initializer List.*/
#include <iostream>
using namespace std;

class A {
    int i;
public:
    A(int );
};

A::A(int arg) {
    i = arg;
    cout << "A's Constructor called: Value of i: " << i << endl;
}

// Class B is derived from A
class B: A {
public:
    B(int );
};

B::B(int x):A(x) { //Initializer list must be used
    cout << "B's Constructor called"<<endl;
}

int main() {
    B obj(10);
    return 0;
}
/*
A's Constructor called: Value of i: 10
B's Constructor called
*/
/*5) When constructor’s parameter name is same as data member
If constructor’s parameter name is same as data member name then the data member must be initialized either using this pointer or Initializer List. In the following example, both member name and parameter name for A() is “i”.
*/
#include <iostream>
using namespace std;

class A {
    int i;
public:
    A(int );
    int getI() const { return i; }
};

//A::A(int i):i(i) { }  // Either Initializer list or this pointer must be used
// The above constructor can also be written as
A::A(int i) {
    this->i = i;
    //i = i;//some garbage
}


int main() {
    A a(10);
    cout<<a.getI()<<endl;
    return 0;
}
/* OUTPUT:
    10
*/

No comments:

Post a Comment