TGViewer
برنامه نویسی | ++C برنامه نویسی | ++C @worldcode_cpp · 29.9K subscribers
Post #2723 26.8K
♨️ مثالی از تابع سازنده، تابع سازنده کپی و مخرب :

#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
More from @worldcode_cpp
  1. Sep 30, 2026🔻چند کانال پیشنهادی ما: ➡️[ @Pythoni_Developers ] ➡️ ️[ @WorldCode ] ➡️[ @AI_University ]…
  2. Sep 15, 2026💎 اگه سوال یا پرسشی تو پایتون دارید، می‌تونید تو گروه زیر مطرحش کنی👇👇 👉 @Group_PyDev �…
  3. Sep 15, 2026🏐 اگه داوطلب کنکور ارشد یا دکتری کامپیوتر یا IT و یا علوم کامپیوتر هستید، حتماً از کانال…
  4. Sep 12, 2026🎉 روز برنامه نویس‌ها مبارک 🎉 🎈 امروز روز برنامه‌نویسه، در سیستم دودویی کامپیوتری تعداد…
  5. Sep 12, 2026⏰ به درخواست کاربران، کانال تست ارشد کامپیوتر و IT ایجاد شد: ⬇️⬇️ 👉🔗 @Computer_Testes 👉…
  6. Sep 10, 2026داوطلب کنکور نبووووود؟؟؟!! 😒👆👆 👉🔗 https://t.me/+gee4un1TeQsxZjI8 👉🔗 https://t.me/+g…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →