class_8:函数重载和运算符重载

函数重载:参数个数、类型,顺序不同,不能仅通过函数返回值重载

运算符重载:

#include 

using namespace std;
class Point{
public:
    //成员数据
    int   x;
    int   y;
    Point operator +(Point ptmp);

};


 Point Point:: operator +(Point ptmp)
{
    Point p;
    p.x = x + ptmp.x;
    p.y = y + ptmp.y;
    return p;

 }
int main()
{
    cout << "Hello World!" << endl;
    Point p1;
    p1.x = 3;
    p1.y = 4;

    Point p2;
    p2.x = 5;
    p2.y = 6;

    Point p3 = p1 + p2;  //两个对象相加("+")的运算符
    cout << "p3.x=" <class_8:函数重载和运算符重载_第1张图片

你可能感兴趣的:(QT,c++,算法,开发语言)