Program : constructor, destructor in multilevel inheritance


#include<iostream.h>
#include<conio.h>
class base
{
protected:
int a;
public:
base(int x)
{
a=x;
cout<<"Inside base constructor"<<endl;
}
~base()
{
cout<<"Inside base destructor"<<endl;
}
};//end of base class


class derived1 : public base
{
protected:
int b;
public:
derived1(int x,int y):base(x)
{
b=y;
cout<<"Inside derived1 constructor"<<endl;
}
~derived1()
{
cout<<"Inside derived1 destructor"<<endl;
}
};//end of derived class
class derived2 : public derived1
{
int c;
public:
derived2(int x,int y,int z):derived1(x,y)
{
c=z;
cout<<"Inside derived2 constructor"<<endl;
}
~derived2()
{
cout<<"Inside derived2 destructor"<<endl;
}
void show()
{
    cout<<"a : "<<a<<" "<<"b : "<<b<<"  "<<"c : "<<c<<endl;
}
};//end of derived class
void main()
{
clrscr();
derived2 obj(3,4,5);
obj.show();
}
/*
Inside base constructor
Inside derived1 constructor
Inside derived2 constructor
a : 3 b : 4  c : 5
Inside derived2 destructor
Inside derived1 destructor
Inside base destructor
*/

8 comments:

  1. thank you.....helped a lot

    ReplyDelete
  2. This excellent website truly has all the information and facts I
    needed concerning this subject and didn't know who to ask.

    My site - cellulite treatment reviews

    ReplyDelete
  3. Very informative article.Thank you author for posting this kind of article .

    http://www.wikitechy.com/view-article/insertion-sorting-

    program-in-cpp-with-example-and-explanation



    Both are really good,
    Cheers,
    Venkat

    ReplyDelete
  4. Useful post about constructor and destructor in multiple inheritance..java training in chennai

    ReplyDelete
  5. browse around these guys replica bags toronto site link replica bags los angeles replica dolabuy replica bags paypal

    ReplyDelete