#include <iostream> using namespace std; class A { public: A(double _data = 0.0):data(_data){} A& operator = (const A& rhs) { data = rhs.data; return *this; } friend A operator + (const A& lhs,const A& rhs); friend A operator - (const A& lhs,const A& rhs); friend A operator * (const A& lhs,const A& rhs); friend A operator + (const A& lhs,double rhs); friend A operator + (double lhs,const A& rhs); friend A operator * (const A& lhs,double rhs); friend A operator * (double lhs,const A& rhs); friend A operator - (const A& lhs,double rhs); friend A operator - (double lhs,const A& rhs); friend ostream& operator << (ostream& fout,A& a); // A& operator += (const A& rhs); // A& operator -= (const A& rhs); // A& operator *= (const A& rhs); private: double data; }; A operator + (const A& lhs,const A& rhs) { A res(0); res.data = lhs.data + rhs.data; return res; } A operator - (const A& lhs,const A& rhs) { A res(0); res.data = lhs.data - rhs.data; return res; } A operator * (const A& lhs,const A& rhs) { A res(0); res.data = lhs.data * rhs.data; return res; } A operator + (const A& lhs,double rhs) { A res(0); res.data = lhs.data + rhs; return res; } A operator + (double lhs,const A& rhs) { A res(0); res.data = lhs + rhs.data; return res; } A operator * (const A& lhs,double rhs) { A res(0); res.data = lhs.data * rhs; return res; } A operator * (double lhs,const A& rhs) { A res(0); res.data = lhs * rhs.data; return res; } A operator - (const A& lhs,double rhs) { A res(0); res.data = lhs.data - rhs; return res; } A operator - (double lhs,const A& rhs) { A res(0); res.data = lhs - rhs.data; return res; } ostream& operator << (ostream& fout,A& a) { fout << a.data ; return fout; } int main(int argc, char* argv[]) { A a(2.3); A b(1.2); A d(3.4); A c; c = a + b + d; c=a+b; c=a+1.0; c=a-b; c=a-1.0; c=a*b; c=a*1.0; cout << c << endl; c=1.0+2.0*a*a-3.0*a*b; cout << c << endl; return 0; }
原文链接: https://www.cnblogs.com/speedmancs/archive/2011/06/09/2076873.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/26937
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!