Monday, April 5, 2021

Destructor (OOP) (19)

  /*destructor

                                     ***********/

//destructor name same as the class name. destructor have no return type. destructor cannot take an 

//arrguments. we use this sign ~ with the destructor. we are no calling the constructor in the main because 

//when we make the object bof the class in the main the destructor call automatically.we can use only one 

//destructor in  one class.destructor can never be static. destructor can not be overloaded. basically 

//destructor destruct the object.

#include<iostream>

using namespace std;

class test

{

static int count; //here we declear the static data member.

public:

test() //here we defind the constructor 

{

count++;

cout<<count<<" "<<"object is created:"<<endl;

}

~ test() //here we defind the destrtuctor. and this is the method of defining the destructor.

{

cout<<count<<" "<<"object is destroyed:"<<endl;

count--;

}

};

int test :: count; //when we declear the static data member we must should be define outside the class.

int main()

{

test t1;

test t2;

}

No comments:

Post a Comment