zoukankan      html  css  js  c++  java
  • ss

    Description
    定义抽象类型Shape,由他派生三个类:Circle(圆形),Rectangle(矩形),Trapezoid(梯形),用一个函数printArea分别输出三者的面积。主函数已经给出,3个图形的数据在定义对象时给定,根据提示完成程序并提交。

    注意:抽象类的定义及使用

    主函数如下:

     

    int main()
    {
        Circle circle(12.6);
        cout<<"area of circle=";
        printArea(circle);
        Rectangle rectangle(4.5,8.4);
        cout<<"area of rectangle=";
        printArea(rectangle);
        Trapezoid trapezoid(4.5,8.4,8.0);
        cout<<"area of trapezoid=";
        printArea(trapezoid);
        return 0;
    }

    Input
    Output

    输出圆的面积

    输出矩形面积

    输出梯形面积

    Sample Input
    Sample Output
    area of circle=498.759

    area of rectangle=37.8

    area of trapezoid=52.5

    #include<iostream>
    using namespace std;

    const double PI=3.14159;

    class Shape   //抽象类
    {
    protected:
        double x, y, z;
    public:
        Shape(double d, double w=0,double z=0){x=d;y=w;this->z=z;}
        double getx() {return x;}
        double gety() {return y;}
        double getz() {return z;}
    };

    class Rectangle:public Shape
    {
    public:
        Rectangle(double d, double w=0,double z=0):Shape(d,w,z){}
    };

    class Circle:public Shape{
    public:
       Circle(double d, double w=0,double z=0):Shape(d,w,z){}
    };

    class Trapezoid:public Shape{
    public:
       Trapezoid(double d, double w=0,double z=0):Shape(d,w,z){}
    };

    void printArea(Circle c)
    {
        cout<<PI*c.getx()*c.getx()<<endl;
    }

    void printArea(Rectangle r)
    {
        cout<<r.getx()*r.gety()<<endl;
    }
    void printArea(Trapezoid t)
    {
        cout<<(t.getx()+t.getz())*t.gety()/2.0<<endl;
    }

    int main()
    {
        Circle circle(12.6);
        cout<<"area of circle=";
        printArea(circle);
        Rectangle rectangle(4.5,8.4);
        cout<<"area of rectangle=";
        printArea(rectangle);
        Trapezoid trapezoid(4.5,8.4,8.0);
        cout<<"area of trapezoid=";
        printArea(trapezoid);
        return 0;
    }

  • 相关阅读:
    108. Convert Sorted Array to Binary Search Tree
    107. Binary Tree Level Order Traversal II
    106. Construct Binary Tree from Inorder and Postorder Traversal
    105. Construct Binary Tree from Preorder and Inorder Traversal
    104. Maximum Depth of Binary Tree
    103. Binary Tree Zigzag Level Order Traversal
    102. Binary Tree Level Order Traversal
    系统和进程相关信息
    文件I/0缓冲
    系统编程概念(文件系统mount等函数的使用)
  • 原文地址:https://www.cnblogs.com/zeross/p/4581681.html
Copyright © 2011-2022 走看看