Some content structure

The establishment of a structure, containing Constructor

struct Point {
     int X, Y;
     // = 0 represents a value of 0 if no default value is specified by the argument to 0 treated   
    Point ( int X = 0 , int Y = 0 ): X (X), Y (Y) { }
     // x (x): initializes the value of x is x 
};

About this function there is another way

point(int x=0,int y=0) { 
    this->x=x; 
    this->y=y; 
} 

Structure and calculation

point operator + (const point& A,const point &B) {
    return point(A.x+B.x,A.y+B.y);
}

Stream output structure, it can be used to output a cout << p structure

ostream& operator << (ostream &out,const point& p) {
    out<<p.x<<" "<<p.y;
    return out;
}

When the structure is defined as: point a, b (1,2); 

At this time, each call point (), and the point (1,2)

It gives a complete code eg

#include <iostream>
using namespace std;

struct point {
    int x,y;
    point(int x=0,int y=0):x(x),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& p) {
    out<<p.x<<" "<<p.y;
    return out;
}
int main( ) {
    point a,b(1,2);
    a.x=3;
    cout<<a+b<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wronin/p/11260890.html