Member initializer list is the place where non-default initialization of these objects can be specified. Initializer list is used to initialize data members. The syntax begins with a colon(:) and then each variable along with its value separated by a comma.
The program starts by including the necessary header files and defining a class Base. The class Base has a private data member x, which is initialized with the value passed as a parameter to the constructor of the class. The class Base also has a member function print(), which prints the value of x to the console.
In the main() function, an object o of class Base is created with an integer value of 25 passed as an argument to its constructor. The print() function of the object o is then called to display the value of x.
The use of a constructor in this program ensures that the private data member x of the class Base is always initialized with a value, which can be useful in preventing uninitialized variables. The program ends by returning 0 from the main() function, indicating that the program ran successfully.
//Member Initializer List //Single Value #include<iostream> using namespace std; class Base { private: int x; public: Base(int a):x(a){} void print(){ cout<<"X : "<<x<<endl; } }; int main() { Base o(25); o.print(); return 0; }
X : 25To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions