Answer to Question #238694 in C++ for Zeeshan

Question #238694
Write a program to calculate the number of objects created and destroyed for the
counter class.
1
Expert's answer
2021-09-18T15:48:00-0400
#include <iostream>
using namespace std;
class Counter{
    public:
    static int created, destroyed;
    Counter(){
        created++;
    }
    ~Counter(){
        cout<<"So far "<<++destroyed<<" destroyed"<<endl;
    }
};
int Counter::created = 0;
int Counter::destroyed = 0;
int main(){
    Counter one, two, three;
    cout<<"So far "<<Counter::created<<" created"<<endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS