测试面向对象编程的相关机制
测试面向对象编程的相关机制:通过类模版定义点,每个点有x、y两个座标,通过构造函数建立对象,重载>>运算符使这个点向右移动(x座标增加)。
2021-05-23 07:36
程序代码:#include <utility>
template<typename T>
struct point
{
explicit point( T x_=T(), T y_=T() ) : x(std::move(x_)), y(std::move(y_))
{
}
template<typename U>
point operator>>( U offset ) const
{
point pt( *this );
pt.x += offset;
return pt;
}
T x;
T y;
};
#include <iostream>
using namespace std;
int main( void )
{
point pt( 1, 2 );
cout << pt.x << endl;
point tmp = pt>>3;
cout << tmp.x << endl;
}
2021-05-23 10:20