#include <iostream>
using namespace std;
class Calculator
{
public:
void stev1(int v1)
{
this->value1 = v1;
}
void stev2(int v2)
{
this->value2 = v2;
}//为私有属性赋值的接口;
int GetResult(string oper)
{
if (oper == "+") {
return value1 + value2;
}
else if(oper == "-"){
return value1 - value2;
}
}//操作;
private:
int value1;
int value2;
};
void test01()
{
Calculator c1;
c1.stev1(10);
c1.stev2(5);
cout << c1.GetResult("+") << endl;
}
int main()
{
test01();
return 0;
}
//计算器版本V1.0
版本1;
多态编写,利于扩展:
#include <iostream>
using namespace std;
//在真正的开发中,有一个开闭原则;
//对扩展开放,对修改关闭;
//用多态的方法实现计算器:
class Calculator
{
public:
int setv1(int v1)
{
return value1 = v1;
}
int setv2(int v2)
{