首页 > 其他 > 详细

结构体

时间:2016-08-23 11:19:57      阅读:270      评论:0      收藏:0      [点我收藏+]

以结构体point为例,成员变量x,y,定义加法和输出流方式。

 1 #include<iostream>
 2 using namespace std;
 3 struct Point{
 4     int x, y;
 5 //  Point(int x=0, int y=0):x(x),y(y) {} // 简单的写法
 6     Point(int x = 0,int y = 0) { this->x = x; this->y = y; }
 7 };
 8 
 9 Point operator + (const Point& A,const Point& B) {
10     return Point(A.x + B.x,A.y + B.y);
11 }
12 
13 ostream& operator << (ostream &out, const Point& p) {
14     out << "(" << p.x << "," << p.y << ")";
15     return out;
16 }
17 
18 int main() {
19     Point a,b(1,2);
20     a.x = 3;
21     cout << a + b << "\n";
22     return 0;
23 }

 

结构体

原文:http://www.cnblogs.com/cyb123456/p/5798432.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!