Counter类(运算符重载练习)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

ProblemA:Counter类(运算符重载练习)TimeLimit:1SecMemoryLimit:128MBSubmit:739Solved:516[Submit][Status][WebBoard]Description编写一个计算器Counter类,对其重载运算符“+”。用下面的主程序测试,使测试结果正确。intmain(){CountervarOne(2),varTwo(4),varThree;varThree=varOne+varTwo;coutvarOne:varOne.getValue()endl;coutvarTwo:varTwo.getValue()endl;coutvarThree:varThree.getValue()endl;return0;}InputOutputvarOne:2varTwo:4varThree:6源代码:#includeiostreamusingnamespacestd;classCounter{public:Counter(doubleVal=0.0){itsVal=Val;}doublegetValue()const{returnitsVal;}Counteroperator+(constCounter&v){returnCounter(itsVal+v.getValue());}private:doubleitsVal;};intmain(){CountervarOne(2),varTwo(4),varThree;varThree=varOne+varTwo;coutvarOne:varOne.getValue()endl;coutvarTwo:varTwo.getValue()endl;coutvarThree:varThree.getValue()endl;return0;}

1 / 2
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功