C++8-2-多态:计算面积(虚函数,动态多态性)

现在要计算长方形、圆、三角形三种类型区域面积,首先输入一个类型指定信息type,若type=0,表示圆,接下来会输入其半径;若type=1,表示三角形,接下来输入其三条边;若type=2,表示长方形,接下来输入其长和宽。最后输出相应区域面积。

圆周率取3.14。

#include <cstdio>

#include <cmath>

#include <iostream>

#include <iomanip>



using namespace std;



enum shape{CIRCLE, TRIANGLE, RECTANGLE};



class Shape

{

public:

    Shape(){};

    virtual void showArea() = 0;

};



class Circle:public Shape

{

public:

    Circle(double r)

    {

        radius = r;

    }

    //补充该函数

    void showArea()    

    {

    }

private:

    double radius;

};



class Triangle:public Shape

{

public:

    Triangle(double a1,double b1,double c1)

    {

    a = a1;

    b = b1;

    c = c1;

    }

    //补充该函数

    void showArea()

    {

    }

private:

    double a, b, c;

};



class Rectangle:public Shape

{

public:

    Rectangle(double x, double y)

    {

    width = x;

    height = y;

    }

    //补充该函数

    void showArea()

    {

    }

private:

    double width, height;

};



//补充该函数

void calArea(Shape* ptr){

}



void init(){

    //补充该函数,在这里进行浮点输出初始化

}



int main(){

    init();

    int type;

    cin >> type;

    if (type == CIRCLE) {

        double r;

        cin >> r;

        Circle circle(r);

        calArea(&circle);

        }

    else if (type == TRIANGLE) {

        double a, b, c;

        cin >> a >> b >> c;

        Triangle triangle(a, b, c);

        calArea(&triangle);

    }

    else {

        double x, y;

        cin >> x >> y;

        Rectangle rectangle(x, y);

        calArea(&rectangle);

    }

}

输入描述

输入保证几何图形是合法的(边长>=0,三角形合法性等)

输出描述

输出相应区域面积,为一个浮点数,始终保留两位小数。

示例1:

输入:1 89 91 93

输出:3582.31

#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;

enum shape{CIRCLE, TRIANGLE, RECTANGLE};

class Shape // 基类,抽象类
{
public:
    Shape(){}; // 默认构造函数
    virtual void showArea() = 0; // 纯虚函数(动态多态性)
};

class Circle:public Shape // 派生类
{
public:
    Circle(double r) // 构造函数初始化
    {
        radius = r;
    }

    //补充该函数
    void showArea()    
    {
      cout << 3.14 * radius * radius << endl;
    }

private:
    double radius;
};

class Triangle:public Shape // 派生类(三角形)
{
public:
    Triangle(double a1,double b1,double c1)
    {
    a = a1;
    b = b1;
    c = c1;
    }

    //补充该函数
    void showArea()
    {
      cout << 0.25*sqrt((a+b+c)*(a+b-c)*(a+c-b)*(b+c-a)) << endl;
      // 1/4sqrt[(a+b+c)(a+b-c)(a+c-b)(b+c-a)]
    }

private:
    double a, b, c;
};

class Rectangle:public Shape
{
public:
    Rectangle(double x, double y)
    {
    width = x;
    height = y;
    }
    
    //补充该函数
    void showArea()
    {
    cout << width * height << endl;
    }

private:
    double width, height;
};

//补充该函数
void calArea(Shape* ptr)
{
  ptr->showArea();
}

void init()
{
    //补充该函数,在这里进行浮点输出初始化
  cout<<setiosflags(ios::fixed)<<setprecision(2);
  // 需要头文件#include <iomanip>
  // 然后再输出实数类型变量,即可以保留2位小数输出了。
  // 当要保留三位小数,setprecision(3)就行。
  // setprecision是指设置输出精度,
  // 当没有cout<<setiosflags(ios::fixed)时,输出格式是数据的有效位数。
}

int main(){
    init();
    int type;
    cin >> type;
    if (type == CIRCLE) { // 圆
        double r;
        cin >> r;
        Circle circle(r);
        calArea(&circle);
        }
    else if (type == TRIANGLE) {  // 三角形
        double a, b, c;
        cin >> a >> b >> c;
        Triangle triangle(a, b, c);
        calArea(&triangle);
    }
    else { // 矩形
        double x, y;
        cin >> x >> y;
        Rectangle rectangle(x, y);
        calArea(&rectangle);
    }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值