第十一周 项目4 - 类族的设计(2)圆

问题及代码:

/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作    者:宋健
* 完成日期:2015年 5月 26日
* 版 本 号:v1.0
*
* 问题描述: 
* 程序输入:
* 程序输出:
*/
#include <iostream>

using namespace std;
const double pi=3.14;
class Point
{
protected:
    double x,y;
public:
    Point() {};
    Point(double ,double);
    double getx();
    double gety();
    friend ostream &operator <<(ostream &output,const Point &p);
};
class Circle:public Point
{
protected:
    double r;
public:
    double area();
    Circle()
    {
        r=0;
    };
    void setr(double);

};
void Circle::setr(double m)
{
    r=m;
}
double Circle::area()
{
    double ar;
    ar=pi*r*r;
    return ar;
}
Point::Point(double m,double n)
{
    x=m;
    y=n;

}
double Point::getx()
{
    return x;
}
double Point::gety()
{
    return y;
}
ostream &operator <<(ostream &output,const Point &p)
{
    output<<"("<<p.x<<","<<p.y<<")"<<endl;
    return output;
}
int main()
{
    Point p(2.3,8.4);
    Circle c;
    double r;
    cout<<"x="<<p.getx( )<<",y="<<p.gety( )<<endl;
    cout<<"p:"<<p<<endl;
    cout<<"输入圆的半径:"<<endl;
    cin>>r;
    c.setr(r);
    cout<<"圆的面积为:"<<c.area();
    return 0;

}

运行结果:

知识点总结:

设置好数据类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值