0

can anyone give me simplest working example for constructor and destructor?

need simplest example to understand...

26th Dec 2016, 6:51 PM
viva
2 Antworten
+ 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
26th Dec 2016, 7:13 PM
Adam Blažek
Adam Blažek - avatar
0
thanks :)
26th Dec 2016, 7:31 PM
viva