Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Breakfast Static Function
//g++ 7.4.0 //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #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
mine
char strcpy
PayTax
pattern
Radix sort
dijkstra
Synchro#1
ClassQuiz2
synowie abrahama
PointClassOpLoad