Cpp - Write program to create a class with default constructor and destructor

Requirements

Write program to create a class with default constructor and destructor

Output message from the default constructor and destructor

Demo

#include <iostream>
  
class Bug// w w  w. j  ava2  s  . co  m
{
public:
    Bug();
    ~Bug();
private:
    int itsAge;
};
  
Bug::Bug()
{
    std::cout << "Constructor called\n";
    itsAge = 1;
}
  
Bug::~Bug()
{
    std::cout << "Destructor called\n";
}
  
int main()
{
    std::cout << "Bug bug ...\n";
    Bug bug;
  
    std::cout << "Bug *pRags = new Bug ...\n";
    Bug * pRags = new Bug;
  
    std::cout << "delete pRag s ...\n";
    delete pRags;
  
    std::cout << "Exiting, watch bug go ...\n";
        
    std::cout << "Bug Spooky ...\n";
    Bug Spooky;
    
    return 0;
}

Result

Related Exercise