initialization list in c
Download
Skip this Video
Download Presentation
Initialization List in C++

Loading in 2 Seconds...

play fullscreen
1 / 38

Initialization List in C++ - PowerPoint PPT Presentation


  • 181 Views
  • Uploaded on

Initialization List in C++. Definition:. Initialization List is another or a better approach for initializing the member variables (and base class objects) of a class upon construction of an instance of it\'s own. Things to know about Initialization List.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Initialization List in C++' - jeslyn


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
definition
Definition:
  • Initialization List is another or a better approach for initializing the member variables (and base class objects) of a class upon construction of an instance of it\'s own.
things to know about initialization list
Things to know about Initialization List
  • Initialization List is used to initialize both user defined data types (like embedded object of a class) and also primitive/built-in data types (like int, char).
  • Initializing the member variables in the Initialization List is better than initializing them inside the body of the constructor of the class.
  • Data members are initialized in the order they are declared, regardless of the order of their initialization. 
things to know about initialization list1
Things to know about Initialization List
  • It is mandatory to initialize Reference Data Member in an Initialization List because it can not exist without being initialized.
  • It is mandatory to initialize Constant Data Member in an Initialization List otherwise it would be compiler error.
  • It is mandatory to construct and initialize, embedded class objects/base class objects in case of inheritance, in an Initialization List, if they do not themselves have a zero-argument/default constructor provided.
slide6

#include<iostream>

using namespace std;class A{  int a;  char b;  public:    A():a(100),b(\'m\')    {      cout<<"Value of a:"<<a<<endl;      cout<<"Value of b:"<<b<<endl;    }};

slide9

#include<iostream>

using namespace std; class B{  int x;  public:    B(int data):x(data){}    void print()    {cout<<"Value of x is: "<<x<<endl;}};

slide10

class A{int a;   B b;  public:    A():a(100),b(a)    {cout<<"Value of a:"<<a<<endl;b.print();    }};

slide15

class Rect{private:intwidth_m, height_m;public:Rect(int w, int h) :width_m(w), height_m(h) //initialization list is here {cout<<"Ctor...\n"; }};

slide17

class Rect{private:intwidth_m, height_m;public:Rect(int w, int h);};Rect(int w, int h) :width_m(w), height_m(h) //initialization list is here{cout<<"Ctor...\n";}

slide18

Initializing the member variables in the Initialization List is better than initializing them inside the body of the constructor of the class

slide19

There are two steps that takes place when Member Objects of a class are initialized inside the body of a constructor.

slide20

Member Objects are allocated a memory/constructed and are given default values by the time when the control enters body of the constructor.

  • Later on the actual initialization happens inside the body of the constructor i.e. user written initialization code that gets called.
slide21

Data members are initialized in the order they are declared, regardless of the order of their initialization.

slide22

It is important to know that the order of initializing the member variables in the Initialization List must match the order of their declarations inside the class. The reason is that the compiler automatically transforms the Initialization List so that it coincides with the order of the declaration of class members inside the class.

slide23

#include<iostream>

using namespace std; class A{  int a;  int b;  public:    A():b(100),a(b)    {      cout<<"Value of b:"<<b<<endl;      cout<<"Value of a:"<<a<<endl;    }};

slide25

With respect to the above point, as the variablea is declared earlier than b, it is (i.e. variable a) initialized earlier than variableb. So, in the Case 1, initialization of a(b) lead to junk initialization as b was not yet initialized to100 then. 

slide27

class A{int a;int b;  public:    A():a(100),b(a)    {cout<<"Value of b:"<<b<<endl;cout<<"Value of a:"<<a<<endl;    }};

reference data member
Reference Data Member

Though it is an option for initializing other member variables of a class in an Initialization List, but it is must to initialize Reference data members in an Initialization List.

slide31

#include<iostream>

using namespace std; class A{  int &a;  int b;    public: A():a(b), b(10){cout<<"Value of reference var a :"<<a<<endl;};};int main(){  A a;  return(0);}

slide32

In the above code, compiler throws an error, if reference variable a is not initialized in the Initialization List.

constant data member
Constant Data Member

#include<iostream>

using namespace std; class A{    const int a;    const int b;public:A(){}    void print(){cout<<"a = "<<a<<endl;                 cout<<"b = "<<b<<endl;};};

constant data member1
Constant Data Member

int main(){A a;a.print();return (0);}Compiler Error: Const must be initiliazed

slide35

As shown in the above code, constant data members a and b are not not initialized in an Initialization List and so they are by default initialized with some junk value.

no default constructor
No Default Constructor

If class has an embedded class object that has no default constructor or if the class is derived from another class which has no default constructor, then we must specify which constructor we wish to use while constructing and initializing the object of that class, in an Initialization List of our class\'s constructor.

slide37

If we take the same example that is used for User Defined Data Type above, the class B is a member variable of class A and has no default constructor and if we are not initializing the class B\'s instance in class A\'s constructor\'s initialization List, then compiler will throw the below error.

slide38

initList3.cc: In constructor `A::A()\':initList3.cc:21: error: no matching function for call to `B::B()\'initList3.cc:4: error: candidates are: B::B(const B&)initList3.cc:8: error: B::B(int) 

ad