【TOJ 5240】C++实验:虚函数

描述

用C++实现一个形状类和矩形类,并完成求面积函数。

主函数里的代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

int main()
{
	int w, h;
	while(cin>>w>>h)
	{
		Shape* p = new Rectangle(w, h);
		cout<<p->Area()<<endl;
		delete p;
	}
	return 0;
}

输入

输入数据有多组,每组占一行,每行两个正整数,分别表示矩形的长和宽。

输出

每组输出一个正整数,表示矩形面积。

样例输入

2 3
5 6

样例输出

6
30

扫描二维码关注公众号,回复: 901001 查看本文章
#include<iostream>
using namespace std;
class Shape{
public:
    virtual int Area(){}
};
class Rectangle:public Shape{
public:
    int a,b;
    Rectangle(int a=0,int b=0):a(a),b(b){}
    int Area()
    {
        return a*b;
    }
};
int main()
{
    int w, h;
    while(cin>>w>>h)
    {
        Shape* p = new Rectangle(w, h);
        cout<<p->Area()<<endl;
        delete p;
    }
    return 0;
}

 

猜你喜欢

转载自www.cnblogs.com/kannyi/p/9052731.html