pls explain me this code Count() : value(5) {} what this statement do and use of this statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

pls explain me this code Count() : value(5) {} what this statement do and use of this statement

25th Dec 2020, 3:53 PM
Tushar Jain
Tushar Jain - avatar
3 Answers
+ 2
Can you share the entier code where it has been used, because in its current, I don't think it is making any sense.
25th Dec 2020, 4:03 PM
Arsenic
Arsenic - avatar
+ 1
Tushar Jain doesn't the comment above that constructor explains it all ? Here, you are using an initializer list to initialize member (value) of that class. Like this whenever you will create an object of type *Count* , value of *value* will be set to 5 for that object edit :- If this type of initialization method is new to you then we can use initialization list to initialize constant members,references and base class also which is not possible with conventional method
25th Dec 2020, 4:16 PM
Arsenic
Arsenic - avatar
0
// C++ program to overload ++ when used as prefix #include <iostream> using namespace std; class Count { private: int value; public: // Constructor to initialize count to 5 Count() : value(5) {} // Overload ++ when used as prefix void operator ++() { value = value + 1; } void display() { cout << "Count: " << value << endl; } }; int main() { Count count1; // Call the "void operator ++()" function ++count1; count1.display(); return 0; }
25th Dec 2020, 4:07 PM
Tushar Jain
Tushar Jain - avatar