纯虚函数与基类指针数组的应用

时间:2021-04-01 18:57:55

题目内容:

定义抽象基类Shape, 其中纯虚函数printName()输出几何图形的名称和相应的成员数据、纯虚函数printArea()计算几何图形的面积。并由Shape类派生出5个派生类:Circle(圆形),数据成员为半径、Square(正方形) ,数据成员为边长、Rectangle(长方形) ,数据成员为长和宽、Trapezoid(梯形) ,数据成员为上底、下底和高、Triangle(三角形) ,数据成员为底和高。测试过程,定义一个指向基类的指针数组,使其每个元素指向一个动态产生的派生类对象,分别调用相应的成员函数显示各个几何图形的属性及面积,最终输出总面积值。


输入格式:

依次输入圆半径、正方形边长、长方形长宽、梯形上底下底和高、三角形底边和高,均为实数。


输出格式:

请参考输出样例,建议直接复制样例中的部分文字粘贴进自己的代码。圆周率取 3.14159

注意输出中的标点符号、空格。

输入样例:

10
5
2 4
1 2 3
4 3


输出样例:

圆:半径=10,面积:314.159
正方形:边长=5,面积:25
长方形:长=2,宽=4,面积:8
梯形:上底=1,下底=2,高=3,面积:4.5
三角形:底边=4,高=3,面积:6
总面积:357.659


时间限制:500ms内存限制:32000kb
#include<iostream>  
using namespace std;  
#define PI 3.14159  
class Shape  
{  
    public:  
    virtual void printName()=0;  
    virtual double printArea()=0;  
};  
class Circle:public Shape  
{  
    double r;  
    public:  
        Circle(double r)  
        {  
            this->r=r;  
        }  
        virtual void printName()  
        {  
            cout<<"圆:"<<"半径="<<r<<",";  
        }  
        virtual double printArea()  
        {  
            return PI*r*r;  
        }  
};  
class Square:public Shape  
{  
    double a;  
    public:  
        Square(double a)  
        {  
            this->a=a;  
        }  
        virtual void printName()  
        {  
            cout<<"正方形:"<<"边长="<<a<<",";  
        }  
        virtual double printArea()  
        {  
            return a*a;  
        }  
};  
class Rectangle:public Shape  
{  
    double a,b;  
    public:  
        Rectangle(double a,double b)  
        {  
            this->a=a;  
            this->b=b;  
        }  
        virtual void printName()  
        {  
            cout<<"长方形:"<<"长="<<a<<","<<"宽="<<b<<",";  
        }  
        virtual double printArea()  
        {  
            return a*b;  
        }  
};  
class Trapezoid:public Shape  
{  
    double m,n,h;  
    public:  
        Trapezoid(double m,double n,double h)  
        {  
            this->m=m;  
            this->n=n;  
            this->h=h;  
        }  
        virtual void printName()  
        {  
            cout<<"梯形:"<<"上底="<<m<<","<<"下底="<<n<<","<<"高="<<h<<",";  
        }  
        virtual double printArea()  
        {  
            return (m+n)*h/2;  
        }  
};  
class Triangle:public Shape  
{  
    double m,h;  
    public:  
        Triangle(double m,double h)  
        {  
            this->m=m;  
            this->h=h;  
        }  
        virtual void printName()  
        {  
            cout<<"三角形:"<<"底边="<<m<<","<<"高="<<h<<",";  
        }  
        virtual double printArea()  
        {  
            return m*h/2;  
        }  
};  
int main()  
{  
    double a1;  
    double b1;  
    double c1,c2;  
    double d1,d2,d3;  
    double e1,e2;  
    cin>>a1;  
    cin>>b1;  
    cin>>c1>>c2;  
    cin>>d1>>d2>>d3;  
    cin>>e1>>e2;  
      
    Shape *s[5];  
    Circle circle1(a1);  
    Square square1(b1);  
    Rectangle rele1(c1,c2);  
    Trapezoid td1(d1,d2,d3);  
    Triangle tre1(e1,e2);  
    s[0]=&circle1;  
    s[1]=&square1;  
    s[2]=&rele1;  
    s[3]=&td1;  
    s[4]=&tre1;  
    s[0]->printName();  
    cout<<"面积:"<<s[0]->printArea()<<endl;  
    s[1]->printName();  
    cout<<"面积:"<<s[1]->printArea()<<endl;  
    s[2]->printName();  
    cout<<"面积:"<<s[2]->printArea()<<endl;  
    s[3]->printName();  
    cout<<"面积:"<<s[3]->printArea()<<endl;  
    s[4]->printName();  
    cout<<"面积:"<<s[4]->printArea()<<endl;  
      
    cout<<"总面积:"<<s[0]->printArea()+s[1]->printArea()+s[2]->printArea()+s[3]->printArea()+s[4]->printArea()<<endl;  
    return 0;  
}