一道简单C++问题,急!急!急!

2024-12-23 01:21:43
推荐回答(2个)
回答1:

// 问题出在纯虚函数重载后函数必须一模一样
#include
using namespace std;
class Shape //定义抽象类
{
public:
virtual double Area()=0;
};

class Triangle:public Shape //派生三角形类
{
public:
Triangle(double h,double w){H=h;W=w;}
double Area() {return H*W*0.5;}
private:
double H,W;
};

class Rectangle:public Shape //派生矩形类
{
public:
Rectangle(double h,double w){H=h;W=w;}
double Area() {return H*W;}
private:
double H,W;
};

void fun(Shape *pt)
{
pt->Area();
}

void main()
{
Shape *p;
Triangle san(5,6);
Rectangle ju(6,8);
p=&san;
fun(p);
p=&ju;
fun(p);
}

回答2:

有错误吗?没有看出来