Kindly Explain the reason behind this?

This is a discussion on Kindly Explain the reason behind this? within the C++ Programming forums, part of the General Programming Boards category; hello, When i run the below code i am getting the value of copyconscount as 2 and not 3 Please ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Kindly Explain the reason behind this?

    hello,
    When i run the below code i am getting the value of copyconscount as 2 and not 3 Please tell me why is it happening?the copy constructor is called thrice in the program and still why is it showing only 2??


    Code:
    #include <iostream.h>
    class room
    {
      int width;
      int height;
      static int copyconscount;
      public:
    	  room()
    	  {
    		  width=12;
    		  height=8;
    	  }
    	  room(room &r)
    	  {
    		 width=r.width;
    		 height=r.height;
    		 copyconscount++;
    	  }
    	  void dispcopyconscount()
    	  {
    		  cout<<copyconscount;
    		  cout<<endl<<height;
    		  cout<<endl<<width;
    	  }
    };
    int room::copyconscount;
    int main(void)
    {
     room objroom1;
     room objroom2(objroom1);
     room objroom3=objroom1;
     room objroom4;
     objroom4=objroom3;
     objroom4.dispcopyconscount();
    
     return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    You only invoke the copy constructor twice, i.e.,
    Code:
     room objroom2(objroom1);
     room objroom3=objroom1;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    So what's the problem laserlight??just see the below code

    Code:
    int main(void)
    {
     clrscr();
     room objroom1;
     room objroom2(objroom1);
     room objroom3=objroom1;
     room objroom4=objroom3;
     objroom4=objroom3;
     objroom2.dispcopyconscount();
     return 0;
    
    }
    i am stilling calling the constructor twice but now its printing 3??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    i am stilling calling the constructor twice but now its printing 3??
    No, you are now invoking the copy constructor three times with:
    Code:
    room objroom2(objroom1);
    room objroom3=objroom1;
    room objroom4=objroom3;
    You are also invoking the copy assignment operator with:
    Code:
    objroom4=objroom3;
    And of course the default constructor with:
    Code:
    room objroom1;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  2. One Easy" C " Question. Please Solve and Explain.
    By RahulDhanpat in forum C Programming
    Replies: 18
    Last Post: 03-24-2008, 01:39 PM
  3. Please Explain me few terms that i have listed in here.
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 07:20 AM
  4. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 05:23 AM
  5. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM

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