Write C++ program to count the number of objects created and destroyed for a class usingÂ
static data members and static member functions.
#include <iostream>
/**
Write C++ program to count the number of objects created and destroyedÂ
for a class usingÂ
static data members and static member functions.
*/
class Base {
public:
    Base() {
        created++;
    }
    virtual ~Base() {
        destroyed++;
    }
    static int CtorCalls() {
        return created;
    }
    static int DtorCalls() {
        return destroyed;
    }
private:
    static int created;
    static int destroyed;
};
int Base::created = 0;
int Base::destroyed = 0;
int main() {
    {
        Base arr[10];
        Base another_one;
    }
    std::cout << "Ctors calls: " << Base::CtorCalls() << "\n";
    std::cout << "Dtors calls: " << Base::CtorCalls() << "\n";
    return 0;
}
Comments
Leave a comment