7-39 用虚函数计算各种图形的面积

7-39 用虚函数计算各种图形的面积(分数 10)

作者 潘荣江

单位 山东大学

定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle( 长方形)、Trapezoid (梯形)和Triangle (三角形),用虚函数分别计算各种图形的面积,输出它们的面积和。要求用基类指针数组,每一个数组元素指向一个派生类的对象。PI=3.14159f,单精度浮点数计算。

输入格式:

输入在一行中,给出9个大于0的数,用空格分隔,分别代表圆的半径,正方形的边长,矩形的宽和高,梯形的上底、下底和高,三角形的底和高。

输出格式:

输出所有图形的面积和,小数点后保留3位有效数字。

输入样例:

12.6 3.5 4.5 8.4 2.0 4.5 3.2 4.5 8.4

输出样例:

578.109

全部代码:

#include <iostream>
using namespace std;
class Shape {
public:
    virtual float getArea() {
        return 0;
    }
};
class Circle :public Shape {
private:
    float radius;
public:
    Circle(float a) :radius(a) {
    }
    float getArea() {
        return radius * radius * 3.14159f;
    }
};
class Square :public Shape {
private:
    float len;
public:
    Square(float a) :len(a) {
    }
    float getArea() {
        return len * len;
    }
};
class Trapezoid :public Shape {
private:
    float up, down, height;
public:
    Trapezoid(float a, float b, float c) :up(a), down(b), height(c) {
    }
    float getArea() {
        return (up + down) * height / 2;
    }
};
class Rectangle :public Shape {
private:
    float width, height;
public:
    Rectangle(float a, float b) :width(a), height(b) {
    }
    float getArea() {
        return width * height;
    }
};
class Triangle :public Shape {
private:
    float height, down;
public:
    Triangle(float a, float b) :down(a), height(b) {
    }
    float getArea() {
        return height * down / 2;
    }
};
int main() {
    float a, b, c, d, e, f, g, h, i;
    cin >> a >> b >> c >> d >> e >> f >> g >> h >> i;
    Shape* array[5];//新建一个指针数组
    Circle c1(a); array[0] = &c1;//取内容传入
    Square s(b); array[1] = &s;
    Rectangle r(c, d); array[2] = &r;
    Trapezoid t(e, f, g); array[3] = &t;
    Triangle tr(h, i); array[4] = &tr;
    float sum = 0;
    for (int j = 0; j < 5; j++)
        sum += array[j]->getArea();//不能是array[j].getArea()否则会报错
    printf("%0.3f", sum);
}

#include<iostream> using namespace std; class CShape{ public:float Area; float Perimeter; CShape() { Area=0;Perimeter=0; } virtual void GetArea(){} virtual void GetPerimeter(){} }; class CRectangle:public CShape{ private: int l;int h; public: CRectangle(int h,int l ):CShape() {this->h=h;this->l=l; } void GetArea() {Area=l*h;cout<<"矩形的面积="<<Area<<endl;} void GetPerimeter() {Perimeter=(l+h)*2;cout<<"矩形的周长="<<Perimeter<<endl; } }; class CCirle:public CShape{ private:float r; public:CCirle(float r):CShape() {this->r=r; } void GetArea() { Area=3.14159*r*r; cout<<"圆的面积="<<Area<<endl; } void GetPerimeter() { Perimeter=r*3.14159*2; cout<<"圆的周长="<<Perimeter<<endl; } }; class CSquare:public CShape{ private: int a; public: CSquare(int a):CShape() { this->a=a; } void GetArea() { Area=a*a; cout<<"正方形面积="<<Area<<endl; } void GetPerimeter() { Perimeter=4*a; } }; class CTrapeziod:public CShape{ private: int lu; int ld; int h; public: CTrapeziod(int lu,int ld,int h):CShape() { this->lu=lu; this->ld=ld; this->h=h; } void GetArea() { Area=(lu+ld)*h/2; cout<<"梯形面积="<<Area<<endl; } }; class CTrangle:public CShape{ private: int d; int h; public: CTrangle(int d,int h):CShape() { this->d=d; this->h=h; } void GetArea() { Area=d*h/2; cout<<"三角形的面积="<<Area<<endl; } }; void main() { CShape *p[5]; CRectangle CR(5,10); CCirle CC(3); CSquare CS(4); CTrapeziod CT1(2,5,4); CTrangle CT2(6,2); p[0]=&CR; p[0]->GetArea(); p[1]=&CC; p[1]->GetArea(); p[2]=&CS; p[2]->GetArea(); p[3]=&CT1; p[3]->GetArea(); p[4]=&CT2; p[4]->GetArea(); int i, double SumArea=0.f; for(i=0;i<5;i++) { SumArea=p[i]->Area+SumArea; } cout<<"面积总和="<<SumArea<<endl; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值