第十、十一周项目一-点-圆-圆柱类族的设计(3)

<pre name="code" class="cpp">/*
 *Copyright(c)2016,烟台大学计算机与控制工程学院
 *All rights reserved
 *文件名称:123.cpp
 *作    者:王蕊
 *完成日期:2016年5月6日
 *版 本 号:v1.0
 *
 *问题描述:以点为基类,派生出一个圆类,增加数据成员r,和求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试。
 *输入描述:无。
 *程序输出:圆的圆心坐标,半径和面积。
*/
#include <iostream>
using namespace std;
class Point
{
public:
    Point(double x=0,double y=0);                    //构造函数Point
    void setPoint(double,double);                  设置x和y的坐标值
    double getX( )
    {
        return x;
    }              //x坐标
    double getY( )
    {
        return y;
    }              //y坐标
    void show();
protected:                                       //受保护的成员
    double x,y;
};
void Point::setPoint(double m,double n)  //设置x和y的坐标值
{
    x=m;
    y=n;
}
Point::Point(double m,double n)  //Point的构造函数
{
    x=m;
    y=n;
}
void Point::show()
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}
class Circle:public Point   //Circle是Point类的公用派生类
{
public:
    Circle(double x=0,double y=0,double r=0);  //构造函数Circle
    void setR(double);                  //设置半径的值
    double getR( );               //读取半径的值
    double area ( );                   //计算圆的面积
    void show();
protected:
    double radius;
};
Circle::Circle(double m,double n,double r):Point(m,n),radius(r) { } //定义构造函数

void Circle::setR(double r)  //设置半径的值
{
    radius=r;
}
double Circle::getR( )  //读取半径的值
{
    return radius;
}
double Circle::area( )  //计算圆的面积
{
    return radius*radius*3.14159;
}
void Circle::show()  //输出点和圆的面积
{
    cout<<"Point=("<<x<<", "<<y<<"), r="<<radius<<", area="<<area( )<<endl;
}
class Cylinder:public Circle
{
public:
    Cylinder (double x=0,double y=0,double r=0,double h=0);//构造函数Cylinder
    void setHeight(double);                      //设置圆柱的高
    double getHeight( ) const;                    //读取圆柱的高
    double area( ) const;                         //计算圆柱的表面积
    double volume( ) const;                       //计算圆柱的体积
    void show();
protected:
    double height;                               //圆柱高
};
Cylinder::Cylinder(double m,double n,double r,double h) :Circle(m,n,r),height(h){}  //定义构造函数

void Cylinder::setHeight(double h)  //设置圆柱的高
{
    height=h;
}
double Cylinder::getHeight( ) const  //读取圆柱的高
{
    return height;
}
double Cylinder::area() const  //计算圆柱的表面积
{
    return 2*(radius*radius*3.14159)+2*3.14159*radius*height;
}
double Cylinder::volume() const  //计算圆柱的体积
{
    return radius*radius*3.14159*height;
}

void Cylinder::show()
{
    cout<<"Point=("<<x<<","<<y<<"), r="<<radius<<", h="<<height
        <<", area="<<area( )<<", volume="<<volume( )<<endl;
}
int main( )
{
    Cylinder cy1(1,2,3,4);
    cout<<"坐标,半径,高,圆柱表面积,圆柱体积"<<endl;
    cy1.show();
    cy1.setHeight(5);
    cy1.setR(6);
    cy1.setPoint(2,1);
    cout<<"改变后的坐标,半径,高,圆柱表面积,圆柱体积"<<endl;
    cy1.show();
    return 0;
}


 

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要定义一个圆柱,包含半径度两个属性。然后,我们可以在圆柱中定义一个计算圆柱体积的方法,该方法使用半径度计算圆柱体积。 具体实现如下: 1. 定义圆类 ``` public class Circle { private double radius; // 半径 public Circle(double radius) { this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public double getArea() { return Math.PI * radius * radius; // 计算面积 } } ``` 2. 定义圆柱 ``` public class Cylinder { private Circle circle; // 圆柱底面 private double height; // 圆柱度 public Cylinder(Circle circle, double height) { this.circle = circle; this.height = height; } public Circle getCircle() { return circle; } public void setCircle(Circle circle) { this.circle = circle; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getVolume() { return circle.getArea() * height; // 计算圆柱体积 } } ``` 3. 使用圆柱计算圆柱体积 ``` public class Main { public static void main(String[] args) { Circle circle = new Circle(2.); // 创建底面 Cylinder cylinder = new Cylinder(circle, 3.); // 创建圆柱 double volume = cylinder.getVolume(); // 计算圆柱体积 System.out.println("圆柱体积为:" + volume); } } ``` 输出结果为: ``` 圆柱体积为:37.69911184307752 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值