Answer to Question #345459 in C++ for Monty

Question #345459

Design a class having the constructor and destructor functions that should display the number of objects being created or destroyed of class type


1
Expert's answer
2022-05-27T09:39:31-0400
#include <iostream>
using namespace std;
class Enemy{
    private:
        static int created;
        static int destroyed;
    public:
        Enemy(){
            cout<<"Created: "<<++created<<endl;
        }
        ~Enemy(){
            cout<<"Destroyed: "<<++destroyed<<endl;
        }
};


int Enemy::created;
int Enemy::destroyed;
int main()
{
    Enemy a, b;
    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
APPROVED BY CLIENTS