Question
I need help with the following problem:
Create a class named Car. The Car class contains a static integer field named count. Create a constructor that adds 1 to the count and displays the count each time a Car is created. Create a destructor that subtracts 1 from the count and displays the count each time a Car goes out of scope. Write a main()function that declares an array of five Car objects. Output consists of five constructor messages and five destructor messages, each displaying the current count, similar to the output in Figure 8-34.
I need help with the following problem: Create a class named Car. The Car class contains a static integer field named count. Create a constructor that adds 1 to the count and displays the count each time a Car is created. Create a destructor that subtracts 1 from the count and displays the count each time a Car goes out of scope. Write a main()function that declares an array of five Car objects. Output consists of five constructor messages and five destructor messages, each displaying the current count, similar to the output in Figure 8-34. Output needs to be like the image below:
Explanation / Answer
#include using namespace std; class Car{ static int car_number; public: Car(); ~Car(); }; //sets class Car::Car() //constructor { car_number++; if(car_number==1) { cout