第5周cplus作业:设计圆类

设计并实现一个圆类 Circle ,具体要求如下:
数据成员
center 用于表示圆心坐标,是一个 Point 类的对象
radious 用于表示半径。要求半径可以取小数。
函数成员
带有两个参数,一个参数用于传递圆心信息,一个参数用于传递半径
当创建 Circle 类对象时,如果没有指定半径,默认半径取值 10.0
比如,这样定义 Circle 类对象: Circle c(p1) ; Circle c(p1, 5) ;
带有三个参数,前两个参数分别表示圆心的坐标,第三个参数用于传递半径
当创建 Circle 类对象时,提供圆心的 x 坐标值和 y 坐标值,以及半径
比如,这样定义 Circle 类对象: Circle c(1.0, 1.0, 5) ;
复制构造函数
当创建 Circle 类对象时,允许用一个已经存在的 Circle 类对象来创建新的
比如,这样定义 Circle 类对象: Circle c2(c1) ;
析构函数
普通函数成员 area()
用于计算并返回圆形面积
普通函数成员 circumference()
用于计算并返回圆形周长
普通函数成员 get_center()
用于返回圆心
普通函数成员 get_radious()
用于返回半径
Circle 类中,成员数据圆心 center Point 类的对象。
Point 具体设计如下:
数据成员
x 用于表示点坐标的 x 值,要求允许取小数
y 用于表示点坐标的 y 值, 要求允许取小数
函数成员
带有两个参数且允许有默认值的普通构造函数
当创建 Point 类对象时,如果没有指定初始坐标值, x y 都取默认值 0
比如,这样定义 Point 类对象: Point p1 ; Point p1(2) ; Point p1(2.3, 5.2) ;
复制构造函数
当创建 Point 类对象时,允许用一个已经存在的 Point 对象来构造新的。
比如,这样定义 Point 类对象: Point p1 ; Point p2(p1) ; 析构函数
普通成员函数 get_x()
用于返回 x 坐标值
普通成员函数 get_y()
用于返回 y 坐标值
编写一个普通函数 output() ,以 Circle 类的对象作为参数,采用引用传递方式,输出圆的信息,包括圆
心、半径、周长、面积。
main() 函数中定义 Circle 类对象,测试其使用,调用函数 output() 完成输出。
已知程序框架和预期输出测试结果如下,请补全程序。程序补全完整后,更换 main() 中测试数据,运行
测试。
#include <bits/stdc++.h>
using namespace std;
constexpr double PI = 3.14;

// 类Point的定义
class point{
public:
    point(double x1 = 0, double y1 = 0):x(x1),y(y1){};
    point(const point &P);
    ~point() = default;
    double get_x(){return x;}
    double get_y(){return y;}
private:
    double x, y;
}; 
point::point(const point &p){
    x = p.x;
    y = p.y;
}

// Circle类的定义
class circle{
public:
    circle(point cen,double r = 10.0):center(cen),radious(r){};
    //circle(double x1 = 10.0,double y1 = 10.0,double r):center(point(x1, y1)),radious(r){};
    ~circle() = default;
    circle(const circle &c){
        center = c.center;
        radious = c.radious;
    }
    circle(double x1,double y1,double r):center(point(x1, y1)),radious(r){};
    
    double area(){return PI * radious * radious;}
    double circumference(){return 2 * PI * radious;}
    point get_center(){return center;}
    double get_radious(){return radious;}
private:
    point center;
    double radious;
};



// 普通函数,输出圆的信息: 圆心、半径、周长、面积
void output(const circle &c) {
	circle temp = c;
    cout << "圆心" << ":" <<"("<<temp.get_center().get_x()<<","<<temp.get_center().get_y()<<")"<<endl;
    cout << "半径" << ":" <<temp.get_radious()<< endl;
    cout << "周长" << ":" <<temp.circumference()<< endl;
    cout << "面积" << ":" <<temp.area()<< endl;
}

// 主函数:测试类
int main()
{
    point p1;			// 定义一个Point类对象p1, 坐标采用默认值
    circle c1(p1);   	// 定义一个Circle了对象c1, 以p1为圆心,半径采用默认值
    output(c1);			// 调用output()函数,输出圆对象c1的信息

    std::cout << std::endl;

    point p2(2.2, 5.7);	// 定义一个Point类对象p2, 指定其初始坐标
    circle c2(p2, 1);	// 定义一个Circle类对象c2, 以p2为圆心,1为半径
    output(c2);			// 调用output()函数,输出圆对象c2的信息

    std::cout << std::endl;

    circle c3(c2);      // 定义一个Circle类对象c3, 使用对象c2构造
    output(c3);

    std::cout << std::endl;

    circle c4(1.0, 1.0, 5);     // 定义一个Circle类对象c4, 圆心坐标(1.0, 1.0), 半径5
    output(c4);

    system("pause");
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值