您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 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;}
本文标题:Counter类(运算符重载练习)
链接地址:https://www.777doc.com/doc-5468340 .html