Doubt regarding constructors

This is a discussion on Doubt regarding constructors within the C++ Programming forums, part of the General Programming Boards category; Code: /* Program to study constructors using inheritance... */ #include<iostream.h> #include<conio.h> class base1 { int x; public: base1(){} base1(int a) ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    79

    Doubt regarding constructors

    Code:
    /*
    Program to study constructors using inheritance...
    */
    #include<iostream.h>
    #include<conio.h>
    class base1
    {
    	int x;
    	public:
    		base1(){}
    		base1(int a)
    		{
    			x=a;
    			cout<<"Constructing base1..."<<x<<endl; getch();
    		}
    		~base1(){cout<<"Destroying base1..."<<endl; getch();}
    };
    class derived1
    {
    	int y; 
    	public:
    		derived1(){}
    		derived1(int b)
    		{
    			y=b;
    			cout<<"Constructing derived1..."<<y<<endl; getch();
    		}
    		~derived1(){cout<<"Destroying derived1..."<<endl; getch();}
    };
    class derived2:public base1,public derived1
    {
    	protected:
    	int z;
    	public:
    		derived2(){}
    		derived2(int a,int b,int c):
    		base1(a),
    		derived1(b)
    		{
    			z=c;
    			cout<<"Constructing derived2..."<<z<<endl; getch();
    		}
    	~derived2(){cout<<"Destroying derived2..."<<endl; getch();}
    };
    int main()
    {
    	clrscr();
    	derived2 d1;
    	d1=derived2(1,2,3);
    	cout<<"Executing getch()..."<<endl;
    	getch(); return(0);
    }
    Hello everyone. I had a small doubt regarding the above program. The destructors of all three classes are being executed just before cout<<"Executing getch()..."<<endl; as well as after the getch() statement, in the main function. I was wondering why this is happening, as I was expecting the destructors to be executed only once, i.e. after the getch() statement in main(). Please help me out.

    Compiler : Borland C++ 5.5
    Last edited by sundeeptuteja; 05-30-2003 at 11:54 PM.

  2. #2
    max_unreg
    Guest
    I executed your program, this is what I got.

    Code:
    Constructing base1...1
    Constructing derived1...2
    Constructing derived2...3
    Destroying derived2...
    Destroying derived1...
    Destroying base1...
    Executing getch()...
    Destroying derived2...
    Destroying derived1...
    Destroying base1...
    Press any key to continue
    here's what happens:

    Code:
    derived2 d1;
    the first constructor for derived2 gets called. you didn't chain the constructors, so no base class constructor gets called.
    (add cout << "derived2 default" in the derived2() constructor) =>nothing gets printed on the output.

    now, you ASSIGN d1 to a new derived2 object.

    Code:
    d1=derived2(1,2,3);
    When you do this, few things happen:

    * constructor call of the right handside member (this outputs the first 3 lines)

    * assignement call of the left handside member => none has been defined, using default assignement operator by destroying d1 (this outputs the destructor calls of d1: next three lines )

    * perform member by member copy between d1 and the new object

    Now, the main() function returns => destruction of d1, SECOND call to its destructor, and hence the last three lines of output

    Everything seems just fine to me...... but maybe what you thought you did was :

    Code:
    derived2 d1;    // declare a d1 object
    d1 = derived2(1,2,3)   // construct the object
    this is true in java:
    Code:
    derived2 d1;
    d1 = new derived2(1,2,3); // only one constructor call
    but not in c++. In C++ you have to do this instead:

    Code:
    derived2 d1(1,2,3);  // only one constructor call

    in C++, you have to understand the basic of constructor / destructor calls (this was a good exercise) or you're in big trouble

  3. #3
    max_unreg
    Guest
    little add-on: using getch() messes everything up.

    this is how I've modified the end of your code:
    Code:
    class derived2: public base1,public derived1
    {
    	protected:
    	int z;
    	public:
    		derived2(){cout <<"default derive2 constructor\n";}
    		derived2(int a,int b,int c):
    		base1(a),
    		derived1(b)
    		{
    			z=c;
    			cout<<"Constructing derived2..."<<z<<endl;
    		}
    	~derived2(){cout<<"Destroying derived2..."<<endl;}
    };
    int main()
    {
    	cout << "code: derived2 d1;\n";
    	derived2 d1;
    	cout << "code: d1=derived2(1,2,3);\n";
    	d1=derived2(1,2,3);
    	cout<<"Executing getch()..."<<endl;
    	getch();
    	return(0);
    }
    this is the output:
    Code:
    code: derived2 d1;
    default derive2 constructor
    code: d1=derived2(1,2,3);
    Constructing base1...1
    Constructing derived1...2
    Constructing derived2...3
    Destroying derived2...
    Destroying derived1...
    Destroying base1...
    Executing getch()...
    Destroying derived2...
    Destroying derived1...
    Destroying base1...
    Press any key to continue

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Thanks max_unreg. That explanation helped a lot.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Originally posted by max_unreg

    you didn't chain the constructors, so no base class constructor gets called.
    That's not true. The default constructors of the base class get called without explicit chaining, but they don't output anything either.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constructors
    By MarlonDean in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2008, 02:21 AM
  2. Doubt abt Storage!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 04-21-2006, 06:30 AM
  3. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 08:20 PM
  4. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 07:21 PM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21