自定义类类型之间互相转换
1 /* 自定义类类型之间互相转换 */
2
3 #include<iostream>
4
5 using namespace std;
6
7 // complex -> area
8 // area -> complex
9
10 class area
11 {
12 public:
13 double x;
14 double y;
15
16 area()
17 {
18
19 }
20
21 area(double a,double b):x(a),y(b)
22 {
23
24 }
25 };
26
27 class complex
28 {
29 public:
30 int x;
31 int y;
32
33 complex()
34 {
35
36 }
37
38 complex(const area & ar)// area 转换为本类型
39 {
40 cout << "构造" << endl;
41 this->x = ar.x;
42 this->y - ar.y;
43 }
44
45 void operator =(const area & ar)// area 转换为本类型
46 {
47 cout << "重载" << endl;
48 this->x = ar.x;
49 this->y - ar.y;
50 }
51
52 operator area()// 完成了本类型转换为area
53 {
54 area temp;
55 temp.x = this->x;
56 temp.y = this->y;
57 return temp;
58 }
59 };
60
61 void main()
62 {
63 area ar(10.9,8.9);
64 //complex com1 = ar;// 调用构造
65
66 complex com1;
67 com1 = ar;// 调用重载
68
69 cout << com1.x << " " << com1.y << endl;
70
71 ar = com1;
72
73 cout << ar.x << " " << ar.y << endl;
74
75 cin.get();
76 }
长风破浪会有时,直挂云帆济沧海
posted on 2015-06-08 09:44 Dragon-wuxl 阅读(210) 评论(0) 收藏 举报
浙公网安备 33010602011771号