A constant is a variable whose value cannot change once it has been assigned. To declare any variable as constant, we use static and final modifiers. It is also known as non-access modifiers. To initialize the const value using constructor, we have to use the initialize list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma .
The program demonstrates the use of a constant data member in a C++ class.
//Initialized Const variable using Member Initializer List #include<iostream> using namespace std; class Base { private: const int x; public: Base(int a):x(a) { cout<<"The Value of X :"<<x; } }; int main() { Base o(50); return 0; }
The Value of X :50To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions