构造函数的调用大全

 1 #include<iostream>
 2 
 3 using namespace std;
 4 //解析和析构函数的调用
 5 class Test
 6 {
 7 public:
 8     Test()
 9     {
10         cout<<"我是无参的构造函数\n";
11     }
12     Test (int i)
13     {
14         t_a=i;
15         cout<<"i= "<<i<<endl;
16         cout<<"哎呀,人家是形参为1的构造函数、\n";
17     }
18     Test (int a,int b)
19     {
20         cout<<"a= "<<a<<" b= "<<a<<endl;
21         cout<<"我是两个参数的构造函数、\n";
22     }
23     Test(const Test &obj)
24     {
25         cout<<"我是赋值构造函数又叫拷贝构造函数\n";
26     }
27     ~Test()
28 
29     {
30         cout<<"我是析构函数\n";
31     }
32 
33 private:
34     int t_a;
35     int t_b;
36 protected:
37 };
38 
39 void playobj(Test a1)
40 {
41     cout<<"看看调用了没、\n";
42 }
43 
44 Test playobj2()
45 {
46     Test b(1,2);
47     cout<<"这是第四种、\n";
48     return b;
49 }
50 int main()
51 {
52     cout<<"\n\n\nTest t1  ====>";
53     Test t1;//无参函数的赋值
54     cout<<endl;
55     cout<<"\n\n\nTest t2(1,3) =========>\n";
56     Test t2(1,3);//两个形参的函数调用
57     cout<<endl;
58 
59     cout<<"\n\n\nTest t3=(1,3)========>\n";
60     Test t3=(1,3);//一个形参的调用
61     cout<<endl;
62     cout<<"\n\n\nTest t4=3========>\n";
63     Test t4=3;//一个形参的调用
64     cout<<endl;
65     cout<<"\n\n\nTest t5(3)========>\n";
66     Test t5(3);//一个形参的调用
67     cout<<endl;
68 
69     cout<<"\n\n\nTest t6=Test(1)========>\n";
70     Test t6=Test(1);//匿名对象,这个较为特殊,右边括号内几个值就调用几个形参的函数。
71     cout<<endl;
72 
73     cout<<"\n\n\nTest t7=Test(1,3)========>\n";
74     Test t7=Test(1,3);//匿名对象,这个较为特殊,右边括号内几个值就调用几个形参的函数。
75     cout<<endl;
76 
77     //接下来是四种赋值构造函数的调用情景、
78     cout<<"\n\n\nTest e1=t1======>\n";
79     Test e1=t1;//第一种赋值构造函数调用方法
80 
81     cout<<"\n\n\nTest e2(t1)======>\n";
82     Test e2(t1);//这是第二种赋值构造函数的调用方法
83 
84     cout<<"\n\n\nplayobj(t1)======>\n";
85     playobj(t1);//第三种调用方法
86 
87     cout<<"\n\n\nTest e4=playobj2()======>\n";
88     Test e4=playobj2();//
89 
90 
91 
92 
93     cout<<" hello world \n";
94     system("pause");
95 }

 

 

转载于:https://www.cnblogs.com/xiaochige/p/6561749.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值