第十周——面向对象——继承与派生——项目四(2)类族的设计

89 篇文章 0 订阅
18 篇文章 0 订阅

问题及代码:

/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:lily.cpp
*作者:李莉
*完成日期:2015年5月18日
*版本号:v1.0
*
*问题描述:先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试
           以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试
*程序输入:无输入
*程序输出:如结果图
*/
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
    Point(double x0,double y0):x(x0),y(y0) {}
    void setPoint(double a,double b);
    double getX();
    double getY();
    void display();
public:
    double x;
    double y;
};
void Point::setPoint(double a,double b)
{
    x=a;
    y=b;
}
double Point::getX()
{
    return x;
}
double Point::getY()
{
    return y;
}
void Point::display()
{
    cout<<"Point:"<<"("<<x<<","<<y<<")"<<endl;
}
class Circle:public Point
{
public:
    Circle(double x0,double y0,double r0);
    void setR(double r0);
    double getarea();
    void display1();
protected:
    double r;
};
Circle::Circle(double x0,double y0,double r0):Point(x0,y0),r(r0){}
void Circle::setR(double r0)
{
    r=r0;

}
double Circle::getarea()
{
    return 3.14159*r*r;
}
void Circle::display1()
{
    display();
    cout<<"Circle's x="<<x<<" "<<"y="<<y<<" "<<"radius="<<r<<" "<<"area="<<getarea()<<endl;
}
int main()
{
    Circle p(1.2,5.2,2.0);
    cout<<"The message of the Circle is:"<<endl;
    p.display1();
    return 0;
}


运行结果:

心得体会:

原谅我实在不知道设计的那个点的坐标有什么用。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值