C++结构体

来源:互联网 发布:英国知山大学 编辑:程序博客网 时间:2024/06/05 14:30
#include <iostream>using namespace std;struct Point{    int x,y;    Point(int x=0,int y=0){        this->x=x;//this是指向当前对象的指针        this->y=y;    }//构造函数};Point operator + (const Point &A,const Point &B){//定义加法    return Point(A.x+B.x,A.y+B.y);}ostream &operator << (ostream &out,const Point &A){//定义结构体的流输入方式    cout << "(" << A.x << "," << A.y << ")" ;    return out;}int main(){    Point a,b(2,3);    cout << a+b << endl;    return 0;}

输入结果:
(2,3)

C++中的结构体可以有一个或多个构造函数,在声明变量时调用。
C++中的函数(不止是构造函数)参数可以拥有默认值。
在C++结构体的成员函数中,this是指向当前对象的指针。

0 0
原创粉丝点击