+ 1

By using static variable

By using static variable write a function that when you call it display a message telling how many time it has been called. " i have been called 3 times " for instance write a main() program that ask the user to call the function i the user press Y the function is called otherwaise if N is pressed the program is terminated.

24th Dec 2018, 5:07 AM
Sami
Sami - avatar
4 Answers
0
If you are defining a class and want to define a static variable you are free to declare it into public, private or protected section. For example : class test{ private: static int a; public: int inc() { a++; } void display () { cout << a; } }; int main() { test t1, t2, t3; t1.inc(); t2.inc(); t3.inc(); t.display(); } When you create a object of class, only one copy of static member is generated through the whole code .
24th Dec 2018, 5:24 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
Hello, 😊 Please, if you want us to help you, then show us your attempt, what you don't understand exactly, where you are struggling, it will be much easier!👍😉 Use the search bar! https://www.sololearn.com/post/10362/?ref=app Please, read our guidelines: https://www.sololearn.com/discuss/1316935/?ref=app An useful code for any new user here! ;) https://code.sololearn.com/WvG0MJq2dQ6y/
24th Dec 2018, 5:13 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
thanks dear
24th Dec 2018, 5:25 AM
Sami
Sami - avatar
0
i am not sure that where this static variable is to be declared ?
24th Dec 2018, 5:16 AM
Sami
Sami - avatar