212k views
1 vote
Write a destructor for the CarCounter class that outputs the following. End with newline.

Destroying CarCounter
#include
using namespace std;
class CarCounter {
public:
CarCounter();
~CarCounter();
private:
int carCount;
};
CarCounter::CarCounter() {
carCount = 0;
return;
}
/* Your solution goes here */
int main() {
CarCounter* parkingLot = new CarCounter();
delete parkingLot;
return 0;
}

1 Answer

0 votes

Answer:

Following are the code to this question:

CarCounter::~CarCounter()//Defining destructor CarCounter

{

cout << "Destroying CarCounter\\";//print message Destroying CarCounter

}

Step-by-step explanation:

Following are the full program to this question:

#include <iostream>//Defining header file

using namespace std;

class CarCounter //Defining class CarCounter

{

public:

CarCounter();//Defining constructor CarCounter

~CarCounter();//Defining destructor CarCounter

private:

int carCount;//Defining integer variable carCount

};

CarCounter::CarCounter()//declaring constructor

{

carCount = 0;//assign value in carCount variable

return;//using return keyword

}

CarCounter::~CarCounter()//Defining destructor CarCounter

{

cout << "Destroying CarCounter\\";//print message Destroying CarCounter

}

int main() //Defining main method

{

CarCounter* parkingLot = new CarCounter();//Defining class object parkingLot

delete parkingLot;//

return 0;

}

  • In the given C++ language code, a class "CarCounter" is defined, and inside the class, a "constructor, Destructors, and an integer variable" is defined.
  • Outside the class, the scope resolution operator is used to define the constructor and assign value "0" in the integer variable.
  • In the above-given code, the scope resolution operator, to define destructor and inside this cout function is used, which prints a message.
  • In the main method, the class object is created, which automatically calls its class constructor and destructors.
User Boris Burtin
by
5.3k points