fork(1) download
  1. #include <iostream>
  2. #include <string.h>
  3.  
  4. using namespace std;
  5.  
  6. class Cwin
  7. {
  8. private:
  9. char id,*title;
  10. public:
  11. Cwin(char i='D',char *text="default windows")
  12. {
  13. id=i;
  14. cout<<"件構元被呼叫....."<<endl;
  15. title=new char[strlen(text)+1];
  16. strcpy(title,text);
  17. }
  18. Cwin(const Cwin &win) //拷貝運算元
  19. {
  20. cout<<"拷貝建構元被呼叫...."<<endl;
  21. id=win.id;
  22. title=win.title;
  23. strcpy(title,win.title);
  24. }
  25. ~Cwin()
  26. {
  27. delete [] title;
  28. }
  29. void show()
  30. {
  31. cout<<"Windows= "<<id<<" : "<< title <<endl;
  32. }
  33. };
  34. int main()
  35. {
  36. Cwin *ptr1=new Cwin('A',"主要的視窗");
  37. Cwin *ptr2=new Cwin(*ptr1);
  38.  
  39. ptr1->show();
  40. ptr2->show();
  41.  
  42. delete ptr1;
  43. cout<<"刪除prt1之後..."<<endl;
  44.  
  45. ptr2->show();
  46. delete ptr2;
  47. return 0;
  48. }
  49.  
Runtime error #stdin #stdout 0.01s 3884KB
stdin
Standard input is empty
stdout
件構元被呼叫.....
拷貝建構元被呼叫....
Windows= A : 主要的視窗
Windows= A : 主要的視窗
刪除prt1之後...
Windows= A :