[C++] 纯文本查看 复制代码 #include<iostream>
using namespace std;
#include<string>
class jsj//普通类
{
public:
int a, b;
void get(int a,int b)
{
this->a = a;
this->b = b;
}
int jg(string fh)
{
if (fh == "+")
{
return a + b;
}
else if (fh == "-")
{
return a - b;
}
else if (fh == "*")
{
return a * b;
}
else if (fh == "/")
{
return a / b;
}
}
};
void test01()
{
jsj j1;
j1.get(10, 20);
cout << "+法 " << j1.jg("+") << " -法 " << j1.jg("-") << " *法 " << j1.jg("*") << " /法 " << j1.jg("/") <<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
普通类不方便后面维护
[C++] 纯文本查看 复制代码
#include<iostream>
using namespace std;
#include<string>
class absjsj//多态实现计算机
{
public:
virtual int js()
{
return 0;
}
int a, b;
};
class add:public absjsj
{
public:
int js()
{
return a + b;
}
};
class sub :public absjsj
{
public:
int js()
{
return a - b;
}
};
class ride :public absjsj
{
public:
int js()
{
return a * b;
}
};
class exc :public absjsj
{
public:
int js()
{
return a / b;
}
};
void test01()
{
absjsj* abs = new add;
abs->a = 10;
abs->b = 5;
cout <<"加法"<< abs->js() << endl;
delete abs;
abs = new sub;
abs->a = 10;
abs->b = 3;
cout << "减法" << abs->js() << endl;
delete abs;
abs = new ride;
abs->a = 9;
abs->b = 8;
cout << "乘法" << abs->js() << endl;
delete abs;
abs = new exc;
abs->a = 8;
abs->b = 4;
cout << "除法" << abs->js() << endl;
delete abs;
}
int main()
{
test01();
system("pause");
return 0;
}
多态更方便实现后期的维护。 |