c++类 构造函数 析构函数 拷贝构造函数

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef int* intpt;
 4 int N=1;
 5 class mybox{
 6     public:
 7         int name;
 8         friend class boxfri;
 9         friend void WatchIt(mybox box);
10         mybox(int len);
11         mybox(const mybox &abox);
12         ~mybox();
13     protected:
14         intpt l,w,h;
15     private:
16         int MyTh;
17     
18 };
19 class boxfri{
20     
21 };
22 /*class boxpub: public mybox{
23 
24 };
25 class boxpri: private mybox{
26 
27 };
28 class boxpro: protected mybox{
29 
30 };*/
31 mybox:: mybox(int len){
32     name=N;++N;
33     MyTh=12345;
34     l=new int; w=new int; h=new int;
35     *l=len; *w=len; *h=len;
36     cout<<"We have created a box:"<<"box"<<name<<endl;
37     
38 }
39 mybox:: mybox(const mybox &abox){
40     l=new int; w=new int; h=new int;
41     *l=*abox.l;*w=*abox.w;*h=*abox.h;
42     name=abox.name;MyTh=abox.MyTh;
43     cout<<"Use the copy code"<<endl;
44     
45 }
46 mybox:: ~mybox(){
47     delete l; delete w; delete h;
48     cout<<"We have destroyed a box:"<<"box"<<name<<endl;
49 } 
50 void WatchIt(mybox box){
51     cout<<"box"<<box.name<<endl;
52     cout<<*box.l<<' '<<*box.w<<' '<<*box.h<<endl;
53     cout<<box.MyTh<<endl;
54 }
55 int main(){
56     int a;scanf("%d",&a);
57     mybox box1(a);
58     WatchIt(box1);
59     cout<<N<<endl;
60     return 0;
61 }
View Code

结果如下:

 

posted @ 2020-08-30 16:01  鲸头鹳  阅读(103)  评论(0编辑  收藏  举报