Thread: Kindly Explain the reason behind this?

  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
    28,413
    You only invoke the copy constructor twice, i.e.,
    Code:
     room objroom2(objroom1);
     room objroom3=objroom1;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 08: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