一个C++编程题

来源:百度知道 编辑:UC知道 时间:2024/06/30 04:56:28
定义一个图形类, 其中有保护类型的成员数据:高度和宽度,一个公有的构造函数.由该图形类建立两个派生类:矩形类和等腰三角形类.在每个派生类中都包括一个函数Area(),分别用来计算矩形和等咬三角形的面积.

若回答令我满意,按上面的要求并且直接能运行正确,我还追加积分.

应该用虚函数吧……
#include <cstdlib>
#include <iostream>
using namespace std;
class Shape
{
protected:
float m_height;//高
float m_width;//宽
float m_area;//面积

public:
Shape() {m_height=0.00;m_width=0.00;m_area=0.00; }
float getHeight() { return m_height; }
float getWidth() { return m_width; }
float getArea() { return m_area; }
void setHeight( float height ) { m_height = height; }
void setWidth( float width ) { m_width = width; }
virtual void procArea( void ) {};
};

class Triangle:public Shape
{
public:
Triangle(){};
void procArea( void ) { m_area = m_width*m_height/2; }
};

class Recangle:public Shape
{
public:
Recangle(){};
void procArea( void ) { m_area = m_width*m_height; }
};

int main(int argc, char *argv[])
{
Shape *a = new Tri