Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
Breakfast Static Function
//g++ 7.4.0 #include <iostream> using namespace std; class Breakfast { public: Breakfast (int qty ):itsqty(qty){item++;} virtual ~Breakfast (){item--;} virtual int GetQty(){return itsqty;} virtual void SetQty(int qty){itsqty=qty;} static int GetItem(){return item;}//GetItem is declared public and static. So any function can access it (reason is public) and there is no need to create an object (reason is static) private: static int item; int itsqty; }; int Breakfast::item=0; void TheFunction(); int main () { const int MaxItem=5; Breakfast * Break[MaxItem]; for(int i=0;i<MaxItem;i++) { Break[i]=new Breakfast (i); TheFunction(); } for(int i=0;i<MaxItem;i++) { delete Break[i]; TheFunction(); } return 0; } void TheFunction() { cout<<"There are "<<Breakfast::GetItem()<<" items left!\n"; }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
stlsizeof.cc
Median of two sorted arrays
sa
BindWithContainer
substr
Const Return Test
extern
Merge overlapping time intervals
single_digit
typename T class T
stackse - search stackoverflow differently
Please log in to post a comment.