CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2016
    Posts
    1

    Lightbulb Copy constructor

    I'm confused how each object calls the member function. can anyone give me step by step working of this code
    Code:
    #include <iostream>
    
    using namespace std;
    
    class add 
    {
    	int num1, num2, sum;
    	public:
    	add()
    	{
    		 cout<<" constructor without parameters ";
    		 num1 = '\0';
    		 num2='\0';
    		 sum='\0';
    		 
    	}
    	add(int s1, int s2)
    	{
    		cout<<"\n constructor with parameters";
    		num1= s1;
    		num2= s2;
    		sum= '\0';
    		
    	}
    	add(add &a)
    	{
    		cout<<"\ncopy const";
    		num1= a.num1;
    		num2 = a.num2;
    		sum= '\0';
    		
    	}
    	
    	void getdata()
    	{
    		cout<<"\nenter data";
    		cin>>num1>>num2;
    		
    	}
    	void addition(add b)
    	{
    		sum = num1+num2+ b.num1 + b.num2 ;
    	
    	}
    	add addition()
    	{
    		add a(5,6);
    		sum = num1+ num2 + a.num1 + a.num2;
    		
    	}
    	void putdata()
    	{
    		cout<<"\nthe  numbers are";
    		cout<<num1<<'\t'<< num2 ;
    		cout<< "\nthe sum of the numbers are"<<sum;
    		
    	}
    };
    
    int main()
    {
    	add a,b(10,20), c(b);
    	a.getdata();
    	a.addition();
    	b=c.addition();
    	c.addition();
    	cout<<"\nobj a";
    	a.putdata();
    	cout<<"\n obj b ";
    	b.putdata();
    	cout<<"\n obj c ";  
    	c.putdata();
    	
    }
    p.s the output of this program is absurd but its main motive is to understand where the copy constructor has been invoked.
    also can you explain what is being done in this function block

    Code:
    add(add &a)
    	{
    		cout<<"\ncopy const";
    		num1= a.num1;
    		num2 = a.num2;
    		sum= '\0';
    		
    	}
    thanks in advance.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Copy constructor

    Firstly, this code doesn't compile.
    Code:
    add addition()
    	{
    		add a(5,6);
    		sum = num1+ num2 + a.num1 + a.num2;
    	}
    This function returns a type of add, but it doesn't have a return statement. Consider
    Code:
    add addition()
    	{
    		add a(5,6);
    		sum = num1+ num2 + a.num1 + a.num2;
                    return *this;		
    	}
    The point of a copy constructor is to produce a new class instance that has the same contents as the original. So
    Code:
    add(add &a)
    	{
    		cout<<"\ncopy const";
    		num1= a.num1;
    		num2 = a.num2;
    		sum= '\0';
    	}
    produces a copy that isn't a copy as sum is 0. Consider
    Code:
    add(const add &a)
    	{
    		cout<<"\ncopy const";
    		num1= a.num1;
    		num2 = a.num2;
    		sum= a.sum;
    	}
    also the copy constructor usually has a const parameter so that a non-modifiable argument can be passed.

    can anyone give me step by step working of this code
    A constructor creates a new instance of a class and should initialise all non-static class variables. A copy constructor creates a new class instance as a copy of an existing class instance (note issues with using dynamic memory here - but that's a different question). A copy constructor is automatically called when a copy of a class instance is required - such as passing a class instance by value as a function parameter.

    The best way to understand code and to determine how it works is to use the debugger to step through the code line by line. If you do this and then still have specific questions, we'll be able to provide more guidance.
    Last edited by 2kaud; November 27th, 2016 at 04:11 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured