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;
}

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *