第十一周 项目4-类族的设计(3)--补

本文介绍了一个基于C++的圆柱类设计,该类继承自Circle类,并新增了高度属性及圆柱表面积与体积的计算方法。文章通过具体示例展示了类的使用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* 
 * Copyright (c) 2014, 烟台大学计算机学院 
 * All rights reserved. 
 * 文件名称:test.cpp 
 * 作    者:呼亚萍 
 * 完成日期:2015年5月27日 
 * 版 本 号:v1.0 
 * 
 * 问题描述: 再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),,以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。
 * 程序输入:相应的程序
 * 程序输出:对应得结果
 */

#include <iostream>
using namespace std;
class Point
{
public:
    Point(double x=0,double y=0);      //构造函数
    void setPoint(double,double);                  //设置坐标值
    double getX( ) const
    {
        return x;   //读x坐标
    }
    double getY( ) const
    {
        return y;   //读y坐标
    }
    friend ostream & operator<<(ostream &,const Point &);//重载运算符“<<”
protected:           //受保护成员
    double x,y;
};


//Point的构造函数
Point::Point(double a,double b)
{
    x=a;
    y=b;
}
//设置x和y的坐标值
void Point::setPoint(double a,double b)
{
    x=a;
    y=b;
}

ostream & operator<<(ostream &output,const Point &p)
{
    output<<"["<<p.x<<","<<p.y<<"]"<<endl;
    return output;
}

class Circle:public Point   //circle是Point类的公用派生类
{
public:
    Circle(double x=0,double y=0,double r=0);  //构造函数
    void setRadius(double);                  //设置半径值
    double getRadius( ) const;               //读取半径值
    double area ( ) const;                   //计算圆面积
    friend ostream &operator<<(ostream &,const Circle &);//重载运算符“<<”
protected:
    double radius;
};

//定义构造函数,对圆心坐标和半径初始化
Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { }

//设置半径值
void Circle::setRadius(double r)
{
    radius=r;
}

//读取半径值
double Circle::getRadius( ) const
{
    return radius;
}

//计算圆面积
double Circle::area( ) const
{
    return 3.14159*radius*radius;
}

//重载运算符“<<”,使之按规定的形式输出圆的信息
ostream &operator<<(ostream &output,const Circle &c)
{
    output<<"Center=["<<c.x<<", "<<c.y<<"], r="<<c.radius<<", area="<<c.area( )<<endl;
    return output;
}
class Cylinder:public Circle
{
public:
    Cylinder(double a,double b,double r,double h):Circle(a,b,r),hight(h) {}
    double area();
    void set_hight(double h);
    double get_hight();
   double volume();
private:
    double hight;
};
void Cylinder::set_hight(double h)
{
    hight=h;
}
double Cylinder::get_hight()
{
    return hight;
}
double Cylinder::area()
{
    return (2*Circle::area()+2*3.14*radius*hight);
}
double Cylinder::volume()
{
    return(Circle::area()*hight);
}
int main( )
{
    Cylinder cy1(3.5,6.4,5.2,10);
    cout<<"\noriginal cylinder:\nx="<<cy1.getX( )<<", y="<<cy1.getY( )<<", r="
        <<cy1.get_hight()<<", h="<<cy1.get_hight()<<"\narea="<<cy1.area()
        <<",volume="<<cy1.volume()<<endl;
    cy1.set_hight(15);
    cy1.set_hight(7.5);
    cy1.setPoint(5,5);
    cout<<"\nnew cylinder:\n"<<cy1;
    return 0;

    return 0;
}



运算结果;

知识点总结;

类的派生,在派生中调用基类成员的应用!

学习心得:

通过多次实践,对知识点有越来越清晰的理解!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值