0
can anyone give me simplest working example for constructor and destructor?
need simplest example to understand...
2 Answers
+ 1
#include <iostream>
using namespace std;
class C{ //define a class named C
C(){ //constructor
cout<<1;
}
~C(){ //destructor
cout<<2;
}
};
int main(){
C instance; //create an instance of the class C.
//This will cause "1" to be printed
//because it's in the constructor.
cout<<3;
return 0; } //The main function ends,
//therefore the instance is destroyed
//which triggers the destructor and prints "2".
Output:
132
Hope I got it right, haven't worked with C++ for some time
0
thanks :)