Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Default copy constructor in c++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 653
    Comment on it

    A copy constructor is a special type of constructor by using which we can initialize the data members of a newly created object by existing objects. The compiler provides a default copy constructor. 
    Syntax of calling a copy constructor is:

    complex c1 (c2); 

    where complex is name of class, C1 is new object and C2 is existing object.


    Program:

    #include<iostream.h>
    #include<conio.h>
    class complex
    {
    int r, i;
    public: complex(int a, int b)
       void show();
    }
     
    complex : : complex(int a, int b)
    {r=a ; i=b;}
    void complex : : show()
        {cout<<r<<"+i"<<i<<endl;}
    void main()
    {
    int x,y;
    clrscr();
    cout<<"Enter real part:"; cin>>x;
    cout<<"Enter imag part:": cin>>y;
    complex C1 (C2);
    cout<<"C2=";
    C1.show();
    getch();
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: