169. Write a program to demonstrate order of execution of constructor and destructor?

#include<iostream.h> #include<conio.h> int count=0; class demo { public:                 demo()                                 {                                 ++count;                                 cout<<"Constructor Is Called With Order :"<<count<<endl;                                 }                 ~demo()                                 {                                 cout<<"Destructor Is Called With Order : "<<count<<endl;                                 --count;                                 } }; void main() {                 clrscr();                 demo d1,d2,d3;                 cout<<"In Main() Function"<<endl; }

Advertisements

168. Write a program to initialize member variable using constructor and displaying through destructor?

#include<iostream.h> #include<conio.h> class demo {                 int x; public:                 demo()                                 {                                 cout<<"Constructor Is Called"<<endl;                                 x=7;                                 }                 ~demo()                                 {                                 cout<<"Destructor Is Called"<<endl;                                 cout<<"X = "<<x<<endl;                                 getch();                                 } }; void main() {                 clrscr();                 demo d;                 cout<<"In Main() Function"<<endl; }

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: