Write a Program to Demonstrate Copy constructor

/*
    Copy constructor Demonstrate
*/
#include<iostream>
using namespace std;

class Sample
{
   int x,y;

   public:
        Sample()
        {
            x=10;
            y=20;
        }

        Sample(Sample &s)
        {
            x=s.x;
            y=s.y;
        }

        void display()
        {
            cout<<"\n X: "<<x;
            cout<<"\n Y: "<<y;
        }
};

int main()
{
    Sample s1;

    Sample s2(s1);

    Sample s3=s2;

    Sample s4;

    s4=s3;

    cout<<"\n\n S1: ";
    s1.display();


    cout<<"\n\n S2: ";
    s2.display();

    cout<<"\n\n S3: ";
    s3.display();

    cout<<"\n\n S4: ";
    s4.display();

    return 0;
}

Previous
Next Post »