前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++创建一个名为Ellipse的椭圆类--练习

C++创建一个名为Ellipse的椭圆类--练习

作者头像
Enterprise_
发布2018-05-18 15:19:48
1.2K0
发布2018-05-18 15:19:48
举报
文章被收录于专栏:小L的魔法馆小L的魔法馆
  • 题目描述:
代码语言:javascript
复制
/*设计名为Ellipse的椭圆类*/
/*
其属性为外接矩形的左上角与右下角两个点的坐标,并能计算出椭圆的面积,并测试该类。
*/
  • 代码如下:
代码语言:javascript
复制
#include<iostream>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const double PI = acos(-1.0);
class Point {
public:
    Point() {}
    Point(int a, int b) {
        setX(a);
        setY(b);
    }
    Point(Point &tp) {
        this->setX(tp.getX());
        this->setY(tp.getY());
    }
    Point get_Point()
    {
        cout << "Please enter the coordinate of another point[ 9 6 menas the point is(9,6) ]:";
        cin >> x >> y;
        return *this;
    }
    ~Point(){}
    void details() {
        cout << "The Point:"
            << "X-coordinate:" << x << endl
            << "Y-coordinate:" << y << endl;
    }
    int getX() {
        return x;
    }
    int getY() {
        return y;
    }
    void setX(int a) {
        x = a;
    }
    void setY(int b) {
        y = b;
    }
private:
    int x;
    int y;
};
class Ellipse {
public:
    Ellipse(int x1, int y1, int  x2, int y2) {
        Pa.setX(x1);
        Pa.setY(y1);
        Pb.setX(x2);
        Pb.setY(y2);
    }
    Ellipse(Point &a,Point &b) {    
        Pa = a;
        Pb = b;
    }
    Ellipse(){}
    ~Ellipse(){}
    double area() {
        double a = fabs(Pa.getX() - Pb.getX())*1.0;
        double b = fabs(Pa.getY() - Pb.getY())*1.0;
        return PI*a*b;
    }
    void show() {
        cout << "The area of the Ellipse is:"
            << area() << endl;
        cout << "The coordinates of the upper-left corner of its external rectangle and the two points in the lower-right corner are:" <<endl;
            Pa.details();
            Pb.details();
    }
private:
    Point Pa, Pb;
};
int main(void)
{
    Point a, b;
    a.get_Point();
    b.get_Point();
    Ellipse asp(a, b);
    asp.show();
    return 0;
}
  • 测试截图:
这里写图片描述
这里写图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年04月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档