源程序:
#include<iostream>
#include<Cmath>
#define P 3.14
using namespace std;
class Point //定义坐标点类
{
protected:
double x, y; //点的横坐标和纵坐标
public:
Point(double x0, double y0):x(x0), y(y0){}
~Point(){}
double getx()
{
return x;
}
double gety()
{
return y;
}
};
class Circle: public Point//定义圆类
{
protected:
double r;//定义圆的半径
public:
Circle(double x0, double y0, double r0):Point(x0, y0),r(r0){}
~Circle(){}
double getr()
{
return r;
}
double Area()
{
return P * getr() * getr();
}
double Grith()
{
return P * getr() * 2;
}
};
class Cylinder:public Circle//定义圆柱类
{
protected:
double h;//定义圆柱类的高
public:
Cylinder(double x0, double y0, double r0, double h0):Circle(x0, y0, r0), h(h0){}
~Cylinder(){}
double geth()
{
return h;
}
double Volume()
{
return Area() * h;
}
double WArea()
{
return (2 * Area() + Grith() * h);
}
/* friend istream& operator >> (istream& input, Cylinder &c1)
{
cout << "请输入一个圆柱底面○的圆心坐标(x和y之间用空格隔开):";
input >> c1.x >> c1.y;
cout << endl;
cout << "请输入一个圆柱底面○的半径:";
input >> c1.r;
cout << endl;
cout << "请输入一个圆柱的高";
input >> c1.h;
return input;
}
*/
friend ostream& operator << (ostream& output, Cylinder &c1)
{
output << "圆柱底面○的圆心为:" << "(" << c1.getx() << "," << c1.gety() << ")" << endl;
output << "圆柱底面○的半径为:" << c1.getr() << endl;
output << "圆柱底面○的面积为:" << c1.Area() << endl;
output << "圆柱底面○的周长为:" << c1.Grith() << endl;
output << "圆柱的高为:" << c1.geth() <<endl;
output << "圆柱的表面积为:" << c1.WArea() << endl;
output << "圆柱的体积为:" <<c1.Volume() << endl;
return output;
}
};
int main()
{
Cylinder c(0, 1, 2, 3);
cout << c;
system("pause");
return 0;
}
截图:
函数都是在类里面直接定义了···因为一开始觉得做不出来,只是想做做试试···后来有思路了···可是太晚了,逸夫要关门了···哎···忘记带优盘或者下载线了···只好发了···纠结····为什么我重载">>"不行呢?奇怪···