Lab – 2018/04/12 implement getTotalCount to return how many ‘nMatrix’s are allocated(exist) (modify constructor and destructor, use static variable and static function) int main() { nMatrix a(5, 3); cout <<nMatrix::getTotalCount(); << endl; // print 1 nMatrix b(5, 1); nMatrix c(3, 5); cout << nMatrix::getTotalCount(); << endl; // print 3 }
Hint 1 – static variables void demo() { // static variable static int count = 0; cout << count << " "; // value is updated and // will be carried to next // function calls count++; ] int main() { for (int i=0; i<5; i++) demo(); return 0; }
Hint 2 – static variables in a class class GfG { public: static int i; GfG() // Do nothing }; int main() { GfG obj1; GfG obj2; obj1.i = 2; GfG::i = 3; // prints value of i cout << obj1.i<<" "<<obj2.i << endl; cout << GfG::i << endl; }
Hint 3 – static functions in a class class GfG { public: // static member function static void printMsg() cout<<"Welcome to GfG!"; } }; int main() { // invoking a static member function GfG::printMsg(); }