第十一周项目五 点,圆,柱类族的设计

本文通过C++代码示例介绍了如何实现图形类的多级多继承,包括点类(Point)、圆类(Circle)和圆柱类(Cylinder)的定义与使用。文章详细解释了各成员函数的功能,并演示了如何输入和输出这些类的实例。

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

问题及代码

/*    
ALL rights reserved.                           
*文件名称: 初学对象11                       
作者:李长鸿                       
完成时间:2015.5.26                 
*问题描述: 日期时间类的多继承   
*/  
#include<iostream>
#include<Cmath>
using namespace std;
double const l=3.14;
class Point
{
    double x,y;
public:
    Point(double a=0,double b=0):x(a),y(b) {}
    void setp()
    {
        char a,b,c;
        cout<<"请输入圆心的坐标(格式为(3,1)):";
        cin>>a>>x>>b>>y>>c;
        while(a!='('||b!=','||c!=')')
        {
            cout<<"输入有误,请按照格式重新输入:";
            cin>>a>>x>>b>>y>>c;
        }
    }
    void outp()
    {
        cout<<"圆心坐标为:"<<"("<<x<<","<<y<<")"<<"; ";
    }
    ~Point()
    {
        cout<<"点类使用结束,谢谢!"<<endl;
    }
};
class Circle:public Point
{
    double r;
public:
    Circle(double a,double b,double c):Point(a,b),r(c) {}
    double getr()
    {
        return r;
    }
    void setc()
    {
        setp();
        cout<<"请输入半径:";
        cin>>r;
        while(r<=0)
        {
            cout<<"您输入的半径r已小于等于0,请重新输入正确的半径:";
            cin>>r;
        }
    }
    void outc()
    {
        outp();
        cout<<"圆的半径为:"<<r<<"; "<<"面积为:"<<area()<<"; ";
    }
    double area();
    ~Circle()
    {
        cout<<"圆类使用结束,谢谢!"<<endl;
    }
};
class Cylinder:public Circle
{
    double h;
public:
    Cylinder(double a,double b,double c,double d):Circle(a,b,c),h(d) {}
    void seth()
    {
        cout<<"请输入圆柱体的高:";
        cin>>h;
        while(h<=0)
        {
            cout<<"您输入的圆柱体的高h已小于等于0,请重新输入正确的高:";
            cin>>h;
        }
    }
    double geth()
    {
        return h;
    }
    double cylinderarea();
    ~Cylinder()
    {
        cout<<"圆柱体使用结束,谢谢!"<<endl;
    }
};
istream &operator>>(istream&in,Cylinder&p)
{
    p.setc();
    p.seth();
}
ostream &operator<<(ostream&out,Cylinder&p)
{
    p.outc();
    out<<"圆柱的高为:"<<p.geth()<<";  "<<"体积为:"<<p.cylinderarea()<<endl;
}
double Cylinder::cylinderarea()
{
    return 2*l*getr()*geth()+2*area();
}
double Circle::area()
{
    return l*r*r;
}
int main()
{
    Cylinder cyl(3,5,7,7);
    cout<<cyl;
    cin>>cyl;
    cout<<cyl;
    cout<<endl;
    return 0;
}


总结:还是之前忘了那些&&&&&&&。。。。。唉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值