+ 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.
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 .
+ 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/
+ 1
thanks dear
0
i am not sure that where this static variable is to be declared ?