Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Breakfast 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; //Accessing static member without an object #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 item; private: int itsqty; }; int Breakfast::item=0; void TheFunction();//This function does not create a breakfast object nor does it take a breakfast object as parameter but it can access item member variable 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::item<<" items left!\n"; }
run
|
edit
|
history
|
help
0
Newspaper
TPSI Primo esercizio
static property
CirclQ
replace_copy-30-Seconds-of-C++
sin_approximation
СП КИ ЭТАП 2
inheritance test
remove_copy-30-Seconds-of-C++
PrintShape