1 #include<iostream>
2
3 using namespace std;
4 //解析和析构函数的调用
5 class Test
6 {
7 public:
8 Test()
9 {
10 cout<<"我是无参的构造函数\n\n";
11 }
12 Test (int i)
13 {
14 t_a=i;
15 cout<<"i= "<<i<<endl;
16 cout<<"哎呀,人家是形参为1的构造函数、\n\n";
17 }
18 Test (int a,int b)
19 {
20 cout<<"a= "<<a<<" b= "<<a<<endl;
21 cout<<"我是两个参数的构造函数、\n\n";
22 }
23 Test(const Test &obj)
24 {
25 cout<<"我是赋值构造函数又叫拷贝构造函数\n\n";
26 }
27 ~Test()
28
29 {
30 cout<<"我是析构函数\n\n";
31 }
32
33 private:
34 int t_a;
35 int t_b;
36 protected:
37 };
38 int main()
39 {
40 cout<<"Test t1 ====>";
41 Test t1;//无参函数的赋值
42 cout<<endl;
43 cout<<"Test t2(1,3) =========>";
44 Test t2(1,3);//两个形参的函数调用
45 cout<<endl;
46
47 cout<<"Test t3=(1,3)========>";
48 Test t3=(1,3);//一个形参的调用
49 cout<<endl;
50 cout<<"Test t4=3========>";
51 Test t4=3;//一个形参的调用
52 cout<<endl;
53 cout<<"Test t5(3)========>";
54 Test t5(3);//一个形参的调用
55 cout<<endl;
56
57 cout<<"Test t6=Test(1)========>";
58 Test t6=Test(1);//匿名对象,这个较为特殊,右边括号内几个值就调用几个形参的函数。
59 cout<<endl;
60
61 cout<<"Test t7=Test(1,3)========>";
62 Test t7=Test(1,3);//匿名对象,这个较为特殊,右边括号内几个值就调用几个形参的函数。
63 cout<<endl;
64
65
66 cout<<" hello world \n";
67 system("pause");
68 }