Search This Blog

Wednesday 24 February 2016

Write a program to create a copy constructor. A constructor should be created, then a second constructor should be created which should have values of the previous constructor.

Practical 19


 #include<iostream>
using namespace std;

class Gajjar
{
    int id;
    public:
    Gajjar()
    {}
    Gajjar(int a)
    {
        id = a;
    }
    Gajjar(Gajjar &x)
    {
        id = x.id;
    }
    void display()
    {
        cout<<id;
    }
};

int main()
{
    Gajjar A(100);
    Gajjar B(A);
    GajjarC=A;
    GajjarD;
    D=A;
    cout<<"\n Id of A: ";
    A.display();
    cout<<"\n Id of B: ";
    B.display();
    cout<<"\n Id of C: ";
    C.display();
    cout<<"\n Id of D: ";
    D.display();
    return 0;
}


output:

A : 100
B : 100
C : 100
D : 100

No comments:

Post a Comment