任务三:点到圆到圆柱体的继承

#include <iostream>  
using namespace std;  
class Point   
{  
protected:  
    double x,y;//分别代表横坐标和纵坐标  
public:  
    Point(double m=0,double n=0);  
    void setpoint(double d,double e){x=d;y=e;}  
    friend ostream& operator << (ostream& output,Point&c);  
    double getx(){return x;}  
    double gety(){return y;}  
};  
Point::Point(double m,double n)  
{  
    x=m;  
    y=n;  
}  
ostream& operator << (ostream& output,Point&c)  
{  
    output<<"点为:"<<"("<<c.x<<","<<c.y<<")"<<endl;  
    return output;  
}  
  
  
class Circle:public Point  
{  
public:  
    Circle(double c1,double c2,double c3):Point(c1,c2){r=c3;}  
    friend ostream& operator << (ostream& output,Circle&c);  
    void setR(double);  
    double area( );  
    double getR(){return r;}  
 protected:  
    double r;//圆的半径   
};  
void Circle::setR(double m)  
{  
    r=m;  
}  
double Circle::area()  
{  
    return 3.1415926*r*r;  
}  
ostream& operator << (ostream& output,Circle&c)  
{  
    output<<"圆心:"<<"("<<c.x<<","<<c.y<<")"<<"面积为:"<<c.area()<<endl;  
    return output;  
}  
  
class Cylinder:public Circle  
{  
private:  
    double h;//圆柱的高  
public:  
    Cylinder(double t1,double t2,double t3,double t4):Circle(t1,t2,t3){h=t4;}  
    friend ostream& operator << (ostream& output,Cylinder&c);  
    double area( ) ;   
    double volume();  
    void setHeight(double m){h=m;}  
    double getHeight(){return h;}  
};  
double Cylinder::area( )  
{   
    return 2*Circle::area( )+2*3.14159*r*h;   
}  
  
double Cylinder::volume()  
{  
    return area()*h;  
}  
  
ostream& operator << (ostream& output,Cylinder&c)  
{  
    output<<"Center=["<<c.x<<","<<c.y<<"], r="<<c.r<<", h="<<c.h    
        <<"\narea="<<c.area( )<<", volume="<<c.volume( )<<endl;    
    return output;  
}  
int main( )    
{    
    Point t(5,6);  
    cout<<t;  
    Circle t1(7,3,1);  
    cout<<t1;  
    Cylinder cy1(3.5,2.4,5.2,10);    
    cout<<"\noriginal cylinder:\nx="<<cy1.getx( )<<", y="<<cy1.gety( )<<", r="    
        <<cy1.getR( )<<", h="<<cy1.getHeight( )<<"\narea="<<cy1.area()    
        <<",volume="<<cy1.volume()<<endl;    
    cy1.setHeight(5);         
    cy1.setR(7.5);          
    cy1.setpoint(5,4);           
    cout<<"\nnew cylinder:\n"<<cy1;            
    system("pause");    
    return 0;    
}    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值