在C++中:转换构造函数与类型转换操作详解
最编程
2024-07-26 22:47:16
...
1. #include<iostream>
2. using namespace std;
3. class Solid
4. {
5. public:
6. Solid(int x,int y,int z):_x(x),_y(y),_z(z)
7. {
8. ;
9. }
10. void show()
11. {
12. cout<<"三维坐标:"<<_x<<","<<_y<<","<<_z<<endl;
13. }
14. friend class Point;
15. private:
16. int _x,_y,_z;
17. };
18. class Point
19. {
20. public:
21. Point(int x,int y):_x(x),_y(y)
22. {
23. ;
24. }
25. Point(const Solid& ptr)
26. {
27. _x=ptr._x;
28. _y=ptr._y;
29. }
30. void show()
31. {
32. cout<<"平面坐标:"<<_x<<","<<_y<<endl;
33. }
34. private:
35. int _x,_y;
36. };
37. int main()
38. {
39. cout<<"原始坐标"<<endl;
40. Point p(1,2);
41. p.show();
42. Solid s1(3,4,5);
43. s1.show();
44. cout<<"三维坐标转化为二维坐标"<<endl;
45. p=s1;//如果没有转换类型构造函数,不同类进行赋值是错误的
46. p.show();
47. return 0;
48. }
49.
50. 结果为:
51. 原始坐标
52. 平面坐标:1,2
53. 三维坐标:3,4,5
54. 三维坐标转化为二维坐标
55. 平面坐标:3,4
上一篇: 在MySQL中玩转行列转换:行变列与列变行的操作详解
下一篇: 转义字符