C++

实验四

2018-05-29  本文已影响2人  修夏之夏i

在已有的Point类的基础上,定义一个“Circle”派生类,
要求:新增一个半径成员;能计算并输出圆的周长及加圆面积

#define _CRT_SECURE_N0_WARNINGS 1
#include<iostream>
using namespace std;

//在已有的Point类的基础上,定义一个“Circle”派生类,
//要求:新增一个半径成员;能计算并输出圆的周长及加圆面积
class Point
{
private:
    float x, y;
public:
    Point(float a = 0, float b = 0)//构造
    {
        x = a;
        y = b;
    }
    void setpoint(float a = 0, float b = 0)
    {
        x = a;
        y = b;
    }
    void printpoint(void)
    {
        cout << "x=" << x << endl;
        cout << "y=" << y << endl;
    }
};
class Circle :public Point
{
private:
    float r;//新增私有成员
public:
    Circle(float a = 0, float b = 0, float c = 0) :Point(a, b)//派生构造
    {
        r = c;
    }
    void setpoint(float a = 0, float b = 0, float c = 0)
    {
        Point::setpoint(a, b);
        r = c;
    }
    double length()
    {
        double ret = 2 * 3.14*r;
        return ret;
    }
    double area()
    {
        double ret = 3.14*r*r;
        return ret;
    }
};
void main(void)
{
    Circle a(3.0, 4.0, 5.0);
    cout << "length=" << a.length() << endl;
    cout << "area=" << a.area() << endl;
}
运行结果: Circle.png
上一篇 下一篇

猜你喜欢

热点阅读