继承——定义基类Point和派生类Circle,求圆的周长.

定义基类Point(点)和派生类Circle(圆),求圆的周长。Point类有两个私有的数据成员float x,y;Circle类新增一个私有的数据成员半径float r和一个公有的求周长的函数getCircumference();主函数已经给出,请编写Point和Circle类。

#include <iostream>
#include<iomanip>
using namespace std;
//请编写你的代码
int main()
{
    float x,y,r;
    cin>>x>>y>>r;
    Circle c(x,y,r);
    cout<<fixed<<setprecision(2)<<c.getCircumference()<<endl;
    return 0;
}

输入格式:

输入圆心和半径,x y r中间用空格分隔。

输出格式:

输出圆的周长,小数点后保留2位有效数字。

输入样例:

1 2 3

输出样例:

在这里给出相应的输出。例如:

Point constructor called
Circle constructor called
18.84
Circle destructor called
Point destructor called

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

使用单继承,公用继承:注意构造函数、析构函数

#include<bits/stdc++.h>
using namespace std;
const double pi=3.14;
class Point{
private :
    float X,Y;
public :
    Point(){}
    Point(float x,float y):X(x),Y(y){
        printf("Point constructor called\n");
    }
    ~Point(){
        printf("Point destructor called\n");
    }
};
class Cir:public Point{
    private:
    float r;
    public:
        float c;
        Cir(){}
        Cir(float x,float y,float r):Point(x,y),r(r){
            printf("Circle constructor called\n") ;
        }
         ~Cir(){
            printf("Circle destructor called\n");
        }

        float getCircumference(){
            c=2*r*pi;
            return 2*r*pi;
        }
};

int main()
{
    float x,y,r;
    cin>>x>>y>>r;
    Cir c(x,y,r);
    cout<<fixed<<setprecision(2)<<c.getCircumference()<<endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值