C++ programming language part- 60 More about Constructor and Destructor
More about Constructor and Destructor
It is possible for a base class, a
derived class, or both to contain constructor or destructor. The first base
constructor is invocked followed by derived and next derived destructor is
called followed by base. Because object is immediatly destroyed in any program.
Virtual and non-virtual base class constructor and then derived class
constructor are invocked in order of inheritance. Destructor are invocked in
the reverse order.
Program
44-1
class A //class
defined
{ public:
A()
{ cout<<"constructor A invoked\n"; }
~A()
{ cout<<"destructor A invoked\n"; }
};
class B //class
defined
{ public:
B()
{ cout<<"constructor B invoked\n"; }
~B()
{ cout<<"destructor B invoked\n"; }
};
class C:virtual public A //class
defined
{ public:
C()
{ cout<<"constructor C invoked\n"; }
~C()
{ cout<<"destructor C invoked\n"; }
};
class
D:virtual public A //class defined
{ public:
D()
{ cout<<"constructor D invoked\n"; }
~D()
{ cout<<"destructor D invoked\n"; }
};
class
E //class defined
{ public:
E()
{ cout<<"constructor E invoked\n"; }
~E()
{ cout<<"destructor E invoked\n"; }
};
class
F:public B,public C,public D
{ private:
E Evar; // member objet of class E
public:
F()
{ cout<<"constructor F invoked\n"; }
~F()
{ cout<<"destructor F invoked\n"; }
};
void main()
{ F Fvar;
cout<<"Program
termited\n"; }
Program
44_2
class
A //class defined
{
private:
int x,y;
public:
A()
{ cout<<"default constructor A
invoked\n";
x=29;
y=45; }
~A()
{ cout<<"destructor A
invoked\n"; }
void disp()
{ cout<< "class A \n";
cout<<"x=
"< };
{ public:
B()
{ cout<<"constructor B
invoked\n"; }
~B()
{ cout<<"destructor B
invoked\n"; }
void disp()
{ cout<<"class B \n"; }
};
class
C:virtual public A //class defined
{ public:
C()
{ cout<<"constructor C
invoked\n"; }
~C()
{ cout<<"destructor C
invoked\n"; }
void disp()
{ cout<<"class C \n"; }
};
class D:public B,public C //class
defined
{ public:
D()
{ cout<<"constructor D
invoked\n"; }
~D()
{ cout<<"destructor D
invoked\n"; }
};
void main()
{ D K;
int p=234;
K.A::disp();
K.A::~A();
K.B::disp();
cout<<"Program
termited\n";
}
Part- 86 assign the content of twostring
জ্ঞানকোষ প্রকাশনী
৩৮/২-ক, বাংলাবাজার (২য় তলা), ঢাকা।
ফোনঃ ৭১১৮৪৪৩, ৮১১২৪৪১, ৮৬২৩২৫১.
কলকাতায় পরিবেশক/প্রাপ্তিস্থান
রিতা ইন্টারন্যাশনাল
৩৬, পি.এন. ব্যানার্জি রোড, কলকাতা
ফোনঃ ২৫১৩৮৩৫৯, ৯৮৩০৪৩৯৬৭৯, +৯১৯৮৩০৪৩৯৬৭৯