第十一周项目4——类族的设计

/*
*Copyright (c) 2014,烟台大学计算机学院void change(int a[8][8]);
*All rights reserved.
*文件名称:main.cpp
*作者:苏强
*完成日期:2015年5月18日
*版本号:v1.0
*
*问题描述:定义一个Point类,派生一个Circl类,再派生出一个Cylinder类,并输出他们的信息
*/
#include<iostream>
#include<Cmath>
using namespace std;
class Point //定义坐标点类
{
protected:
    double x;
    double y;
public:
    Point(double xx,double yy):x(xx),y(yy) {}
    void setPoint(double a,double b);
    friend ostream &operator<<(ostream &output,Point &p);
};
void Point::setPoint(double a,double b)
{
    x=a;
    y=b;
}
ostream &operator<<(ostream &output,Point &p)
{
    output<<"point:("<<p.x<<","<<p.y<<")"<<endl;
    return output;
}
class Circle:public Point
{
protected:
    double r;
public:
    Circle(double a,double b,double R):Point(a,b),r(R) {}
    void setR(double Radius);
    double area();
    friend ostream&operator<<(ostream&output,Circle c);
};
double Circle::area()
{
    return r*r*3.14;
}
void Circle::setR(double Radius)
{
    r=Radius;
}
ostream&operator<<(ostream&output,Circle c)
{
    output<<"Center:"<<"("<<c.x<<"<"<<c.y<<"),R="<<c.r<<",Area="<<c.area()<<endl;
    return output;
}
class Cylinder:public Circle
{
private:
    double h;
public:
    Cylinder(double a,double b,double r,double H);
    void setHeight(double Height);
    double area1();
    double volume();
    friend ostream&operator<<(ostream&output,Cylinder c);
};
Cylinder::Cylinder(double a,double b,double r,double H):Circle(a,b,r),h(H){}
void Cylinder::setHeight(double Height)
{
    h=Height;
}
double Cylinder::area1()
{
    return (2*r*r*3.14+2*3.14*r*h);
}
double Cylinder::volume()
{
    return 3.14*r*r*h;
}
ostream&operator<<(ostream&output,Cylinder c)
{
    output<<"Center:"<<"("<<c.x<<","<<c.y<<"),R="<<c.r<<",H="<<c.h<<",Area="<<c.area()<<endl;
    output<<"AllArea="<<c.area1()<<",Volunme="<<c.volume()<<endl;
    return output;
}
int main( )
{
    Cylinder cy1(3.5,6.4,5.2,10);
    cout<<cy1<<endl;
    cy1.setHeight(15);
    cy1.setR(7.5);
    cy1.setPoint(5,5);
    cout<<"\nnew cylinder:\n"<<cy1;
    return 0;
}


 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值