*烟台大学计算机学院学生
*All right reserved.
*文件名称*烟台大学计算机学院学生
*All right reserved.
*文件名称:项目4 - 类族的设计
*作者:杨飞
*完成日期:2014年5月5日
*版本号:v1.0
*对任务及求解方法的描述部分:项目4 - 类族的设计
*我的程序:
#include <iostream>
using namespace std;
const double pi=3.14;
class point
{
protected:
int x,y;
public:
point(double x1=0,double y1=0);
void setpoint(double x1,double y1);
friend ostream &operator <<(ostream &output,point &c);
double getx()
{
return x;
}
double gety()
{
return y;
}
};
point::point(double x1,double y1)
{
x=x1;
y=y1;
}
void point::setpoint(double x1,double y1)
{
x=x1;
y=y1;
}
ostream &operator <<(ostream &output,point &c)
{
output<<"("<<c.x<<","<<c.y<<")"<<endl;
return output;
}
class circle :public point
{
protected:
double r;
public:
friend ostream &operator<<(ostream &output,circle &c);
circle(int x1,int y1,double a):
point(x1,y1),r(a){};
void setr(double a);
double getr();
double area();
};
void circle::setr(double a)
{
r=a;
}
double circle::getr()
{
return r;
}
double circle::area()
{
double s;
s=pi*r*r;
return s;
}
ostream &operator<<(ostream &output, circle &c)
{
output<<"yuanxin=("<<c.x<<", "<<c.y<<"), r="<<c.r<<", area="<<c.area( )<<endl;
return output;
}
class cylinder:public circle
{
private:
double h;
public:
friend ostream &operator<<(ostream &output,cylinder &c);
cylinder(double x1,double y1,double a,double b):
circle(x1,y1,a),h(b){};
void seth(double b);
double geth();
double area1();
double volume();
};
void cylinder::seth(double b)
{
h=b;
}
double cylinder::geth()
{
return h;
}
double cylinder::area1()
{
double s;
s=2*area()+2*pi*r*h;
return s;
}
double cylinder::volume()
{
double v;
v=area()*h;
return v;
}
ostream &operator<<(ostream &output,cylinder &c)
{
output<<"yuanxin=("<<c.x<<", "<<c.y<<"), r="<<c.r<<endl;
output<<"表面积="<<c.area1( )<<"体积="<<c.volume()<<endl;
return output;
}
int main()
{
cout<<"原来的数据和结果:"<<endl;
cylinder t(3,1,5.2,10);
cout<<"圆心坐标="<<endl;
cout<<"x="<<t.getx()<<endl;
cout<<"y="<<t.gety()<<endl;
cout<<"半径="<<t.getr()<<endl;
cout<<"底面面积="<<t.area()<<endl;
cout<<"表面积="<<t.area1()<<endl;
cout<<"体积"<<t.volume()<<endl;
cout<<"更改半径,坐标,和高后的运行结果:"<<endl;
t.seth(12);
t.setr(6);
t.setpoint(4,2);
cout<<t;
return 0;
}
运行结果:
心得体会:呵呵