In class member initializers
As the name suggests that we can initialize the data members in the class declaration. Example:
class test { int m_a{1}; int m_b=2; };
source code with example:
/* Program: In class initialization Author: Alpha Master Date: 10 March 2021 */ //Header File #include<iostream> class test { int m_a{1}; int m_b=2; public: void display() { std::cout<<"A:"<<m_a<<" B:"<<m_b<<std::endl; } }; int main() { std::cout<<"In Class Intialization"<<std::endl; test obj1; obj1.display(); return 0; }
Output:
In Class Intialization A:1 B:2