1.

डिस्ट्रक्टर का अर्थ समझाइए। अथवाडिस्ट्रक्टर को उदाहरण सहित समझाइए। 

Answer»

यह किसी क्लास का एक विशेष सदस्य फंक्शन होता है, जो तब क्रियान्वित होता है जब उस क्लास का ऑब्जेक्ट सीमा (Scope) से बाहर जाता है।
इसका नाम भी वही होता है, जो क्लास का नाम होता है, परन्तु उसमें पहले टाइल्ड (~) प्रीफिक्स का उपयोग किया जाता है।

उदाह्रण डिस्ट्रक्टर की क्रियाविधि

#include<iostream.h>
#include<conio.h>
class Line
{
private:
double length;
public:
void set Length (double len);
double getLength ();
Line (); //This is the constructor
//declaration
~Line(); //This is the destructor
//declaration
};
Line :: Line () //Constructor definition
{
cout<<"Object is being created"<<end1;
}
Line : : ~Line() //Destructor definition
{
cout<<"Object is being deleted"<<end1;
}
void Line :: set Length (double len)
{
length = len;
}
double Line :: getLength()
{
return length;
}
void main()
{
Line line; line.setLength(6.0); //set line length
cout<<"Length of line";
cout<<line.getLength() <<end1;
getch();
}

आउटपुट

0bject is being created
Length of line 6
Object is being deleted



Discussion

No Comment Found