一个计算机类,想用一下纯虚函数写一下多态,但不管输入什么结果都是0呢?

#include<iostream>
using namespace std;
class Calculator{
	public:
		virtual int GetRusult(){
			return 0;
		}
	int m_num1;
	int m_num2;
};
class addCalculator:public Calculator{
	public:
		int GetResult(){
			return m_num1+m_num2;
		}
};
class subCalculator:public Calculator{
	public:
		int GetResult(){
			return m_num1 - m_num2;
		}
};
class mulCalculator:public Calculator{
	public:
		int GetResult(){
			return m_num1*m_num2;
		}
};
class divCalculator:public Calculator{
	public:
		int GetResult(){
			return m_num1/m_num2;
		}
};
void test01(){
	Calculator *p= new mulCalculator;
	p->m_num1=100;
	p->m_num2=300;
	cout<<p->GetRusult()<<endl;
	delete p;
}
int main(){
	test01();
}

 

查看全部
hacker_zrq
hacker_zrq
2020/11/26 13:52
  • c++
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复