Site icon DataFlair

Destructors in C++

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Destructor in C++
#include<iostream>
#include<conio.h>
using namespace std;
class Test
{
     int a,b;
    public:
    Test(int a,int b)  //constructor
    {  
        this->a=a;
        this->b=b;
        cout<<"\nI am constructor\n";
    }
    void maxData()
    {
         if(a>b)
          cout<<"\nFirst no is max";
        else
            cout<<"\nSecond no is max";
    }

  ~Test()  //desctuctor
    {
          a=0;
          b=0;
        cout<<" \n I am destructor\n";
    }

};
int main()
{
    system("cls");
    Test T1(100,200);
    T1.maxData();
   return 0;
}
Exit mobile version