adsense


Wednesday, 25 November 2015

C++ LANGUAGE USING INITIALIZATION LISTS TO INITIALIZE FIELDS

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



C++ LANGUAGE USING INITIALIZATION LISTS TO INITIALIZE FIELDS






Using Initialization Lists to Initialize Fields 


In case of parameterized constructor, you can use following syntax to initialize the fields: 


Line::Line( double len): length(len) 


{ 
    cout << "Object is being created, length = " << len << endl; 
} 
Above syntax is equal to the following syntax: 
Line::Line( double len) 
{ 
    cout << "Object is being created, length = " << len << endl; 
    length = len; 
} 
If for a class C, you have multiple fields X, Y, Z, etc., to be initialized, 
then use can use same syntax and separate the fields by comma as follows: 

C::C( double a, double b, double c): X(a), Y(b), Z(c) 
{ 
  .... 
} 


No comments: