tgoop.com/WorldCode_Cpp/2723
Last Update:
♨️ مثالی از تابع سازنده، تابع سازنده کپی و مخرب :
#include <iostream>
using namespace std;
class myclass{
int a, b;
int *c; //single
int *d; //array
public:
myclass(const myclass &A) //copy constructor
{
a=A.a;
b=A.b;
c=new int;
*c=A.c[0];
d=new int[5];
for(int i=0;i<5;i++)
d[i]=A.d[i];
}
myclass(int a1,int b1 , int c1,int d0,int d1,int d2,int d3,int d4) //constructor with value
{
a=a1;
b=b1;
c=new int;
*c=c1;
d=new int[5];
d[0]=d0;
d[1]=d1;
d[2]=d2;
d[3]=d3;
d[4]=d4;
}
myclass() //constructor without value
{
a=1;
b=2;
c=new int;
*c=3;
d=new int[5];
d[0]=11;
d[1]=12;
d[2]=13;
d[3]=14;
d[4]=15;
}
~myclass() //destructor
{
delete c;
delete [] d;
cout<<"Destructor"<<endl; //message for show when destructor is run
}
void set(int a1,int b1 , int c1,int d0,int d1,int d2,int d3,int d4)
{
a=a1;
b=b1;
*c=c1;
d[0]=d0;
d[1]=d1;
d[2]=d2;
d[3]=d3;
d[4]=d4;
}
void print()
{
cout<<a<<endl;
cout<<b<<endl;
cout<<*c<<endl;
cout<<d[0]<<endl;
cout<<d[1]<<endl;
cout<<d[2]<<endl;
cout<<d[3]<<endl;
cout<<d[4]<<endl;
cout<<endl; //space
}
};
void Des()
{
myclass D;
}
int _tmain(int argc, _TCHAR* argv[])
{
myclass A(10,20,30,0,1,2,3,4);
myclass B=A;
myclass C;
A.set(100,200,300,5,6,7,8,9);
A.print();
B.print();
C.print();
Des(); //this function just for show destructor running after compiler pass block {}
system("pause");
return 0;
}
☘ کانالی برای برنامهنویسان
🆔 @WorldCode_Cpp
BY برنامه نویسی | ++C
Share with your friend now:
tgoop.com/WorldCode_Cpp/2723