Tuesday, 20 April 2021

Constructor in C++ - 1

 #include<iostream.h>

#include<conio.h>

class A

{

public:

int a , b;

A()

{

a=5;

b=6;

}

A(int x,int y)

{

a=x;

b=y;

}

A(A &k)

{

cout<<"Copy Constructor"<<endl;

cout<<k.a+k.b<<endl;

}

};

int main()

{

clrscr();

A ob;

A ob3=A();

//ob.A();

A ob1(8,9);    //implicit

A ob2=A(10,15);          //Explicit

cout<<ob2.a+ob2.b<<endl;  //25

cout<<ob.a+ob.b<<endl;  //11

cout<<ob1.a+ob1.b<<endl;  //17

cout<<ob3.a+ob3.b<<endl;  //11

A ob4(ob);   //11

A ob5=ob2;    //25

A ob6=A(ob1);    //17

//cout<<ob4.a+ob4.b<<endl;   //11

//cout<<ob5.a+ob5.a<<endl;   //25

getch();

return 0;

}

No comments:

Post a Comment