利用多态的方式写一个简易的计算器
#include<iostream>
using namespace std;
#include<string>
class Abstract
{
public:
virtual int getresult()
{
return 0;
}
int m_a;
int m_b;
};
class addAbstract: public Abstract
{
public:
int getresult()
{
return (m_a + m_b);
}
};
class subAbstract: public Abstract
{
public:
int getresult()
{
return (m_a - m_b);
}
};
class mulAbstract: public Abstract
{
public:
int getresult()
{
return m_a * m_b;
}
};
void test1()
{
Abstract *abc= new addAbstract;
abc->m_a=10;
abc->m_b=10;
cout<<abc->m_a<<"+"<<abc->m_b<<"="<<abc->getresult()<<endl;
delete abc;
abc= new subAbstract;
abc->m_a=100;
abc->m_b=10;
cout<<abc->m_a<<"-"<<abc->m_b<<"="<<abc->getresult()<<endl;
delete abc;
abc= new mulAbstract;
abc->m_a=10;
abc->m_b=10;
cout<<abc->m_a<<"*"<<abc->m_b<<"="<<abc->getresult()<<endl;
delete abc;
}
int main()
{
test1();
system("pause");
return 0;
}